bitwise scripts fixed

This commit is contained in:
Mari Wahl 2015-01-06 21:55:53 -05:00
parent 01703751f1
commit 91825867f6
25 changed files with 96 additions and 335 deletions

View File

@ -1,17 +1,16 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail
''' Example of how to use a bit array in python as a "counter" dict: __author__ = "bt3"
''' Example of how to use a bit array in python as a "counter" dict'''
def print_dupl_ba(l1):
'''
>>> l1 = [0, 1, 2, 3, 4, 2, 6, 7, 8, 9] >>> l1 = [0, 1, 2, 3, 4, 2, 6, 7, 8, 9]
>>> print_dupl_ba(l1) >>> print_dupl_ba(l1)
2 2
''' '''
def print_dupl_ba(l1):
bs = bytearray(10) bs = bytearray(10)
for i in range(len(l1)): for i in range(len(l1)):
if i == l1[i]: if i == l1[i]:
@ -23,9 +22,6 @@ def print_dupl_ba(l1):
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -1,40 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' Clear a bit in a binary number. It is almost like the reverse of set bit:
1) first create a number filled of 1s, with 0 at i (can create 0001000 and ~)
2) AND the number so it clears the ith bit
>>> num = int('10010000', 2)
>>> clear_bit(num, 4)
'0b10000000'
>>> num = int('10010011', 2)
>>> clear_all_bits_from_i_to_0(num, 2)
'0b10010000'
>>> num = int('1110011', 2)
>>> clear_all_bits_from_most_sig_to_1(num, 2)
'0b11'
'''
def clear_bit(num, i):
mask = ~ (1 << i) # -0b10001
return bin(num & mask)
def clear_all_bits_from_i_to_0(num, i):
mask = ~ ( (1 << (i+1)) - 1)
return bin(num & mask)
def clear_all_bits_from_most_sig_to_1(num, i):
mask = ( 1 << i) -1
return bin(num & mask)
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -1,28 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' Find how many bits a int has:
1) Start with a mask of 1
2) Mask with AND
3) if result (if true): count += 1
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
>>> for i in range(17): print(find_bit_len(i))
'''
def find_bit_len(int_num):
lenght = 0
while int_num:
int_num >>= 1
lenght += 1
return lenght
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -1,41 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' Get a bit in a binary number:
1) Shifts 1 over by i bits
2) make an AND with the number
3) all the other than the bit at i are clean, now compare to 0
4) if the new value is not 0, bit i is 1
>>> num = int('0100100', 2)
>>> get_bit(num, 0)
0
>>> get_bit(num, 1)
0
>>> get_bit(num, 2)
1
>>> get_bit(num, 3)
0
>>> get_bit(num, 4)
0
>>> get_bit(num, 5)
1
>>> get_bit(num, 6)
0
'''
def get_bit(num, i):
mask = 1 << i
return num & mask != 0
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -1,45 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' This method returns the number of bits that are necessary to change to convert two
numbers A and B:
1) XOR
2) count 1s
>>> a = int('10010000', 2)
>>> b = int('01011010', 2)
>>> count_bits_swap(a, b)
4
>>> count_bits_swap2(a, b)
4
'''
def count_bits_swap2(a, b):
count = 0
m = a^b
while m:
count +=1
m = m & (m-1)
return count
def count_bits_swap(a, b):
m = a^b
return count_1s(m)
def count_1s(m):
count = 0
while m:
if m& 1 :
count +=1
m >>= 1
return count
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -1,38 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' Set a bit in a binary number:
1) Shifts 1 over by i bits
2) make an OR with the number, only the value at bit i will change and all the others bit
of the mask are zero so will not affect the num
>>> num = int('0100100', 2)
>>> set_bit(num, 0)
'0b100101'
>>> set_bit(num, 1)
'0b100110'
>>> set_bit(num, 2) # nothing change
'0b100100'
>>> set_bit(num, 3)
'0b101100'
>>> set_bit(num, 4)
'0b110100'
>>> set_bit(num, 5) # nothing change
'0b100100'
'''
def set_bit(num, i):
mask = 1 << i
return bin( num | mask )
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -1,26 +0,0 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
''' This method merges set bit and clean bit:
1) first clear the bit at i using a mask such as 1110111
2) then shift the intended value v by i bits
3) this will create a number with bit i to v and all other to 0
4) finally update the ith bit with or
>>> num = int('10010000', 2)
>>> update_bit(num, 2, 1)
'0b10010100'
'''
def update_bit(num, i, v):
mask = ~ (1 << i)
return bin( (num & mask) | (v << i) )
if __name__ == '__main__':
import doctest
doctest.testmod()

View File

@ -6,7 +6,8 @@ __author__ = "bt3"
1) Start with a mask of 1 1) Start with a mask of 1
2) Mask with AND 2) Mask with AND
3) if result (if true): count += 1 3) if result (if true): count += 1
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int)) (obs: to find the int of a bin do int('1001', 2)) and to show in bin
do bin(int))
''' '''
@ -22,4 +23,3 @@ if __name__ == '__main__':
for i in range(17): for i in range(17):
print(find_bit_len(i)) print(find_bit_len(i))
print i.bit_length() print i.bit_length()
print

View File

@ -1,21 +1,22 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail __author__ = "bt3"
''' Find how many 1s in the binary: ''' Find how many 1s in the binary:
1) Start with a mask of 1 1) Start with a mask of 1
2) Mask with AND 2) Mask with AND
3) if result (if true): count += 1 3) if result (if true): count += 1
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int)) (obs: to find the int of a bin do int('1001',
2)) and to show in bin do bin(int))
>>> find_how_many_1_in_a_binary(9)
2
''' '''
def find_how_many_1_in_a_binary(n): def find_how_many_1_in_a_binary(n):
'''
>>> find_how_many_1_in_a_binary(9)
2
'''
counter = 0 counter = 0
while n: while n:
if n & 1: if n & 1:
@ -25,9 +26,6 @@ def find_how_many_1_in_a_binary(n):
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -1,20 +1,22 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail __author__ = "bt3"
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary ''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
representation. If the Number cannot be represented accurately in binary, with at representation. If the Number cannot be represented accurately in binary, with at
most 32 chars, print error: most 32 chars, print error:
'''
def get_float_rep(num):
'''
>>> get_float_rep(0.72) >>> get_float_rep(0.72)
('Error 2', '.1011100001010001111010111000010') ('Error 2', '.1011100001010001111010111000010')
>>> get_float_rep(0.1) >>> get_float_rep(0.1)
('Error 2', '.0001100110011001100110011001100') ('Error 2', '.0001100110011001100110011001100')
>>> get_float_rep(0.5) >>> get_float_rep(0.5)
'.1' '.1'
''' '''
def get_float_rep(num):
if num >= 1 or num <= 0: return 'Error 1' if num >= 1 or num <= 0: return 'Error 1'
result = '.' result = '.'
while num: while num:

View File

@ -1,22 +1,25 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail __author__ = "bt3"
''' Given two 32-bit numbers, N and M, and two bit positions, i and j, this ''' Given two 32-bit numbers, N and M, and two bit positions, i and j, this
method insert M into N such that M starts at bit j and ends at bit i: method insert M into N such that M starts at bit j and ends at bit i:
1) clear the bits j thru i in N' 1) clear the bits j thru i in N'
2) shift M so that it lines up with bits j thru i 2) shift M so that it lines up with bits j thru i
3) merge M and N 3) merge M and N
'''
def insert_small_bin_into_big_bin(M, N, i, j):
'''
>>> N = 0b10000000000 >>> N = 0b10000000000
>>> M = 0b10011 >>> M = 0b10011
>>> j = 6 >>> j = 6
>>> i = 2 >>> i = 2
>>> insert_small_bin_into_big_bin(M, N, i, j) >>> insert_small_bin_into_big_bin(M, N, i, j)
'0b10001001100' '0b10001001100'
''' '''
def insert_small_bin_into_big_bin(M, N, i, j):
allOnes = ~0 allOnes = ~0
left = allOnes << (j+1) # 1110000 left = allOnes << (j+1) # 1110000
right = ( (1 << i) - 1) # 0000111 right = ( (1 << i) - 1) # 0000111
@ -27,8 +30,6 @@ def insert_small_bin_into_big_bin(M, N, i, j):
return bin( N_cleared | M_shifted) return bin( N_cleared | M_shifted)
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -1,21 +1,13 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail __author__ = "bt3"
''' Give a positive int, print the next smallest and next largest ints with ''' Give a positive int, print the next smallest and next largest ints with
same number of 1 bits. same number of 1 bits.
The brute force is: The brute force is:
1) find number of 1 bits 1) find number of 1 bits
2) loop above and down until find same, checking for each 2) loop above and down until find same, checking for each
>>> num = 0b1001
>>> next = '0b1010'
>>> prev = '0b110'
>>> print_prev_same_1s(num) == prev
True
>>> print_next_same_1s(num) == next
True
''' '''
@ -59,6 +51,9 @@ def find_num_1s(num):
if __name__ == '__main__': if __name__ == '__main__':
import doctest num = 0b1001
doctest.testmod() n = '0b1010'
p = '0b110'
print_prev_same_1s(num) == p
print_next_same_1s(num) == n

View File

@ -8,18 +8,14 @@ __author__ = "bt3"
1) Shifts 1 over by i bits 1) Shifts 1 over by i bits
2) make an OR with the number, only the value at bit i will change and all the others bit 2) make an OR with the number, only the value at bit i will change and all the others bit
of the mask are zero so will not affect the num of the mask are zero so will not affect the num
''' '''
def set_bit(num, i): def set_bit(num, i):
mask = 1 << i mask = 1 << i
return bin( num | mask ) return bin( num | mask )
if __name__ == '__main__': if __name__ == '__main__':
num = int('0100100', 2) num = int('0100100', 2)
print set_bit(num, 0) #'0b100101' print set_bit(num, 0) #'0b100101'

View File

@ -8,6 +8,10 @@ __author__ = "bt3"
def swap_bit(a, b): def swap_bit(a, b):
'''
>>> swap_bit(14, 73)
(73, 14)
'''
a = a^b a = a^b
b = a^b b = a^b
a = a^b a = a^b
@ -15,7 +19,5 @@ def swap_bit(a, b):
if __name__ == '__main__': if __name__ == '__main__':
a = 14 import doctest
b = 73 doctest.testmod()
a2, b2 = swap_bit(a, b)
print "a was {0}, now it is {1}. \nb was {2}, now it is {3}".format(a, a2, b, b2)

View File

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/env python
# mari von steinkirch @2013
# steinkirch at gmail __author__ = "bt3"
''' Swap odd and even bits in a smart way in a binary: ''' Swap odd and even bits in a smart way in a binary:
1) first for odds, take n and move the odd: 1) first for odds, take n and move the odd:
@ -8,17 +9,18 @@
(b) shift by right by 1 (b) shift by right by 1
2) do the same to ints with 01010101 2) do the same to ints with 01010101
3) merge 3) merge
>>> num = 0b11011101
>>> result = '0b1101110'
>>> swap_odd_even(num) == result
True
''' '''
def swap_odd_even(num): def swap_odd_even(num):
'''
>>> num = 0b11011101
>>> result = '0b1101110'
>>> swap_odd_even(num) == result
True
'''
mask_odd = 0xAA # 0b10101010 mask_odd = 0xAA # 0b10101010
mask_even = 0x55 # 0b1010101 mask_even = 0x55 # 0b1010101
odd = num & mask_odd odd = num & mask_odd
@ -29,9 +31,6 @@ def swap_odd_even(num):
if __name__ == '__main__': if __name__ == '__main__':
import doctest import doctest
doctest.testmod() doctest.testmod()

View File

@ -1,8 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
__author__ = "bt3"
def bubble_sort(seq): def bubble_sort(seq):

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"

View File

@ -1,8 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
__author__ = "bt3"
def gnome_sort(seq): def gnome_sort(seq):

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"
class Heap(object): class Heap(object):

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"
''' Heapsort using Pythons libraries''' ''' Heapsort using Pythons libraries'''

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"

View File

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "bt3"
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"
''' Some examples of how to implement Quick Sort in Python ''' Some examples of how to implement Quick Sort in Python

View File

@ -1,7 +1,6 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "Mari Wahl" __author__ = "bt3"
__email__ = "marina.w4hl@gmail.com"
def selection_sort(seq): def selection_sort(seq):

View File

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/env python
__author__ = "bt3"
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
''' A method to sort an array so that all the anagrams are together. Since we only ''' A method to sort an array so that all the anagrams are together. Since we only
want the anagrams to be grouped, we can use a dictionary for this task. This want the anagrams to be grouped, we can use a dictionary for this task. This