• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Manipulating Hex Integers

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I get a question like the one below, what is the best strategy to try and work out the answer? I don't know where to start when manipulating hex. Is it best to convert everything to binary first?


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

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand this either:

When I have the hex value 0xffffffff, and I try to convert it to an int like this:

int i = Integer.decode("0xffffffff").intValue();
System.out.println(i);


...I get a NumberFormatException.

Yet the compiler allows me to do this:
int i1 = 0xffffffff;

Why is this the case?
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is primarily testing your understanding of shift operations. It may help to convert to binary, especially when you're learning.

But since each of these shift operations only shifts one bit, and all 32 bits are filled or set, you don't need to convert the whole number to binary, just the nibble that is changed by the shift.

For example, 0xffffffff is
1111 1111 1111 1111 1111 1111 1111 1111 in binary.
f f f f f f f f

If you shift left one place, (0xffffffff << 1) the result is:
1111 1111 1111 1111 1111 1111 1111 1110
f f f f f f f e

Since the shift is only one position, the right-most bit is the only one that changes. Therefore the rightmost hex-digit (since it represents 4-bits or a nibble) is also the only one that changes in hex format. Once you see that, you can work out the shift operation just by examining the bits that change, converting from hex to binary if that helps.

Shifting right one position is just the same except the leftmost bits are the ones that change. And how they change depends on the shift operator >> or >>>.

So, if you right shift one position and maintain the sign bit 1111 (0xf) remains 1111.
But if you don't maintain the sign bit, 1111 becomes 0111 (0x7).



Arthur's problem is caused by Integer.decode(String number) which throws a NumberFormatException if the parameter represents a negative number.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arthur,

I am not so sure but I have heared that Bitwise and shift operators are not on the new Java 5 exam.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edisandro is correct!

You might get questions concerning bit shifting and bit twiddling on the 1.4 exam, but if you're studying for the tiger, 1.5 exam you won;t have to worry about this topic.

hth,

Bert
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic