• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

using shift operators

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am confused when using shift operators(<<,>>,>>> on hexadecimal values. Can anyone tell me how to find the easy way to deal with them.

For example consider the following code:

class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}

What is the result of attempting to compile and run the program?

a. Prints: ffffffff,ffffffff,ffffffff
b. Prints: ffffffff,ffffffff,7fffffff
c. Prints: ffffffff,7fffffff,ffffffff
d. Prints: ffffffff,7ffffffe,7ffffffe
e. Prints: fffffffe,ffffffff,ffffffff
f. Prints: fffffffe,ffffffff,7fffffff
g. Prints: fffffffe,7fffffff,ffffffff
h. Prints: fffffffe,7fffffff,7fffffff
i. Run-time error
j. Compile-time error
k. None of the above

The answer given is f.
Thanks in advance
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The shift operators (also known as bitshift operators) work on bits. So I think you're usually best off converting to binary to perform these operations, because this is the level on which the shift is really taking place.

Notice that each hexadecimal digit represents 4 bits (since 2 raised to the 4th power is 16). So i1=0xffffffff in binary is 1111 1111 1111 1111 1111 1111 1111 1111.

So...

i2 = i1 << 1 is 1111 1111 1111 1111 1111 1111 1111 1110, which in hexadecimal is 0xfffffffe.

i3 = i1 >> 1 is 1111 1111 1111 1111 1111 1111 1111 1111, which is still 0xffffffff (since the signed right shift fills in with whatever the most significant bit is).

i4 = i1 >>> 1 is 0111 1111 1111 1111 1111 1111 1111 1111, which is 0x7fffffff (since this is the unsigned right shift, which fills in with zeros).
 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc, now I understand very well.

Thanks for your reply.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic