# True / False

ankur rathi

Ranch Hand

Posts: 3830

posted 11 years ago

How many r true :

1 ] An unsigned right shift >>> by a smaller positive amount (< total of bits of type) of a small negative number will result in a large positive number.

2 ] A mod (%) 16 is performed on the shift operand which affects shifts of more than 16 places for integers.

3 ] The signed right shift operation >> results in a number with the same sign bit.

4 ] The operators <<< and >>> are called bit-wise operators.

5 ] The operators << and >> are called shift operators.

Answer 2 is incorrect, It is mod 32 that returns the same value for integers. A mod 16 is applied for short values that will return the same value, the modulus of the size in bits of the type (integer, short, ..) is applied.

Answer 3 is correct. It is the left hand value of the operator that will be moved by the amount of positions specified in the right hand side operator. The sign bit of the first operand is shifted and replaced with its original value (so the sign is maintained).

2 >> 100 : This mean that the binary represantation of 2 will be moved to the right (closer to null) with 100 positions. The result for this example is 0.

-2 >> 100 : The result is -1. The right shift operation will not change the sign.

Answer 4 is incorrect, the operator <<< does not exists.

Answer 5 is correct they are called right shift operator (>> and left shift operator (<< .

Hi all ,

Answer 4 & 5 is clear to me but not others .

Please explain other 3 .

Thanks ,

ankur

1 ] An unsigned right shift >>> by a smaller positive amount (< total of bits of type) of a small negative number will result in a large positive number.

2 ] A mod (%) 16 is performed on the shift operand which affects shifts of more than 16 places for integers.

3 ] The signed right shift operation >> results in a number with the same sign bit.

4 ] The operators <<< and >>> are called bit-wise operators.

5 ] The operators << and >> are called shift operators.

Answer 2 is incorrect, It is mod 32 that returns the same value for integers. A mod 16 is applied for short values that will return the same value, the modulus of the size in bits of the type (integer, short, ..) is applied.

Answer 3 is correct. It is the left hand value of the operator that will be moved by the amount of positions specified in the right hand side operator. The sign bit of the first operand is shifted and replaced with its original value (so the sign is maintained).

2 >> 100 : This mean that the binary represantation of 2 will be moved to the right (closer to null) with 100 positions. The result for this example is 0.

-2 >> 100 : The result is -1. The right shift operation will not change the sign.

Answer 4 is incorrect, the operator <<< does not exists.

