0

I want to know if there is a way to find the last bits of a given decimal number. I want to calculate this by hand.

For example if i have 133 and 255 how can i find the last two bits in binary, without dividing repeatly by 2?

Luca Angioloni
  • 2,243
  • 2
  • 19
  • 28
tzoella
  • 1
  • 2

1 Answers1

1

Here is an easy way to find the two least significant bits of a decimal number, appropriate for doing in your head.

You need only the last two decimal digits, so throw away (mentally) the rest. Your examples are thus 33 and 55. Look at the last decimal digit: if it is odd, the least significant digit is 1; if it is even, the least significant digit is 0. In your examples, the last digits are 3 and 5, so they both have the last binary digit 1.

If the binary digit we got in the last step was 1, subtract 1 from the numbers. So your examples are now 32 and 54. Now we look at both those last decimal digits. If the last decimal digit is divisible by 4 (i.e. it is 0, 4, or 8) and the next-to-last decimal digit is even, the next-to-last binary digit is 0. If the last decimal digit is not divisible by 4 (i.e. it is 2 or 6) and the next-to-last decimal digit is odd, the next-to-last binary digit is 0. Otherwise, the next-to-last binary digit is 1. In your first example, where we now have 32, the last decimal digit is 2, not in the first list but in the second, and the next-to-last decimal digit is odd, so the next-to-last binary digit is 0. In your second example, where we now have 54, the last decimal digit is 4, in the first list, and the next-to-last decimal digit is odd, so the next-to-last binary digit is 1. Therefore, 133 ends with 01 in binary, while 255 ends in 11 in binary.

If you don't like that last rule, you can express it differently. If the two-digit number in that step is divisible by 4, the next-to-last binary digit is 0, otherwise it is 1. In your examples, 32 is divisible by 4 so we get the 0 bit, but 54 is not divisible by 4 so we get the 1 bit. The results are the same, but this second approach requires division which you seem to want to avoid, while the first approach avoids division but requires more memory work.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50