File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes bitwise & operation on char variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bitwise & operation on char variable" Watch "bitwise & operation on char variable" New topic
Author

bitwise & operation on char variable

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 05, 2002
Posts: 787
correction
Replace $ with & in above code. Sorry...
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Jan 30, 2000
Posts: 18671
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).


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bitwise & operation on char variable