Results 1 to 4 of 4
Could some one tell me what does this mean sum = (sum >> 16) + (sum & 0xffff) in c.If you could also giva a link on where to learn ...
- 03-07-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 10
sum = (sum >> 16) + (sum & 0xffff)
Could some one tell me what does this mean sum = (sum >> 16) + (sum & 0xffff) in c.If you could also giva a link on where to learn these operator it would be great.Thanks
- 03-07-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Is sum a 16 bit, 32 bit, or 64 bit value? What happens depends upon that plus the behavior of the processor in a shift-right operation.
The first operation, (sum >> 16) shifts all the bits to the right by 16 places. Most of the time, the bits at the right end of the value will "fall off" and be lost, so if this is a 16-bit value, it will probably zero out value. If it is a 32-bit integer (most common these days), then it is the same as an integer divide by 65536. Each shift right divides the number by 2 and 2^16 is 65536.
The second operation, (sum & 0xffff) masks off the high-order 16 bits of the value, leaving only the lowest 16-bits unchanged.
So, the resulting value of sum, assuming a 32-bit value and normal x86 type of processor instructions will be (sum/65535) plus the part of sum that is <= 65535. Which is to say, it is the value of the 16 low-order bits of sum plus the 16-bit value of the 16 high-order bits. So, another way to look at it is if sum is a 32-bit value, we can cast it as an array of 2 16 bit values and add them together. IE:
unsigned int sum = some_value;
unsigned short* sarray = (unsighed short*)∑
unsigned int results = sarray[0] + sarray[1];
sum = results;
By the way, if this is a school assignment, then my bad for helping you. Terms of use for these forums precludes helping with school work other than in a very general way, such as referring you to appropriate documentation where you can find out what you need to know.
Anyway, the short explanation of these operators:
>> right shift - shift bits right (each bit is same as divide by 2)
<< left shift - shift bits left (each bit is same as multiply by 2, unless top bit was set)
& and - results will only include bits set in both source and mask values.
| or - results will include bits set in either source or mask
^ xor - results will include bits set in only the source or mask
! not - results will flip bit values.
See this Wikipedia page: Bitwise operation - Wikipedia, the free encyclopediaSometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 03-08-2010 #3Just Joined!
- Join Date
- Mar 2010
- Posts
- 10
Thanks Rubberman that was hepful.How long you have been using linux anyways and which distribution do you use??
- 03-08-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I've been using Linux on and off for about 10 years, and full time for about 2 years. I run CentOS 5.4 on my primary development workstation/server and Ubuntu 9.04 on my 2 laptops.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote