Win a copy of Microservices in Action this week in the Web Services 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

# short-circuit operators

Ranch Hand
Posts: 183
hi!
how to find out bits in an integer.for instance how to calculate this example.
Q. 12
Which of the following are correct?
A.128 >> 1 gives 64
B.128 >>> 1 gives 64
C.128 >> 1 gives �64
D.128 >>> 1 gives �64

Greenhorn
Posts: 16
first you need to convert int (decimals) to binary
the way to do it is to add up the binary positions to make
up the interger you want
for example
if we have 128, and we want to know its binary equivalent
1000 0000 is the binary equivalent
each position in the binary representation stands for a decimal
value, so starting from the right and going to the left
128 64 32 16 8 4 2 1
you notice that we have 8 numbers, corresponding to the 8 binary
positions.
so we find that 128 is actually 1000 0000
if we want for example 4 then it will be 0000 0100
if we want 10 , it will be 0000 1010
as so on.

Ranch Hand
Posts: 81
u devide the number by 2 and keep the remainder on the right side
and 128/2 = 64 and remainder 0
now again devide 64 by 2 and put teh remainder on the right as done below .. keep on deviding till u get 0 in the last
now arrange the digits from bottomto top and u get the answer
2|128|0
--
64|0
--
32|0
--
16|0
--
8|0
-
4|0
-
2|0
_
1|1
_
0

shabbir zakir
Ranch Hand
Posts: 183
thanks for the explanation.
i don't understand one thing .
rose says that the binary equilant of 4 is
0000 0100
but you divide 4 by 2 you get
and keep the remainder right hand side we get 0010

Ranch Hand
Posts: 68
Maybe I can help.
the >> and >>> symbols are bitwise shift to the right
you take the number, in this case 128, and convert to binary, so:
1000 0000
Then you move all the digits to the right either 1 >> or 2 >>> spaces.
Anything that "falls off" the end you ignore, and you put zeros in the place of anything that "comes on" to the byte, so:
1000 0000 >> 0100 0000
1000 0000 >>> 0010 0000
Then you just convert back.
so, 128 >> 64 and 128 >>> 32.
In this case, the answer would be A.
Hope this helps.

Matthew Jones
Ranch Hand
Posts: 68

Originally posted by Matthew Jones:
Maybe I can help.
the >> and >>> symbols are bitwise shift to the right
you take the number, in this case 128, and convert to binary, so:
1000 0000
Then you move all the digits to the right either 1 >> or 2 >>> spaces.
Anything that "falls off" the end you ignore, and you put zeros in the place of anything that "comes on" to the byte, so:
1000 0000 >> 0100 0000
1000 0000 >>> 0010 0000
Then you just convert back.
so, 128 >> 64 and 128 >>> 32.
In this case, the answer would be A.
Hope this helps.

PS. actually, I tested, and >> and >>> did exactly the same think, so the answer would be A and B.

Ranch Hand
Posts: 625
The correct answers are A and B, because for positive values, the shift-right-with-zero-fill and shift-right-with-sign-fill operators are equivalent. That is, for positive numbers, >>> and >> are the same.

shabbir zakir
Ranch Hand
Posts: 183
thanks for the explanation
but i don't understand one thing .rose says that the equaliant value of 4 in binary is 0000 0100.
but we divide 4 by 2 and put the remainder in the right hand side we get 0010.

shabbir zakir
Ranch Hand
Posts: 183
thanks for that.
but my question is how do you convert a int compailant number in java in binary. for example if we divide 4 by 2 put the remainder