• 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

Binary problem, help!

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mock Exam Question:
After execution of the following code, what is the value of a?
byte b = 3; //binary 00000011
byte c = -3;
if (~b > c)
{
a = c;
}
else
{
a = b;
}
a. -3
b. 3
c. 0
d. none of the above.
b is correct. "~" is the bitwise inverter. Hence the binary representation of ~b is "11111100" which is -4, thus the "else" condition gets executed, and a gets the value of b which is 3.
My question is, why is 11111100 -4 instead of 252?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I follow the method for reading -ve nos. in binary like this.
When you see sign bit is 1 i.e number is -ve then count 0's of number and 1 in the answer and place -(minus) sign.

11111100
0 makes 1+2=3
add 1 in answer i.e 1+3=4
place - sign -4
Regards,
Hassan.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike
I just answered this in the Java in General forum here
Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic