my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Shift Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Shift Operators" Watch "Question on Shift Operators" New topic
Author

Question on Shift Operators

Michael Scott
Ranch Hand

Joined: Jan 20, 2003
Posts: 57
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

Joined: Nov 17, 2002
Posts: 115
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 ]

SCJP 1.4
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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 ]

giddee up
Naveed Ali
Ranch Hand

Joined: Aug 30, 2002
Posts: 41
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

Joined: Nov 22, 2008
Posts: 18944
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Shift Operators
 
Similar Threads
well a doubt !
problem base64 encoding the 3 hex bytes E2 80 A9
scjp (bit shifting)
JCP certification
for loop and byte to int coversion.