14/14 error


#1
def flip_bit(number,n):
    mask = (bin(number) << n)
    result = mask ^ n
    return bin(result)

what is wrong with the code?


Anyone has any ideas on "Turn it on"?
#2

Here is the correct function:

def flip_bit(number,n):
    mask = (0b1 << (n-1))
    result = number ^ mask
    return bin(result)

#3
== Right Shift 5 >> 4 ==================
0b00000101 (5)
0b00000000 (5 >> 4 )
0
== Left Shift 5 << 1 ==================
0b00000101 (5)
0b00001010 (5 << 1 )
10
== Bitwise AND 8 & 5==================
AND | 0 1 
----+-----
 0  | 0 0 
 1  | 0 1 
0b00000101 (5)
0b00001000 (8)
0b00000000
== Bitwise OR 9 | 4 ==================
 OR | 0 1
----+-----
  0 | 0 1  
  1 | 1 1  
0b00001001 (9)
0b00000100 (4)
0b00001101
13

==

== Bitwise XOR  12 ^ 42 ==================
XOR | 0 1
----+----
  0 | 0 1
  1 | 1 0
0b00001100 (12)
0b00101010 (42)
0b00100110
38
== Bitwise NOT ~88==================
NOT | 0 1
----+-----
    | 1 0
0b01011000
-0b1011001
-89
====================

==

== flip_bit(6,2) ==
0b00000110 (6)
0b00011000 ( 6 << 2 ) (24)
============

0b00000110 (6)
== mask = number << n // 0b00000110 << 2
0b00011000 ( 6 << 2 ) (24)
0b00000010 (2)
== mask ^ n // 0b00011000 ^ 0b00000010
XOR | 0 1
----+----
  0 | 0 1
  1 | 1 0
0b00011000 (24)
0b00000010 (2)
0b00011010  //Result 26
== END flip_bit

==

== flip_bit(7,2) ==
0b00000111 (7)
0b00011100 ( 7 << 2 ) (28)
============

0b00000111 (7)
== mask = number << n // 0b00000111 << 2
0b00011100 ( 7 << 2 ) (28)
0b00000010 (2)
== mask ^ n // 0b00011100 ^ 0b00000010
XOR | 0 1
----+----
  0 | 0 1
  1 | 1 0
0b00011100 (28)
0b00000010 (2)
0b00011110  //Result 30
== END flip_bit

Slide to the Left! Slide to the Right!
#4
print "== Right Shift 5 >> 4 =================="
print format(5, '#010b') +" ("+str(5) +")"
print format(5 >> 4, '#010b') +" ("+str(5) +" >> 4 )"
print 5 >> 4  # Right Shift
print "== Left Shift 5 << 1 =================="
print format(5, '#010b') +" ("+str(5) +")"
print format(5 << 1, '#010b') +" ("+str(5) +" << 1 )"
print 5 << 1  # Left Shift

=

print "== Bitwise AND 8 & 5=================="
print "AND | 0 1 "
print "----+-----"
print " 0  | 0 0 "
print " 1  | 0 1 "
print format(5, '#010b') +" ("+str(5) +")"
print format(8, '#010b') +" ("+str(8) +")"
print format(8 & 5, '#010b')   # Bitwise AND

=

print "== Bitwise OR 9 | 4 =================="

print " OR | 0 1"
print "----+-----"
print "  0 | 0 1  "
print "  1 | 1 1  "
print format(9, '#010b') +" ("+str(9) +")"
print format(4, '#010b') +" ("+str(4) +")"
print format(9 | 4, '#010b')
print 9 | 4   # Bitwise OR
print "== Bitwise XOR  12 ^ 42 =================="

=

print "XOR | 0 1"
print "----+----"
print "  0 | 0 1"
print "  1 | 1 0"
print format(12, '#010b') +" ("+str(12) +")"
print format(42, '#010b') +" ("+str(42) +")"
print format(12^42, '#010b')
print 12 ^ 42 # Bitwise XOR
print "== Bitwise NOT ~88=================="

=

print "NOT | 0 1"
print "----+-----"
print "    | 1 0"
print format(88, '#010b')
print format(~88, '#010b')