Answer 5 is correct they are called right shift operator (>> and left shift operator (<< .

Hi all ,

Answer 4 & 5 is clear to me but not others .

Please explain other 3 .

Thanks ,

ankur

posted 11 years ago

Note that in integral primitives, the leftmost bit indicates the sign. Specifically, all negative values start with a "1" bit, and non-negative values start with a "0" bit.

The unsigned right shift operation fills the left bits with 0's,

In binary representation, a "small negative number" and a "large positive number" both contain a lot of 1's

So if we have a "small negative number" (1 followed by a lot of 1's) and perform an unsigned right shift on it, we might expect the result to be a "large positive number" (0 followed by a lot of 1's). But this isn't necessarily the case; because if we shift far enough, we'll replace most of those 1's with 0's, and end up with a "small" positive number. So yes, we'll change the sign of a negative number; but a "small" or "large" result really depends on the amount of the shift.

When shifting a 32-bit int,

When shifting a 64-bit long, only the last 6 bits of the right operand are used, so a long shift is always between 0 and 63.

In general, this has the same effect as performing a modulus operation on the right operand -- %32 for ints and %64 for longs.

Again, note that in integral primitives, the leftmost bit indicates the sign. Negative values start with "1" and non-negative values start with "0."

The signed right shift operation fills the left bits with whatever was originally in that first position.

Thus, if the original value is negative (starting with "1"), then it will remain negative when shifted because the left bits will be filled with 1's. On the other hand, it the original value is positive (starting with "0"), then it will remain positive when shifted because the left bits will be filled with 0's.

[ December 12, 2004: Message edited by: marc weber ]

**"An unsigned right shift >>> by a smaller positive amount (< total of bits of type) of a small negative number will result in a large positive number."**

Note that in integral primitives, the leftmost bit indicates the sign. Specifically, all negative values start with a "1" bit, and non-negative values start with a "0" bit.

The unsigned right shift operation fills the left bits with 0's,

*regardless*of what was originally in that first position. Therefore, an unsigned right shift (by some positive amount less than the total number of bits) will always result in a positive number.

In binary representation, a "small negative number" and a "large positive number" both contain a lot of 1's

*(generally speaking).*The difference is that the negative number

*starts*with "1" while the positive number starts with "0."

So if we have a "small negative number" (1 followed by a lot of 1's) and perform an unsigned right shift on it, we might expect the result to be a "large positive number" (0 followed by a lot of 1's). But this isn't necessarily the case; because if we shift far enough, we'll replace most of those 1's with 0's, and end up with a "small" positive number. So yes, we'll change the sign of a negative number; but a "small" or "large" result really depends on the amount of the shift.

**"A mod (%) 16 is performed on the shift operand which affects shifts of more than 16 places for integers."**

When shifting a 32-bit int,

*only the last 5 bits of the right operand are used.*Thus, an int shift is always between 0 and 31 inclusively (even if the right operand is negative).

When shifting a 64-bit long, only the last 6 bits of the right operand are used, so a long shift is always between 0 and 63.

In general, this has the same effect as performing a modulus operation on the right operand -- %32 for ints and %64 for longs.

**"The signed right shift operation >> results in a number with the same sign bit."**

Again, note that in integral primitives, the leftmost bit indicates the sign. Negative values start with "1" and non-negative values start with "0."

The signed right shift operation fills the left bits with whatever was originally in that first position.

Thus, if the original value is negative (starting with "1"), then it will remain negative when shifted because the left bits will be filled with 1's. On the other hand, it the original value is positive (starting with "0"), then it will remain positive when shifted because the left bits will be filled with 0's.

[ December 12, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

ankur rathi

Ranch Hand

Posts: 3830

posted 11 years ago

Thanks Marc ,

u r genius .

but i have 2 doubt in this ..

why small -ve no is 1 followed by a lots of 1 .

ex. int i = -3 => 100000...( 29 zeros )....000011 .

It should be like that .

the answer of this will be :

-3 >>> 1 => 0100000...(29 zeros )...0000001

The 3rd one is clear but plz explain 2nd one with example .

Thank u very much marc .

bye

Ankur

u r genius .

but i have 2 doubt in this ..

**" In binary representation, a "small negative number" and a "large positive number" both contain a lot of 1's (generally speaking). The difference is that the negative number starts with "1" while the positive number starts with "0."**

So if we have a "small negative number" (1 followed by a lot of 1's) .. "So if we have a "small negative number" (1 followed by a lot of 1's) .. "

why small -ve no is 1 followed by a lots of 1 .

ex. int i = -3 => 100000...( 29 zeros )....000011 .

It should be like that .

the answer of this will be :

-3 >>> 1 => 0100000...(29 zeros )...0000001

**Am I right ?**The 3rd one is clear but plz explain 2nd one with example .

Thank u very much marc .

bye

Ankur

Barry Gaunt

Ranch Hand

Posts: 7729

posted 11 years ago

"Am I right ?"

No.

-3 is 11111111 11111111 11111111 11111101 as a binary 32 bit

Hint: learn about "twos complement arithmetic".

No.

-3 is 11111111 11111111 11111111 11111101 as a binary 32 bit

**int**.Hint: learn about "twos complement arithmetic".

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch

Getting someone to think and try something out is much more useful than just telling them the answer.

ankur rathi

Ranch Hand

Posts: 3830

posted 11 years ago

OK

So -3 will be :

3 => 00000000 00000000 00000000 00000011

2s compliments is => see when comes the first 1 from the right side , till that point dont change any bit . after that point compliment(change) all the bit .Right .

like this : 11111111 11111111 11111111 11111101

Now it has became -3 .

-3 >>> 1

will be : 01111111 11111111 11111111 11111110

Am I right now ?

thanks .

So -3 will be :

3 => 00000000 00000000 00000000 00000011

2s compliments is => see when comes the first 1 from the right side , till that point dont change any bit . after that point compliment(change) all the bit .Right .

like this : 11111111 11111111 11111111 11111101

Now it has became -3 .

-3 >>> 1

will be : 01111111 11111111 11111111 11111110

Am I right now ?

thanks .

posted 11 years ago

Correct!

OUTPUT:

-3 in binary is 11111111111111111111111111111101

-3>>>1 is 1111111111111111111111111111110 = 2147483646

(Note that toBinaryString does not include leading zeros. Only 31 bits are shown in the binary representation of 217483646, so a leading zero is implied.)

For clarification on the second item, see this thread...

http://www.coderanch.com/t/246350/java-programmer-SCJP/certification/Shift-Operation-negative-operand

[ December 12, 2004: Message edited by: marc weber ]

OUTPUT:

-3 in binary is 11111111111111111111111111111101

-3>>>1 is 1111111111111111111111111111110 = 2147483646

(Note that toBinaryString does not include leading zeros. Only 31 bits are shown in the binary representation of 217483646, so a leading zero is implied.)

For clarification on the second item, see this thread...

http://www.coderanch.com/t/246350/java-programmer-SCJP/certification/Shift-Operation-negative-operand

[ December 12, 2004: Message edited by: marc weber ]

*~Joe Strummer*

sscce.org