• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

14 ^ 23

 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic