Having a little difficulty with a simple prioblem


#1

I'm 80% sure everything but eleven and 12 are right. I need someone to look over this please. Thanks in advance.

one = 0b1
two = 0b10
three = 0b11
four =0b100
five=0b101
six =0b110
seven =0b111
eight= 0b1000
nine=0b1001
ten=0b1010
eleven=0b1101
twelve=0b1110


#2

If you don't have an IDE to try them,

0b1110 == 12
# OUTPUT: False
0b1101 == 11
# OUTPUT: False

To determine what a number is in binary use bin()

Example:

bin(12)
# OUTPUT: '0b1100'

You can also do that in the workstation too.


#3

Also a neat little trick with string formating you can just have it count for you...

Example

for x in range(13):
    print("Number: %s, Binary: %s" % (x, bin(x)))

Output:

Number: 0, Binary: 0b0
Number: 1, Binary: 0b1
Number: 2, Binary: 0b10
Number: 3, Binary: 0b11
Number: 4, Binary: 0b100
Number: 5, Binary: 0b101
Number: 6, Binary: 0b110
Number: 7, Binary: 0b111
Number: 8, Binary: 0b1000
Number: 9, Binary: 0b1001
Number: 10, Binary: 0b1010
Number: 11, Binary: 0b1011
Number: 12, Binary: 0b1100

#4

Well, there's only one way to be sure right? It's math! /o/

Remember that these bits in these nibbles actually represent powers of 2: 2**3, 2**2, 2**1 and 2**0, or literally: 8, 4, 2 and 1. So what is eleven? Eleven is 8 + 2 + 1 = (1 * 2**3) + (0 * 2**2) + (1 * 2**1) + (1 * 2**0). So eleven in binary is 0b1011.

And what is twelve? Twelve is 8 + 4 = (1 * 2**3) + (1 * 2**2) + (0 * 2**1) + (0 * 2**0). So twelve in binary is 0b1100.