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!

14 ^ 23

Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
Hi This was at the javaprepare test
14 ^ 23 actual ans was 25
but i reached 37 as follows
1 - 0001
4 - 0100
2 - 0010
3 - 0011
So 14 0001 0100
^
23 0010 0011
-------------------------
0011 0111
3 7
Can anyone do a postmortem of this.

When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
A single hexadecimal digit is equivalent to 4 binary digits. That's because 16 is equivalent to 2^4. However, one binary digit is not equivalent to 4 binary digits because 10 is not equivalent to 2^4. Therefore, your method of converting decimal digits to binary digits is flawed.
14 decimal = 1110 binary
23 deimcal = 10111 binary
14 ^ 23 = 1110 ^ 10111 = 11001 = 25
I hope that helps,
Corey

SCJP Tipline, etc.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11858

18

In other words, values are not stored how you apparently think they are. It is not true that each digit is stored in it's own 4 bytes, and then they are pushed together. What you are describing is Binary Coded Decimal (and i have a clock on my desk that tells time that way).
in Java (and other languages), the number is stored in base 2, or binary. each position, as we move from the least significant up, represents an increasingly larger power of 2. so, the least sig. digit represents 2^0 (that's 2 raised to the 0th power), the next over is 2^1, then 2^2... or in other words 1, 2, 4... etc.
there are many ways to convert from 14 base 10 into base 2. if the numbers are small enough, i just start figuring out which powers of 2 i need to add up to the value i want.
for 14, i need 8 + 4 + 2. those are 2^3, 2^2, and 2^1, so i would have
00001110.
for 23, i need 16 + 4 + 2 + 1, or 2^4 + 2^2 + 2^1 + 2^0. that give us
00010111

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by fred rosenberger:
...and i have a clock on my desk that tells time that way...

Geek.
[ April 13, 2004: Message edited by: Corey McGlone ]
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
Thanks that was great explanation
So what i understand is this
17 is (16 + 1) i.e. 00010001
12 is (8+4) i.e 00001100
and 17 ^ 12 is 00010001
00001100
------------------------------
00011101 (1+4+8+16) = 29
Gottit
Thanks a lot.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11858

18

Originally posted by Corey McGlone:

Geek.

Yeah. I'm not sure if the fact that it was given to me by my wife (an attorney) makes me MORE or LESS of a geek...

I agree. Here's the link: http://aspose.com/file-tools

subject: 14 ^ 23