posted 17 years ago
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
Select all correct answers
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
Select all correct answers
posted 17 years ago
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.
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.
posted 17 years ago
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
21280

640

320

160

80

40

20
_
11
_
0
so the answer is 100000000
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
21280

640

320

160

80

40

20
_
11
_
0
so the answer is 100000000
shabbir zakir
Ranch Hand
Posts: 183
posted 17 years ago
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
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
posted 17 years ago
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.
(someone please correct if I have made a mistake somewhere.)
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.
(someone please correct if I have made a mistake somewhere.)
Matthew Jones
Ranch Hand
Posts: 68
posted 17 years ago
PS. actually, I tested, and >> and >>> did exactly the same think, so the answer would be A and B.
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.
(someone please correct if I have made a mistake somewhere.)
PS. actually, I tested, and >> and >>> did exactly the same think, so the answer would be A and B.
posted 17 years ago
The correct answers are A and B, because for positive values, the shiftrightwithzerofill and shiftrightwithsignfill operators are equivalent. That is, for positive numbers, >>> and >> are the same.
shabbir zakir
Ranch Hand
Posts: 183
posted 17 years ago
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.
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
posted 17 years ago
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
in the right hand side we get 0010. but that is not the right answer. please help.
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
in the right hand side we get 0010. but that is not the right answer. please help.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? 