|
-
Dec 20th, 2011, 12:55 PM
#1
[RESOLVED] Bit Shifting with C
I've come to understand C coding quite a bit over the past week. I am not a C programmer at all. Trying to convert/interpret some C code, I've come across the following simple calculations and am unsure what the results would be. I do not own a C compiler.
If anyone could provide the answers to the these 2 calculations and/or answer my following question, I would very much appreciate it.
All variables are defined as: int
Calc 1: x4 =-17728437 x5 =-3330316
x2 = (181 * (x4 + x5) + 128) >> 8
Calc 2: x4 = 33479030 x5 = 6609623
x2 = (181 * (x4 + x5) + 128) >> 8
Where I'm having problems with this is that calcs above, before shifting, will result in a value that needs more than 32 bits.
Is there some sort of variable type promotion?
or possibly truncation?
When this is the case, what part of the result, exactly, whether positive or negative, is shifted? just 32 bits, 32+ bits, something else?
Last edited by LaVolpe; Dec 20th, 2011 at 01:43 PM.
-
Dec 20th, 2011, 02:28 PM
#2
Re: Bit Shifting with C
When the value exceeds the size of the 32 bit integer, it wraps around.
To prove this, I did this:
0xFFFFFFFF + 1
Result = 0
0xFFFFFFFF + 10
Result = 9.
The result (32 bit integer) is shifted 8 bytes to the right.
-
Dec 20th, 2011, 03:01 PM
#3
Re: Bit Shifting with C
 Originally Posted by Atheist
When the value exceeds the size of the 32 bit integer, it wraps around...
The result (32 bit integer) is shifted 8 bytes to the right.
In your examples:
0xFFFFFFFF + 1 would be 0x100000000 (33 bits)
0xFFFFFFFF + 10 would be 0x100000009 (33 bits)
It appears that the right 32 bits (0xFFFFFFFF mask) are simply used and I expected that. In the calculations I provided in previous reply: the intermediary value, the stuff btwn the parentheses, is not directly assigned to a variable. And in this case, I don't know if C is shifting all bits or truncating it before shifting. Whether before or after, the result can be vastly different. If I had the actual result to the calculations, I could make that determination with confidence I think.
Edited: Hmmm. From what I've read on variable declaration, when declaring a variable as INT, it is signed by default unless explicitly declaring it as unsigned int. If that's the case, then wouldn't 0xFFFFFFFF be -1? And if so, your addition examples make perfect sense too. If I've misunderstood that basic concept, I'll have to go back & relook at every line of code I've interpreted
Last edited by LaVolpe; Dec 20th, 2011 at 03:24 PM.
-
Dec 20th, 2011, 04:02 PM
#4
Re: Bit Shifting with C
 Originally Posted by LaVolpe
In your examples:
0xFFFFFFFF + 1 would be 0x100000000 (33 bits)
0xFFFFFFFF + 10 would be 0x100000009 (33 bits)
Yes but not if we are dealing with 32-bit integers, which I assume is the case 
 Originally Posted by LaVolpe
It appears that the right 32 bits (0xFFFFFFFF mask) are simply used and I expected that. In the calculations I provided in previous reply: the intermediary value, the stuff btwn the parentheses, is not directly assigned to a variable. And in this case, I don't know if C is shifting all bits or truncating it before shifting. Whether before or after, the result can be vastly different. If I had the actual result to the calculations, I could make that determination with confidence I think.
Edited: Hmmm. From what I've read on variable declaration, when declaring a variable as INT, it is signed by default unless explicitly declaring it as unsigned int. If that's the case, then wouldn't 0xFFFFFFFF be -1? And if so, your addition examples make perfect sense too. If I've misunderstood that basic concept, I'll have to go back & relook at every line of code I've interpreted
msvc will treat integer constants as 32-bit values per default, unless explicitly defined otherwise by suppying a suffix.
Take this for instance:
Code:
unsigned long long result64 = 4294967295 + 4294967295;
The result will be 4294967295. Not quite what you'd expect if you thought you where dealing with 64-bit values.
The outcome is different here:
Code:
unsigned long long result64 = 4294967295LL + 4294967295LL;
The result is 8589934590.
Although different compilers and/or platforms would behave differently.
Yeah integers are per default signed.
The result to the calculations in your original post is 1888020 and -5210502 respectively!
-
Dec 21st, 2011, 09:51 AM
#5
Re: [RESOLVED] Bit Shifting with C
Though the thread was educational, my problem was not with bit shifting after all. I misunderstood the precedence of ++ operator when on the left or right side of a pointer. Bottom line, I was using the wrong value for the x4, x5 variables.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|