This week's book giveaway is in the Servlets forum. We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line! See this thread for details.

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.

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

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

posted

0

Originally posted by fred rosenberger: ...and i have a clock on my desk that tells time that way...

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.