File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise" Watch "Bitwise" New topic
Author

Bitwise

shiva rao
Ranch Hand

Joined: May 26, 2005
Posts: 31
public class Test {

public Test () {
System.out.println("Inside the contructor");
return ;
}

public String Test () {
System.out.println("Inside the Method");
return "Welcome";
}

public static void main(String[] args) throws Exception {
int i=-10;
i=i>>2;
System.out.println("-10>>2 = "+i);
}
}

Output of this is -10>>2 = -3
Intially I thought it was -10>>2 = -2. Can somebody explain this in detail.

Thanks
shiva rao
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Hi,

this topic has been discussed many times, please use the search facility on top, you will get about 200 hits easily !

Soumya.
sweety do
Greenhorn

Joined: Jun 08, 2005
Posts: 14
Hi Shiva,
i = -10 is a negative number..so take 2's complement of the number..
that is 2's complement of all the 32 bits of 10 (0000....1010)...the 2'complement is (1111....0101)
Now right shift it 2 times..Make a note that this is signed right shift..
so the value will be(1111....1101),now to get the exact value of this negative number..do 2'complement again..
so you are left with (0000...0011)..since it is a negative number add a -before 3..
Hope it helps,
sweety
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
i = -10 is a negative number..so take 2's complement of the number..
that is 2's complement of all the 32 bits of 10 (0000....1010)...the 2'complement is (1111....0101)

How ?

when i m doing 2 complement ( i.e reversing bits and adding 1)
i m getting 1111....1110
by reversing bits of 10 (0000....1010) ->11111...0101 adding 1 ->1111..1110

? confused ...help pls


Thanks and Regards, Amit Taneja
sweety do
Greenhorn

Joined: Jun 08, 2005
Posts: 14
hey amit,
here adding 1 means using OR operation..hence OR operation for 0101 +1
is

0101
+ 0001
_____________________
0101

1+1 does not give a carry 1 into next bit
hope this helps,
sweety
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
Originally posted by sweety do:
hey amit,
here adding 1 means using OR operation..hence OR operation for 0101 +1
is

0101
+ 0001
_____________________
0101

1+1 does not give a carry 1 into next bit
hope this helps,
sweety



is it really true..? but in course i have done adding like
1+1=10 and 1 is shifted to next bit...
pls give me link to make sure of this..

really confused


so give final clarification...( i m genuinely confused)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

i = -10 is a negative number..so take 2's complement of the number..
that is 2's complement of all the 32 bits of 10 (0000....1010)...the 2'complement is (1111....0101)

How ?


The two's complement is negate plus one so... it should be (1111....0110).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
thanx henry


u removed my confusion

all clear.... sweety ..pls look at complement and see how it works so u will not do mistake while calculating...
in above post u confused me..u didn't add 1

any way.. take care
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Decimal -10 expressed in 32 bit binary is as follows
0000 0000 0000 0000 0000 0000 0000 1010
1's compliment of above number
1111 1111 1111 1111 1111 1111 1111 0101
Add 1 to get 2's compliment
1111 1111 1111 1111 1111 1111 1111 0101
1
=========================================
1111 1111 1111 1111 1111 1111 1111 0110
=========================================
(1)111 1111 1111 1111 1111 1111 1111 0110
Now we need to convert 111 1111 1111 1111 1111 1111 1111 0110
to decimal right ? so how will we get 3 after shifting.
[ June 10, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

(1)111 1111 1111 1111 1111 1111 1111 0110
Now we need to convert 111 1111 1111 1111 1111 1111 1111 0110
to decimal right ? so how will we get 3 after shifting.


The original question stated that if you shift right by 2, you will get -3 ... well, let's see...

Shift right by two, and you'll get:

1111 1111 1111 1111 1111 1111 1111 1101

Believe it or not, this is -3. There are two ways to confirm it. First, if you add 3, it should roll over and you'll end up with zero. The other way is to take a two's complement, and you'll get +3 as the result.

Henry
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
Computation of expression n>>s

For nonnegative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s,

and for negative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s and -1,

for example -10>>2 = -3 is compuatated as [ (-10 / (2 * 2) ) - 1 ] = -3
and 10>>2 = 2 is computated as [ 10 / (2 * 2) ] = 2

Regards,


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
sweety do
Greenhorn

Joined: Jun 08, 2005
Posts: 14
hi amit,
In java there are four bitwise operators.They are
Bitwise NOT,
Bitwise AND,
Bitwise OR,
Bitwise XOR.

The operations u do on bitwise operands must fall in one of the above 4 ..I donot know how adding two 1's can give u a carry on to next bit..because there is no operations that says so..it is not arithematic operation..

Clear this ambuguity..

sweety
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
actually, the operation is called addition. Addition works with bits exactly as it does in base 10, but you carry when the sum is 2 or greater instead of 10 or greater!
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
hi sweety..
hope u good...

add two bits like 1+1 will give you 2 which is 10 in binary so 1 is carried to next bit as in base 10

have u read my pvt msg yet i send to u sweety ?
sweety do
Greenhorn

Joined: Jun 08, 2005
Posts: 14
hi timmy,
i am not clear with one thing ::what does adding
0011
0101
0001
0111

gives??
what is the result..
sweety
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
The two's complement is negate plus one so... it should be (1111....0110).

How??


Shubhada
shiva rao
Ranch Hand

Joined: May 26, 2005
Posts: 31
Hi All,

Correct me if I am wrong.
Decimal -10 expressed in 32 bit binary is as follows
0000 0000 0000 0000 0000 0000 0000 1010
1's compliment of above number
1111 1111 1111 1111 1111 1111 1111 0101
Add 1 to get 2's compliment
1111 1111 1111 1111 1111 1111 1111 0101
1
=========================================
1111 1111 1111 1111 1111 1111 1111 0110
=========================================
Not shift right by two points, we will get.
111111 1111 1111 1111 1111 1111 1111 01
Now if I repeat the above process as 1's compliment and adding 1 (which is 2' compliment again).
which is like
0000 0000 0000 0000 0000 0000 0000 0010
adding 1 to it.
which result as
0000 0000 0000 0000 0000 0000 0000 0011
I am getting 3. But I still don't get -3

