Toasty
Sony battery
Am stuck on my laptop with Ubuntu until I get my desktop motherboard replaced, and since Linux isn't too great in the gaming department I figured I'd do a little programming. I noticed one of my functions was producing some incorrect results though and finally tracked down the problem to something that has thoroughly mystified me. Here's a simple example:
main.cpp
As expected, g++ was a little upset with me about the line where I shifted test down by 64 bits (warning: right shift count >= width of type), but it was only a warning and compiled fine. The warning of course makes sense, as that command (both commands actually, though the compiler didn't figure the first one out) would shift away all the bits in test (leaving 0 as the result, no matter what test's value is). At least, I thought that's what would happen. I figured I would receive two results printed, both of which being 0, but instead, I get:
So I'm completely confused. I thought shifting an n-bit integer right (or left, for that matter) by n bits would always result in zero. Am I missing something?
main.cpp
Code:
#include <stdio.h>
#define NUM 1
int main(int argc, char** argv) {
unsigned long long test = NUM;
//Let's shift right by 64 bits - in two steps
unsigned long long r1 = (test >> 63) >> 1;
//Let's shift right by 64 bits - in one step
unsigned long long r2 = (test >> 64); //A compiler warning here
//Print out r1 and r2 on separate lines, formatted as hex
//These *should* be identical, right?
printf("%llX\n%llX\n", r1, r2);
return 0;
}
Code:
0
1