• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hexadecimal calculation

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can anyone tell me what is the simplest way to calculate 0x67^0xBD,the result is 0xDA
simon
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon
The ^ operator is the 'exclusive or' that operates on the individual bits of the operands. In the example you gave
0x67^0xBD
The way to do it is to convert the hex into binary. I have to go through decimal to do it
0x67 is 103 in decimal and 0xBD is 189, in binary they are (remember that ints are 4 bytes):
00000000 00000000 00000000 01100111
00000000 00000000 00000000 10111101
the results of your 'exclusive or' are:
00000000 00000000 00000000 11011010
which equates to: 218 in decimal or 0xDA in hex.
Remember the ^ means that the result is one if one, but not the other, of the bits is a 1.
hope that helps
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Easiest way is to convert to binary.
0x67 = 0110 0111
^ 0xBD = 1011 1101
------------------
1101 1010 = 0xDA
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by simon kumar:
Hi,
can anyone tell me what is the simplest way to calculate 0x67^0xBD,the result is 0xDA
simon


Convert to binary!
Every hexadecimal number is equivalent to 4 bits. Therefore, you can convert from hex to binary very easily.
0x67 = 01100111
0xBD = 10111101
Now, just XOR the bits:
0x67^0xBD = 11011010
Then, convert back to hex:
1101 = D
1010 = A
Therefore, your final answer is 0xDA.
Happy hexing!
Corey
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a link that might be worth reading:
Javaranch campfire:Cat and Mouse Games with Bits
[ February 22, 2002: Message edited by: Valentin Crettaz ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic