# Bitwise

shiva rao

Ranch Hand

Posts: 31

posted 11 years ago

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

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

Posts: 300

sweety do

Greenhorn

Posts: 14

posted 11 years ago

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)

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

Posts: 813

posted 11 years ago

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

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

Posts: 14

amit taneja

Ranch Hand

Posts: 813

posted 11 years ago

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)

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)

Thanks and Regards, Amit Taneja

posted 11 years ago

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

Henry

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

Srinivasa Raghavan

Ranch Hand

Posts: 1228

posted 11 years ago

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 ]

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

posted 11 years ago

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

(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

Posts: 24

posted 11 years ago

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,

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

Posts: 14

posted 11 years ago

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

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

Posts: 226

amit taneja

Ranch Hand

Posts: 813

posted 11 years ago

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 ?

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 ?

Thanks and Regards, Amit Taneja

sweety do

Greenhorn

Posts: 14

Shubhada Nandarshi

Ranch Hand

Posts: 59

shiva rao

Ranch Hand

Posts: 31

posted 11 years ago

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 ]

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

Posts: 1071

Steven Bell

Ranch Hand

Posts: 1071

shiva rao

Ranch Hand

Posts: 31

Bert Bates

author

Sheriff

Sheriff

Posts: 8900

5

posted 11 years ago

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

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

Posts: 411

Shubhada Nandarshi

Ranch Hand

Posts: 59

posted 11 years ago

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?

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?

Shubhada

sweety do

Greenhorn

Posts: 14

shiva rao

Ranch Hand

Posts: 31

posted 11 years ago

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.

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.