473,545 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sum of 64 bits using 32 bits cpu

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?
Jun 27 '08 #1
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 ?
Jun 27 '08 #2
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
Jun 27 '08 #3
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 ?
Jun 27 '08 #4
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?
Jun 27 '08 #5
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 */
}
}
}
Jun 27 '08 #6
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
Jun 27 '08 #7
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
Jun 27 '08 #8
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

Jun 27 '08 #9
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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
5664
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++;
53
8114
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.
7
4791
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,
2
5203
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
10
10668
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;
11
14640
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
77
4207
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
6
2817
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
11
1845
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...
11
2330
by: spasmous | last post by:
Just wondering.
0
7660
Oralloy
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. ...
0
7813
jinu1996
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...
1
7431
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...
0
7761
tracyyun
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...
0
5976
agi2029
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...
0
4949
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...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1888
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
0
709
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.