Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Shift Operators

 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question on a mock exam is as follows:
Given the byte with a value of 01110111, which of the following statements will produce 00111011?
A) 0x77 << 1;
B) 0x77 >>> 1;
C) 0x77 >> 1;
D) None of the above
The answer is 'B' and 'C'. I understand the concept of shifting bits but isn't the byte with a value of 01110111 the equivalent of 128 and isn't Ox77 the equivalent of 119. I'm missing the point here and would appreciate it if someone could shed some light on this.
Thanks very much.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Scott:
but isn't the byte with a value of 01110111 the equivalent of 128 and isn't Ox77 the equivalent of 119

Just 1 small error - the 4th bit from the right in the binary number is not set, so it is 127 - 8 = 119, or calculating it the long way:
64 + 32 + 16 + 4 + 2 + 1 = 119
[ January 30, 2003: Message edited by: John Paverd ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how high do we need to be able to count in octal, hex or binary for the exam? i wonder if this question would appear in the actual exam?
[ January 30, 2003: Message edited by: Jasper Vader ]
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't realy understand how it is happening.
isn't Ox77 is an octal number which is equavelent to 63 in decimal base. So isn't the question should be calculated as
63 >> 1
63 >>>1
63 << 1.
If yes then the answer should be something else.
Plz its very urgent.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naveed Ali:
I don't realy understand how it is happening.
isn't Ox77 is an octal number [ ... ]

Nope, 0x77 is the hexadecimal representation of the decimal number 119. The leading '0x' prefix denotes a hexadecimal notation; without the 'x' (just a leading zero) it would have been octal notation (077) of the decimal number 63.
kind regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic