• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Bit Shifting

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading Corey's journal article about bit shifting, and while typing in some code to see some shifts in the debugger, I ran into this:

// -32 = 11100000
byte A = -32;
A >>= 1;

byte B = -32;
B >>>= 1;

System.out.println("A -> " + A + " B -> " + B);

What would you expect to see here and why?

I expected B to be a positive number since the sign bit would be shifted to the right and a zero would be inserted into the sign bit.

This is the output of the above code:

'A -> -16 B -> -16'

Does this behavior have to do with the implicit casting of the arguements to the shift operators as ints?
[ August 30, 2004: Message edited by: Keith Tingle ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Tingle:
Does this behavior have to do with the implicit casting of the arguements to the shift operators as ints?



Yes. Can you figure out what's happening? Let me know if you need a hint, but you're on the right track.
 
Keith Tingle
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this code is the functional equivlant:

byte B = -32;

// implicitly widen
int C = B;

System.out.println("C = " + C + " (0x" + Integer.toHexString(C) + ")");

// bit shift as an int
C >>= 1;

// narrowing cast
B = (byte)C;

System.out.println("C = " + C + " (0x" + Integer.toHexString(C) + ")");
System.out.println("B = " + B);

Output:

C = -32 (0xffffffe0)
C = -16 (0xfffffff0)
B = -16

Replace C >>= 1 with C >>>= 1 and the output becomes:

C = -32 (0xffffffe0)
C = 2147483632 (0x7ffffff0)
B = -16

So the unsigned shift behaves as advertised but on an integer primitive not a byte, so the -16 result for B is a product of a narrowing cast where the three highest bytes are lopped off the resulting integer.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Tingle:
...where the three highest bytes are lopped off the resulting integer.



That's exactly right, Keith. There are a couple important things to take note of in your sample code. First of all, any operands of a bit shift operation are automatically promoted to ints. Therefore, if you have a byte with value -16, which has a binary equivalent of 11110000, it becomes an int with value -16, which has a binary equivalent of 11111111 11111111 11111111 11110000.

Second, keep in mind that compound assignment operators, such as +=, -=, and >>>=, all contain an implicit cast. Therefore, this code:



Is equivalent to this code:



So, what is really happening?

Well, first we promote our byte to an int and we end up with the value we saw before (11111111 11111111 11111111 11110000). Next, we shift that to the right one position and push a 0 into the highest order bit. That leaves us with 01111111 11111111 11111111 11111000. Finally, we perform a cast, which lops off the highest 3 bytes (which includes the 0 we just shifted in). That leaves us with b being assigned 11111000. That, of course, is -8.

I hope that helps,
Corey
 
Hot dog! An advertiser loves us THIS much:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic