views:

165

answers:

3

Hi

Continuing my previous question http://stackoverflow.com/questions/1782927/why-i-cannot-derive-from-long

I found an interesting problem.

Step one:

4294967296 & 0xFFFFFFFF00000000

Result: 4294967296.

Step two.

4294967296 & 0x00000000FFFFFFFF

Result: 0

Aha, So here I assume that 4294967296 == 0xFFFFFFFF

Let's check

(long)0x00000000FFFFFFFF

Result: 4294967295. Fail.

Let's double check

4294967296 >> 32

Result: 1. Fail.

The only explanation is that because i am using long where some bit is reserved for sign. In C I would use unsigned long. What do you think guys?

+3  A: 
4294967296 & 0xFFFFFFFF00000000 = 4294967296

This indicates that the value 4294967296 has no bits set in the lower 32-bits. In fact, 4294967296 is 0x100000000, so this is true.

4294967296 >> 32 = 1

Again, consistent.

In other words, your conclusion that 4294967296 is 0xFFFFFFFF is wrong so the remaining checks will not support this.

Tom
Shame on me. Thanks. But I came to this conclusion debugging the following code which does not work long a = 2; long b = 3; long packed = a + b << 32; // now let's unpack long a1 = packed long b1 = packed >> 32;Result:a1 = 0;b1 = 5;Where is an error? Must be some really simple mistake.
Captain Comic
Sorry formatting doesn't work in comments
Captain Comic
Your problem is operator precidence. Plus is processed before bit shifts. Try "long packed = a + (b << 32);"
Tim Schneider
precidence = precedence, silly typo. Figured I'd correct cause you may want to google for it ;)
Tim Schneider
Worked. The precedence has you!!! Shame on me (again)
Captain Comic
+1  A: 

I think you are failing to understand the bitwise and operation. The bitwise and will return the bits that are set in both. If the two were the same then

(4294967296 & 0xFFFFFFFF00000000) == 4294967296

and

(4294967296 & 0xFFFFFFFF00000000) == 0xFFFFFFFF00000000

would both hold, but they obviously don't.

tvanfosson
+3  A: 

Um... I'm not sure why you came to the conclusions you did but 4294967296 is 0x100000000. To write out the bitwise AND's in easily readable hex...

0x0000000100000000 &
0x00000000FFFFFFFF =
0x0000000000000000

0x0000000100000000 &
0xFFFFFFFF00000000 =
0x0000000100000000

Both of those make perfect sense. Perhaps you're misunderstanding a bitwise AND... it maps the bits that are the same in both. Your comments seem more appropriate to a bitwise XOR than a bitwise AND (Which is not the operation you're using)...

Tim Schneider