From 8219effaa9c1d9b188e707f92f4c993cdacbc6a7 Mon Sep 17 00:00:00 2001 From: phil9l Date: Wed, 14 Oct 2020 14:22:48 +0200 Subject: [PATCH 1/3] Add single bit manipuation operations. --- .../single_bit_manipulation_operations.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bit_manipulation/single_bit_manipulation_operations.py diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py new file mode 100644 index 000000000000..c99d40aec7b9 --- /dev/null +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import doctest + +"""Provide the functionality to manipulate a single bit.""" + + +def set_bit(number: int, position: int): + """ + Set the bit on position to 1. + + Details: perform bitwise or for given number and X. + Where X is a number with all the bits – zeroes and bit on given + position – one. + + >>> set_bit(0b1101, 1) # 0b1111 + 15 + >>> set_bit(0b0, 5) # 0b100000 + 32 + >>> set_bit(0b1111, 1) # 0b1111 + 15 + """ + + return number | (1 << position) + + +def clear_bit(number: int, position: int): + """ + Set the bit on position to 0. + + Details: perform bitwise and for given number and X. + Where X is a number with all the bits – ones and bit on given + position – zero. + + >>> clear_bit(0b10010, 1) # 0b10000 + 16 + >>> clear_bit(0b0, 5) # 0b0 + 0 + """ + + return number & ~(1 << position) + + +def flip_bit(number: int, position: int): + """ + Flip the bit on position. + + Details: perform bitwise xor for given number and X. + Where X is a number with all the bits – zeroes and bit on given + position – one. + + >>> flip_bit(0b101, 1) # 0b111 + 7 + >>> flip_bit(0b101, 0) # 0b100 + 4 + """ + + return number ^ (1 << position) + + +def is_bit_set(number: int, position: int) -> bool: + """ + Flip the bit on position. + + Details: perform bitwise xor for given number and X. + Where X is a number with all the bits – zeroes and bit on given + position – one. + + >>> is_bit_set(0b1010, 0) + False + >>> is_bit_set(0b1010, 1) + True + >>> is_bit_set(0b1010, 2) + False + >>> is_bit_set(0b1010, 3) + True + >>> is_bit_set(0b0, 17) + False + """ + + return ((number >> position) & 1) == 1 + + +if __name__ == "__main__": + doctest.testmod() From 42516669bb4c32eb6ed115435578b419c62fcbc3 Mon Sep 17 00:00:00 2001 From: phil9l Date: Wed, 14 Oct 2020 15:59:46 +0200 Subject: [PATCH 2/3] fixup! Add single bit manipuation operations. --- bit_manipulation/single_bit_manipulation_operations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py index c99d40aec7b9..5b486410b033 100644 --- a/bit_manipulation/single_bit_manipulation_operations.py +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -import doctest - """Provide the functionality to manipulate a single bit.""" @@ -82,4 +80,6 @@ def is_bit_set(number: int, position: int) -> bool: if __name__ == "__main__": + import doctest + doctest.testmod() From eb85c69f774097f0e0d1a98e8645d0efd33dcdd2 Mon Sep 17 00:00:00 2001 From: phil9l Date: Sun, 25 Oct 2020 10:05:16 +0100 Subject: [PATCH 3/3] Change wording. --- .../single_bit_manipulation_operations.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py index 5b486410b033..114eafe3235b 100644 --- a/bit_manipulation/single_bit_manipulation_operations.py +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -5,7 +5,7 @@ def set_bit(number: int, position: int): """ - Set the bit on position to 1. + Set the bit at position to 1. Details: perform bitwise or for given number and X. Where X is a number with all the bits – zeroes and bit on given @@ -18,13 +18,12 @@ def set_bit(number: int, position: int): >>> set_bit(0b1111, 1) # 0b1111 15 """ - return number | (1 << position) def clear_bit(number: int, position: int): """ - Set the bit on position to 0. + Set the bit at position to 0. Details: perform bitwise and for given number and X. Where X is a number with all the bits – ones and bit on given @@ -35,13 +34,12 @@ def clear_bit(number: int, position: int): >>> clear_bit(0b0, 5) # 0b0 0 """ - return number & ~(1 << position) def flip_bit(number: int, position: int): """ - Flip the bit on position. + Flip the bit at position. Details: perform bitwise xor for given number and X. Where X is a number with all the bits – zeroes and bit on given @@ -52,17 +50,15 @@ def flip_bit(number: int, position: int): >>> flip_bit(0b101, 0) # 0b100 4 """ - return number ^ (1 << position) def is_bit_set(number: int, position: int) -> bool: """ - Flip the bit on position. + Is the bit at position set? - Details: perform bitwise xor for given number and X. - Where X is a number with all the bits – zeroes and bit on given - position – one. + Details: Shift the bit at position to be the first (smallest) bit. + Then check if the first bit is set by anding the shifted number with 1. >>> is_bit_set(0b1010, 0) False @@ -75,7 +71,6 @@ def is_bit_set(number: int, position: int) -> bool: >>> is_bit_set(0b0, 17) False """ - return ((number >> position) & 1) == 1