| Author |
System.out.println(0x47 ^ 0xA3); What does it do?
|
Brian Kenney
Greenhorn
Joined: Mar 22, 2012
Posts: 6
|
|
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
Joined: Oct 02, 2003
Posts: 9946
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3791
|
|
|
Just to clarify, it's an exclusive-OR operation (or XOR). | is the bitwise inclusive OR.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
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.
|
 |
 |
|
|
subject: System.out.println(0x47 ^ 0xA3); What does it do?
|
|
|