Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
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
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# True / False

Ranch Hand
Posts: 3852
• • Number of slices to send:
Optional 'thank-you' note:
• • 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 .

Thanks ,
ankur

Sheriff Posts: 11343   • • Number of slices to send:
Optional 'thank-you' note:
• • "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 ]

ankur rathi
Ranch Hand
Posts: 3852
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanks Marc ,
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) .. "

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

Ranch Hand
Posts: 7729
• • Number of slices to send:
Optional 'thank-you' note:
• • "Am I right ?"

No.

-3 is 11111111 11111111 11111111 11111101 as a binary 32 bit int.
Hint: learn about "twos complement arithmetic".

ankur rathi
Ranch Hand
Posts: 3852
• • Number of slices to send:
Optional 'thank-you' note:
• • 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 .

marc weber
Sheriff Posts: 11343   • • Number of slices to send:
Optional 'thank-you' note:
• • 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...

https://coderanch.com/t/246350/java-programmer-SCJP/certification/Shift-Operation-negative-operand
[ December 12, 2004: Message edited by: marc weber ] Don't get me started about those stupid light bulbs.