Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Question on Shift Operators

Brandon Bay
Greenhorn

Joined: Aug 31, 2007
Posts: 29
Hi all, I'm a new kid on the block. I've actually borrowed the study guide from kathy sierra for SCJP 1.4 (i couldn't get 1.5, i couldnt hunt down the book in the country i'm living in), so that was how I knew abt this webbie.

Question 1
Question on Shift Operators.

Taken from kathy's book :

x gets a hexadecimal value in it.. so it looks like this

8 0 0 0 0 0 0 0 - in hexadecimal
1000 0000 0000 0000 0000 0000 0000 0000 - in binary

but it states : When the leftmost bit is 1, the number is negative; and when it is 0, the number is positive.

Is it referring to the "1" from the hexadecimal "8"? It does not make sense, I thought the "1" was supposed to be part of the value. x holds a value of "-2147483648". What if i want a positive value of "2147483648"? I would have to change the first digit to 0. That'd make 32 bits of zeroes and the value would be 0 and not 2147483648 when printed out.

Question 2
Question on Bitwise Complement operator.

Taken from kathy's book :

Result:

In bit representation, the conversion looks like this,
0000 0000 0000 0000 0000 0000 0000 0101 // this is binary for 5
so when i use the ~ operator, it becomes :
1111 1111 1111 1111 1111 1111 1111 1010 // is this -6 in binary ?

Hope i can get some feedback. I'm stuck with these mis-conceptions, without clarifying, i cannot move on to understand further.

Thank you!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Originally posted by Clemi Clemi:
Hi all, I'm a new kid on the block. I've actually borrowed the study guide from kathy sierra for SCJP 1.4 (i couldn't get 1.5, i couldnt hunt down the book in the country i'm living in), so that was how I knew abt this webbie.

Question 1
Question on Shift Operators.

Taken from kathy's book :

x gets a hexadecimal value in it.. so it looks like this

8 0 0 0 0 0 0 0 - in hexadecimal
1000 0000 0000 0000 0000 0000 0000 0000 - in binary

but it states : When the leftmost bit is 1, the number is negative; and when it is 0, the number is positive.

Is it referring to the "1" from the hexadecimal "8"? It does not make sense, I thought the "1" was supposed to be part of the value. x holds a value of "-2147483648". What if i want a positive value of "2147483648"? I would have to change the first digit to 0. That'd make 32 bits of zeroes and the value would be 0 and not 2147483648 when printed out.

In the original number, the 1 in the left-most position does make it a negative number. But when you shift the bits left by 1, that bit falls off and makes the number non-negative.

Question 2
Question on Bitwise Complement operator.

Taken from kathy's book :

Result:

In bit representation, the conversion looks like this,
0000 0000 0000 0000 0000 0000 0000 0101 // this is binary for 5
so when i use the ~ operator, it becomes :
1111 1111 1111 1111 1111 1111 1111 1010 // is this -6 in binary ?

Hope i can get some feedback. I'm stuck with these mis-conceptions, without clarifying, i cannot move on to understand further.

Thank you!

When you have a number with a 1 in the left-most bit position, the number is negative.

To figure out what the number is, flip all of the bits and add 1 to it.

If you flip the bits back, you get the bit representation of 5. When you add 1, you get 6.

So the number is -6.
[ September 02, 2007: Message edited by: Keith Lynn ]
Brandon Bay
Greenhorn

Joined: Aug 31, 2007
Posts: 29
okay, i get a hang of it

1111 1111 1111 1111 1111 1111 1111 1010 // -6 in binary

if so,

1111 1111 1111 1111 1111 1111 1111 0000 // i'll do this

1) If the left most bit is "1", meaning it's a negative, i'll have to flip all the bits
2) After flipping, i get 0000 0000 0000 0000 0000 0000 0000 1111
3) Which represents 1 + 2 + 4 + 8 = 15
4) Thereafter, I shall add 1 to it, resulting in 16
5) Then making it a negative number, -16 (correct answer?)

Another qn : If the left most bit is "0", meaning it's a Positive, i DO NOT have to flip all the bits, is that correct?

Thanks!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy "Clemi Clemi" !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!

Hope you'll enjoy.

Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp

It will not affect anything you've already posted here. Just your user name will update.

I'm posting this because I am one of the moderators of this forum.

Yours,
Bu.

all events occur in real time

I agree. Here's the link: http://aspose.com/file-tools

subject: Question on Shift Operators