• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Bitwise

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Soumya.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sweety do
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
sweety do
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi timmy,
i am not clear with one thing ::what does adding
0011
0101
0001
0111

gives??
what is the result..
sweety
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two's complement is negate plus one so... it should be (1111....0110).

How??
 
shiva rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.. interesting thread. thought you guys might wanna check this thread

Hope that helps
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Steven,
thank you..i am more clear now...
sweety
 
shiva rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm curious...

Are you guys all studying for the 1.4 exam?

Why not the 1.5?
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic