From 97c369b0e0408fb8684506745fefe311bc04f586 Mon Sep 17 00:00:00 2001 From: Emmanuel Hernandez Date: Mon, 30 Jan 2023 09:53:26 -0600 Subject: [PATCH] write function in python to clear bits in range (i, j) --- .../ClearBitsInRange/main.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Bit-manipulationTechniques/ClearBitsInRange/main.py diff --git a/Bit-manipulationTechniques/ClearBitsInRange/main.py b/Bit-manipulationTechniques/ClearBitsInRange/main.py new file mode 100644 index 0000000..a181d55 --- /dev/null +++ b/Bit-manipulationTechniques/ClearBitsInRange/main.py @@ -0,0 +1,17 @@ +def clear_bits_in_range(n: int, i: int, j: int): + a = -1 << (j + 1) + b = (1 << i) - 1 + mask = a | b + + return n & mask + + +def main(): + n, i, j = 31, 1, 3 + + n = clear_bits_in_range(n, i, j) + print(n) + + +if __name__ == "__main__": + main()