Thanks!
shiva rao
[ June 13, 2005: Message edited by: shiva rao ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by sweety do:

i am not clear with one thing ::what does adding
0011
0101
0001
0111


0011 + 0101 = 1000
1000 + 0001 = 1001
1001 + 0111 = 1 0000


Just to clarify the carry question

0001 + 0001 = 0010

The same as
0009 + 0001 = 0010
in decimal
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by shiva rao:
Hi All,
I am getting 3. But I still don't get -1

Thanks!
shiva rao


Why would you get -1?
shiva rao
Ranch Hand

Joined: May 26, 2005
Posts: 31
Originally posted by Steven Bell:


Why would you get -1?


Sorry its a typo error. I am getting 3. I am not getting -3

Thanks!!
Shiva rao
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Ah well,

Late to the party again! Just an FYI for those of you studying for the Tiger version of the SCJP...

There are NO bit-twiddling questions on the new exam!!! If you're studying for 1.5 - skip this whole thread and topic!!

Yeah!

For those of you studying for 1.4... carry on.

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Hmm.. interesting thread. thought you guys might wanna check this thread

Hope that helps


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
Thanks Jay,
U helpd me to remove my confusion.

hi shiva ,
see
the Q is -10>>2=?

Now 1st start with 10.
In binary 10 is represented as;
0000 0000.......0000 1010

Now do 2's complement

0000 0000.......0000 1010
+ ^1 (^ start inverting)
--------------------------------------
1111 1111 ......1111 0110 (-10 in decimal)

shift to right by 2.

this will give;
1111 1111.......1111 1101
(This is actually no. -3 in the binary form)

Once again do 2's complement ;

1111 1111.........1111 1101
+ ^1
-------------------------------------
0000 0000.........0000 0011 (3 in decimal)


means the ans is -3.

Am i right friends?
sweety do
Greenhorn

Joined: Jun 08, 2005
Posts: 14
hi Steven,
thank you..i am more clear now...
sweety
shiva rao
Ranch Hand

Joined: May 26, 2005
Posts: 31
Originally posted by shubh hadan:
Thanks Jay,
U helpd me to remove my confusion.

hi shiva ,
see
the Q is -10>>2=?

Now 1st start with 10.
In binary 10 is represented as;
0000 0000.......0000 1010

Now do 2's complement

0000 0000.......0000 1010
+ ^1 (^ start inverting)
--------------------------------------
1111 1111 ......1111 0110 (-10 in decimal)

shift to right by 2.

this will give;
1111 1111.......1111 1101
(This is actually no. -3 in the binary form)

Once again do 2's complement ;

1111 1111.........1111 1101
+ ^1
-------------------------------------
0000 0000.........0000 0011 (3 in decimal)


means the ans is -3.

Am i right friends?


Sounds good to me.
Only bothering thing is applying 2's complement second time to see the actual number. Otherwise its hard to identify the number after shift.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
So I'm curious...

Are you guys all studying for the 1.4 exam?

Why not the 1.5?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bitwise
 
Similar Threads
Garbage Collection for out of scope objects references
instance initializer
Interview Question
what does this mean here?
integer palindrome 'no strings'