• 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

Please answer,with explanation on how to perform bitwise

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,please help answer this question with explanation,i.e how to do shift.
What is the output of the following code?
1: int i = 16;
2: int j = 17;
3:
4: System.out.println("i >> 1 = " + (i >> 1));
5: System.out.println("j >> 1 = " + (j >> 1));
A) Prints "i >> 1 = 8"
"j >> 1 = 8"

B) Prints "i >> 1 = 7"
"j >> 1 = 7"
C) Prints "i >> 1 = 8"
"j >> 1 = 9"
D) Prints "i >> 1 = 7"
"j >> 1 = 8"
what is the answer? with explanation
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ans:
in both cash it prints 8

1.
int i = 16
i >> 1
step 1 (Binary representation)
int i = 16 = 0000 0000 0000 0000 0000 0000 0001 0000
step 2(shifting)
i >> 1 = 0000 0000 0000 0000 0000 0000 0000 1000
(shift one bit right)
step 3 (fill the bit either 0 or 1)
fill the left bit by 0 or 1 becoz >>
0000 0000 0000 0000 0000 0000 0000 1000
(Here it's fill by all 0s(zeros) becoz >> fills o if precede bit is o and 1 if preceded bit is 1 )
2.
int i = 17
i >> 1
same as above
Remember >> fills either 0 or i depending on preceded bit
>>>always fills 0 (zero)

Hope this will helps
Avi

int j = 17;
3:
4: System.out.println("i >> 1 = " + (i >> 1));
5: System.out.println("j >> 1 = " + (j >> 1));
A) Prints "i >> 1 = 8"
"j >> 1 = 8"

B) Prints "i >> 1 = 7"
"j >> 1 = 7"
C) Prints "i >> 1 = 8"
"j >> 1 = 9"
D) Prints "i >> 1 = 7"
"j >> 1 = 8"
what is the answer? with explanation
[/B]
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a helpfull link.
http://javaranch.com/campfire/StoryBits.jsp
 
reply
    Bookmark Topic Watch Topic
  • New Topic