Hi, I need help in the following question .
I have a cpu that knows to do the computations on 32 bits(unsigned
integer(
write a function that gets 2 64 bits numbers and return their sum.
I start with the following structure.
typedef struct{
unsigned int low;
unsigned int high;
}64bits;
and define the nums
64bits num1.num2;
1.I know that the limit of unsigned integer is ~64000 what happend in
case of overflow it start again from 0.?
2.could someone add solution for the question? 19 2757
On 13 May, 18:17, sarahh <cohen...@gmail .comwrote:
Hi, I need help in the following question .
I have a cpu that knows to do the computations on 32 bits(unsigned
integer(
write a function that gets 2 64 bits numbers and return their sum.
I start with the following structure.
typedef struct{
unsigned int low;
unsigned int high;
}64bits;
and define the nums
64bits num1.num2;
Are you working on a C89 compiler which
means you don't have unsigned long long
available ?
1.I know that the limit of unsigned integer is ~64000 what happend in
case of overflow it start again from 0.?
UINT_MAX is guaranteed to be at least 65535. It could
be a lot more , in fact the standard doesn't specify an
upper bound. And yes it will wrap around upon reaching
UINT_MAX + 1
2.could someone add solution for the question?
Is it homework ?
sarahh writes:
1.I know that the limit of unsigned integer is ~64000
....on a host where unsigned int is 32-bit like on your question, yes....
what happend in case of overflow it start again from 0.?
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U == 0.
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
2.could someone add solution for the question?
The point of homework is to learn by doing. But if you need a hint:
Think of how you do addition of two-digit numbers by hand, and think
of low and high as the two digits of a number. (In base 0x100000000
instead of base 10, but what the hey...) You need to check somehow
whether there was carry from adding the "low" digits.
--
Hallvard
On 13 מאי, 20:45, Hallvard B Furuseth <h.b.furus...@u sit.uio.no>
wrote:
sarahh writes:
1.I know that the limit of unsigned integer is ~64000
...on a host where unsigned int is 32-bit like on your question, yes....
what happend in case of overflow it start again from 0.?
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U == 0..
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
2.could someone add solution for the question?
The point of homework is to learn by doing. But if you need a hint:
Think of how you do addition of two-digit numbers by hand, and think
of low and high as the two digits of a number. (In base 0x100000000
instead of base 10, but what the hey...) You need to check somehow
whether there was carry from adding the "low" digits.
--
Hallvard
Thanks,know I understand it better what about not unsigned, what
happend if I add to max+1 ?
It is not homework it is a question from interview I did .*****
just to be sure about unsigned int
I know that in base 10 it is
~65,000
when I add it 1 ,I return to 0.
I don't understand it because I write it in c and I get number bigger
than the limit.
what I lost?
On 13 May, 19:17, sarahh <cohen...@gmail .comwrote:
On 13 מאי, 20:45, Hallvard B Furuseth <h.b.furus...@u sit.uio.no>
wrote:
sarahh writes:
1.I know that the limit of unsigned integer is ~64000
...on a host where unsigned int is 32-bit like on your question, yes....
what happend in case of overflow it start again from 0.?
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U ==0.
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
2.could someone add solution for the question?
The point of homework is to learn by doing. But if you need a hint:
Think of how you do addition of two-digit numbers by hand, and think
of low and high as the two digits of a number. (In base 0x100000000
instead of base 10, but what the hey...) You need to check somehow
whether there was carry from adding the "low" digits.
Thanks,know I understand it better what about not unsigned, what
happend if I add to max+1 ?
Undefined behaviour. Hallvard Furuseth has said
so already. Undefined behaviour has as an implication
that you cannot predict the outcome so you must
not allow undefined behaviour to appear in your code.
In other words, when you are dealing with signed
integers you must either know that the values your
programme will be dealing with will be such that no
overflow will occur or you need to check *before*
performing the arithmetic operations whether they
will lead to overflow and if yes take appropriate measures
like emit an error message or something.
Example:
#include <limits.h>
/* ..... */
int a,b,c ;
/* ..... */
/* We want to perform c = a+b without risking
* undefined behaviour.
*/
if ( a >= 0 ) {
if ( b >= 0 ) {
if ( a <= INT_MAX - b ) {
c = a+b ;
} else {
/* OVERFLOW */
}
} else {
c = a+b ;
}
} else {
if ( b >= 0 ) {
c = a+b ;
} else {
if ( a >= INT_MIN - b ) {
c = a+b ;
} else {
/* OVERFLOW */
}
}
}
In article <88************ *************** *******@59g2000 hsb.googlegroup s.com>,
sarahh <co******@gmail .comwrote:
>just to be sure about unsigned int
I know that in base 10 it is ~65,000 when I add it 1 ,I return to 0. I don't understand it because I write it in c and I get number bigger than the limit. what I lost?
The maximum unsigned int is only 65535 on systems using 16 bit
integers. if the system you were using had 32 bit integers, then
you would have gotten different results than you expected,
depending exactly how you wrote the constants and exactly which
format you used to print them out.
--
"When we all think alike no one is thinking very much."
-- Walter Lippmann
Walter Roberson writes:
The maximum unsigned int is only 65535 on systems using 16 bit
integers. if the system you were using had 32 bit integers, (...)
duh, I was reading too quicly to notice that...
--
Hallvard
On May 13, 7:46 pm, Spiros Bousbouras <spi...@gmail.c omwrote:
On 13 May, 19:17, sarahh <cohen...@gmail .comwrote:
On 13 מאי, 20:45, Hallvard B Furuseth <h.b.furus...@u sit.uio.no>
wrote:
sarahh writes:
1.I know that the limit of unsigned integer is ~64000
...on a host where unsigned int is 32-bit like on your question, yes.....
what happend in case of overflow it start again from 0.?
Yes. With 32-bit unsigned int, 0xffffffffU (all bits = 1) + 1U == 0.
Unsigned arithmetic is defined as modulo-(max value + 1) arithmetic.
(Strictly speaking this is not what is called overflow in the C language
- precisely because it is well-defined. "overflow" is what happens to
signed integers, where the result is not defined.)
Thanks,know I understand it better what about not unsigned, what
happend if I add to max+1 ?
Undefined behaviour. Hallvard Furuseth has said
I think the OP was interested in unsigned numbers, where there is no
undefined behaviour (UB) nor implementation defined behaviour (IDB)
For signed numbers: overflow is UB.
But it is IDB whether or not the signed numbers are represented as
two's-compement. And it is well defined how to convert a signed number
to unsigned, and how to add them. Then it is IDB how to convert them
back
to a signed integer. So
int a,b;
(int)((unsigned )a+b) has IDB
but a+b has UB
on overflow.
If the OP's system represents negative numbers as two's complement, he
may
well stick to unsigned arithmetics, as it will just reproduce what he
wants,
even with signed numbers.
Szabolcs
On 14 May, 12:07, Eligiusz Narutowicz<elig iuszdotn...@hot mail.com>
wrote:
I often wonder about this nonsensical part of the standard.
If you mean the fact that exceeding the bounds in
signed arithmetic has undefined consequences then
I believe the answer is that different architectures
behave in a different manner. I have heard of the
following behaviours:
1) Wraparound.
2) If the overflow is towards the maximum value then
the result is the maximum value. Some DSPs exhibit
this behaviour.
3) Generation of a signal specific to the implementation.
There may be more behaviours. The following thread may
also be of interest http://tinyurl.com/4r3zum This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: aku |
last post by:
I'm looking for the absolute fastest way
to count the nr of bits that are set to "1"
in a string. Presumably I then first need the
fastest way to do this in a byte.
I think this is it, but welcome any improvements:
i = 0;
if (g && 1) i++;
if (g && 2) i++;
|
by: Zhiqiang Ye |
last post by:
Hi, All
I am reading FAQ of this group. I have a question about this:
http://www.eskimo.com/~scs/C-faq/q7.31.html
It says:
"
p = malloc(m * n);
memset(p, 0, m * n);
The zero fill is all-bits-zero, and does not therefore guarantee useful null
pointer values (see section 5 of this list) or floating-point zero values.
|
by: sathyashrayan |
last post by:
Group,
Following function will check weather a bit
is set in the given variouble x.
int bit_count(long x)
{
int n = 0;
/*
** The loop will execute once for each bit of x
set,
|
by: UJ |
last post by:
I'm using the MS wrapper for BITS (Microsoft.MSDN.Samples.BITS) and I don't
see any way to upload a file. Does anybody know of how to do that?
TIA - Jeff
|
by: krunalb |
last post by:
Hi,
I am trying to shift unsigned long long value by 64 bits and this is
what i get
#include <stdio.h>
int main()
{
unsigned short shiftby= 64;
| |
by: Mack |
last post by:
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
Thanks in advance,
-Mukesh
|
by: borophyll |
last post by:
As I read it, C99 states that a byte is an:
"addressable unit of data storage large enough to hold any member of
the basic character
set of the execution environment" (3.6)
and that a byte must be at least 8 bits:
"The values given below shall be replaced by constant expressions
suitable for use in #if
|
by: John Messenger |
last post by:
I notice that the C standard allows padding bits in both unsigned and
signed integer types. Does anyone know of any real-world examples of
compilers that use padding bits?
--
John
|
by: JoeC |
last post by:
I am working on a graphics program but my question has nothing to do
with graphics but trying to get an algorithm to work. I set graphics
from a 16x16 grid to bits of a graphic with:
bitData = binTemp * 128 + binTemp * 64 +
binTemp * 32 + binTemp * 16 +
binTemp * 8 + binTemp * 4 +
binTemp * 2 + binTemp;
But I want to populate...
|
by: spasmous |
last post by:
Just wondering.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |