Qbasicnews.com

Full Version: Binary Problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I learned how to convert decimal to binary this way.

Here's an example, from 63 to binary.

the first number is always 1

divide 63 by 2, come up with 31, remainder 1

divide 31 by 2, come up with 15, remainder 1

divide 15 by 2, come up with 7, remainder 1

divide 7 by 2, come up with 3, remainder 1

divide 3 by 2, come up with 1, remainder 1

Put all the remainders together, and you come up with binary number 111111. Put this example in windows calculator and you get the same.

But when I do this for number 11 I get this

First number 1
Divide by 11, get 5, remainder 1
Divide 5 by 2, get 2, remaidner 1
Divide 2 by 2, get 0, remainder 0.

Binary number is 1110. But when I put this in windows calculator, I get 1011. What am I doing wrong Sad? Thanks for any help!
learn a new way?
Like so, for 11:
11 / 2 = 5 remainder 1
5 / 2 = 2 remainder 1
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1

Now list the digits in reverse: 1011

You were doing two things wrong:
1. False: "first number is always 1"
2. This zero should be a one: "Divide 2 by 2, get 0"
Ok got it, thanks.
Let me show you a more algorithmic method of converting and positive interger value (maximum 255) to its binary equivalent.
Code:
CONVERT2BIN:      'Subroutine to convert V to binary in VBIN$ (V=0 TO 255)
  VBIN$="00000000"
  FOR Z=0 TO 7
      IF (V AND 2^Z) THEN MID$(VBIN$,(Z XOR 7)+1,1)="1"
  NEXT Z
RETURN
*****
If you want to convert it orally/mentally. Then there is a simple solution. Remember the powers of two:
Code:
power of 2 : 0 1 2 3 4  5  6
decimal    : 1 2 4 8 16 32 64
Now you wanna convert 11 to binary? Its as simple as addition!
1 + 2 + 8 = 11
Thus the binary is 1011. Just combine the "decimal" numbers in the above table. Note numbers(here 1, 2, 8) that you use to get the actual decimal number(here 11). Put a 1 against these numbers while 0 against those you dont use. In this case,

Code:
power of 2 : 0 1 2 3 4  5  6
decimal    : 1 2 4 8 16 32 64
binary     : 1 1 0 1 0  0  0

Now, read the binary backwards i.e.e 1101000 should be read as 0001011. You can remove the leading zeros to get 1011 Wink