This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

simon kumar
Greenhorn

Joined: Feb 06, 2002
Posts: 13
Hi,
can anyone tell me what is the simplest way to calculate 0x67^0xBD,the result is 0xDA
simon
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Dave
Donald Yee
Greenhorn

Joined: Feb 22, 2002
Posts: 6
Easiest way is to convert to binary.
0x67 = 0110 0111
^ 0xBD = 1011 1101
------------------
1101 1010 = 0xDA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
Happy hexing!
Corey

SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610