print ~88     # Bitwise NOT
print "====================\n\n\n"

=

def flip_bit(number,n):
    print "== flip_bit(" + str(number) +","+ str(n) + ") =="
    print format(number, '#010b')+" ("+str(number)+")"
    print format(number << n, '#010b')+\
          " ( "+str(number)+" << "+str(n)+" ) ("+\
          str(number << n)+")"
    print "============\n"
    print format(number, '#010b')+" ("+str(number)+")"
    print "== mask = number << n // "+format(number, '#010b')+" << "+str(n)
    mask = number << n
    print format(mask, '#010b') +" ( "+str(number)+" << "+str(n)+" )"+\
          " ("+ str(number << n)+")"
    print format(n, '#010b')+" ("+str(n)+")"
    print "== mask ^ n // "+ format(mask, '#010b') + " ^ "+\
          format(n, '#010b')
    print "XOR | 0 1"
    print "----+----"
    print "  0 | 0 1"
    print "  1 | 1 0"
    print format(mask, '#010b') +" ("+ str(number << n)+")"
    print format(n, '#010b') +" ("+ str(n)+")"
    result = mask ^ n
    print format(result, '#010b') +"  //Result "+\
          str(mask ^ 2) 
#    return bin(result)

=

flip_bit(6,2)
print "== END flip_bit \n\n\n"
flip_bit(7,2)
print "== END flip_bit \n\n\n"

11. The man behind the mask is giving me pronlems
Bit mask
#5

Thanks for the answer, but can you explain it? I wrote the following code, and I don't understand why it isn't correct. Why do you need to subtract 1 from n?

def flip_bit(number, n):
    mask = (0b1 << n)
    result = number ^ mask
    return bin(result)

Thank you!


#6

@sardawson,

'''
Let's say that I want
to turn on the 2th bit from the right of the integer a.

Instead of writing out the entire number,
we slide a bit over using the << operator.

We use 1
because we only need to slide the mask 1 place over
from the first bit to reach the 2nd bit.
'''

print "== your flip_bit function =="
def flip_bit(number, n):
    mask = (0b1 << n)   #you move to the 3th bit
    print format(mask, '#010b')
    print format(number, '#0010b')
    result = number ^ mask
    print format(result, '#010b')
    print bin(result)
    return bin(result)
    
flip_bit(10,2)

def flip_bit(number,n):
    mask = (0b1 << (n-1)) #he moves over to the 2nd bit
    print format(mask, '#010b')
    print format(number, '#010b')
    result = number ^ mask
    print format(result, '#010b')
    print bin(result)
    return bin(result)

print "===================="
print "== his  flip_bit function =="
flip_bit(10,2)

[output]

== your flip_bit function ==
0b00000100
0b00001010
0b00001110
0b1110
====================
== his  flip_bit function ==
0b00000010
0b00001010
0b00001000
0b1000

#7

I'm pretty sure I get sliding the bit, but I still don't understand why you have to subtract 1 from n. Why is the default the 3th position if the argument is n?


#8

1 byte is 8 bit

  8765 4321 bit-count as number - - - - - -+
  0000 0000                                |
  7654 3210 bit-count as =shift= <<    (number-1)

  8765 4321 bit-count as number - - - ( shift-count + 1)
  0000 0000                                |
  7654 3210 bit-count as =shift= << - - - -+

#9

Oh dear. I am actually more confused now :frowning:


#10

Hi,

In response to your question regarding the position to insert 0b1, think of it sort of as a list. 0b1 = 1 and you want to position it so that it has the same place value as n; that way you can flip with XOR.

But just because you want 0b1 to fall in position n, you can't discount the fact 0b1 already has a place value of 1. Shifting by n would result in shifting n+1. That is why you subtract 1 (at least that is how I think about it).


#11

So with 0b1, subtracting one shifts the original value over one slot to it's starting slot... of 1? If this is what you mean, I understand. I really hope this is what you mean, LOL!


#12

the reason for substracting 1 from n is to get the shift position.
take for instance: 0b0001
you want to shift left to bit 3 which should give us 0b0100
all we had to do was move the 1 left two times i.e (n-1) 3-1= 2