Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.println(0x47 ^ 0xA3); What does it do?  RSS feed

 
Brian Kenney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(0x47 ^ 0xA3);

What does this do? My initial reaction was exponent, but that can't be it. I compiled and ran...answer: 228

Does it convert to decimal from hex?

Thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it? Wouldn't that be the easiest way to find out what it does?

edit: Ah...Your subject is misleading.

The caret ^ is a bitwise OR operation.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify, it's an exclusive-OR operation (or XOR). | is the bitwise inclusive OR.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find it in the Java Language Specification. That might not be easy to read, so let’s try without. Those numbers are ints, which occupy 32 bits (4 bytes) each.
0x47 should be 0x00000047, which in binary is 0000_0000_0000_0000_0000_0000_0100_0111
0xa3 should be 0x000000a3, which in binary is 0000_0000_0000_0000_0000_0000_1010_0011
The exclusive or operator checks whether the two digits in the same location are the same (1^1 or 0^0), or different (1^0 or 0^1). It returns 1 for different and 0 for same. Putting those numbers together, we get

0000_0000_0000_0000_0000_0000_0100_0111
0000_0000_0000_0000_0000_0000_1010_0011
0000_0000_0000_0000_0000_0000_1110_0100

Voilà! 228 when it is converted to decimal.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!