• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, it's an exclusive-OR operation (or XOR). | is the bitwise inclusive OR.
 
Marshal
Posts: 77157
370
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic