From b39803b9108ad936f9fa4d9015d12d0bbac23d81 Mon Sep 17 00:00:00 2001 From: Emmanuel Hernandez Date: Tue, 24 Jan 2023 14:48:29 -0600 Subject: [PATCH] write function to set ith bit in python --- Bit-manipulationTechniques/SetIthBit/main.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Bit-manipulationTechniques/SetIthBit/main.py diff --git a/Bit-manipulationTechniques/SetIthBit/main.py b/Bit-manipulationTechniques/SetIthBit/main.py new file mode 100644 index 0000000..64d6750 --- /dev/null +++ b/Bit-manipulationTechniques/SetIthBit/main.py @@ -0,0 +1,15 @@ +def set_ith_bit(n: int, i: int): + mask = (1 << i) + return (n | mask) + + +def main(): + n = 5 + i = int(input()) + + n = set_ith_bit(n, i) + print(f"New value of n: {n}") + + +if __name__ == "__main__": + main()