• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

short-circuit operators

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
so the answer is 100000000
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Matthew Jones
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic