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

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

posted

0

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: 812

posted

0

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)

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).

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

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 ]

(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.

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

posted

0

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..

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: 812

posted

0

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

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

posted

0

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

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

posted

0

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

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.