• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bitwise & operation on char variable

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char c = 'C', d = 'D';
System.out.println(c $ d)
The above code prints 64. How did it derive that number?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correction
Replace $ with & in above code. Sorry...
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat,
Well the int representation for the chars 'c' and 'd' are 67 and 68 respectively. So in binary that would be:

Bitwise anding gives us:

which is 64.
Hope this clears it up,
Michael Morris
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat,
Note that you have the ability to edit your posts at JavaRanch. Just click on the icon that looks like a piece of paper with a pencil.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a nice introduction to bitwise operations, take a look at The Cat and Mouse Games with Bits Story of The Campfire Stories.
Good Luck.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also - c and d were chars, but c & d is promoted to int by the rules of binary promotion. That's why it prints as the number 64 rather than the char '@' (which has value 64).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic