This week's book giveaway is in the Agile and other Processes forum.We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login Win a copy of The Mikado Method this week in the Agile and other Processes forum!

# Explaination for BITWISE OPERATIONS

Asleshkumar Thukaram
Greenhorn

Joined: Jan 25, 2001
Posts: 9
Hi,
I've coded a pgm like......
public class Mine{
public static void main(String argv[]){
System.out.println(-1);
System.out.println((-1 >> 1));
System.out.println((-1 >>> 1));
}
}
The o/p of the pgm is :
-1
-1
2147483647
Could you tell me how is that? I'm a bit confused. My understanding is like, as -1 is an integer (32 bits - 4 bytes) it is represented in binary like :
10000000000000000000000000000001
(the highest order bit is 1 as it is a -ve)
When -1 >> 1, all the bits are moved to the right once except the highest order bit which is preserved. So the result should be like :
10000000000000000000000000000000 which is NOT equal to -1.
When -1 >>> 1, all the bits are moved to the right once including the highest order bit. So the result should be like :
01000000000000000000000000000000 which is equal to 1073741824.
Please clarify me if my understanding is wrong.
Thanx,
Aslesh
Tony Alicea
Sheriff

Joined: Jan 30, 2000
Posts: 3219
Nope!
-1 (int) in binary is all ones: 0xFFFFFFFF (or 32 ones). That's because Java, like most hardware platforms, uses TWO's complement to represent negative numbers.
When you arithmetically shift a value with all ones (-1) to the right you get all ones again. It is called a "signed" shift. That's because the sign bit is propagated to the right as requested.
When you shift unsigned (AKA "logical" shift; >>> ) to the right a value of all ones (i.e., -1), you get the most significant bits FILLED/cleared to zero as the unsigned shift that it is.
In your example this leaves you with all 31 least significant (rightmost) bits=one with the most significant bit=0.
And this translates into the highest positive Java <CODE>int</CODE>:
---
public static final int MAX_VALUE
The largest value of type int. The constant value of this field is 2147483647.
---
Sun doc.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
0xFFFFFFFF
how do i know what number is this
could anyone explain how to represent numbers as above
thx

sona<br />SCJP
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033

10
1 in binary = 1 in hexadecimal
10 in binary = 2 in hexadecimal
11 in binary = 3 in hexadecimal
100 in binary = 4 in hexadecimal
.
.
.
1010 in binary = A in hexadecimal = 10 in decimal
.
.
.
1111 in binary = F in hexadecimal = 15 in decimal

Therefore, 0xFFFFFFFF (hexadecimal) = 1111 1111 1111 1111 1111 1111 1111 1111 (binary) = -1 (decimal)

Like Tony said, "-1 (int) in binary is all ones: 0xFFFFFFFF (or 32 ones)".

[This message has been edited by Marilyn deQueiroz (edited February 24, 2001).]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week

subject: Explaination for BITWISE OPERATIONS