From b18351507c39a8ab20148303bb7ef179c85dc710 Mon Sep 17 00:00:00 2001 From: Emmanuel Hernandez Date: Tue, 24 Jan 2023 13:55:11 -0600 Subject: [PATCH] write function to get ith bit in python --- Bit-manipulationTechniques/GetIthBit/main.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Bit-manipulationTechniques/GetIthBit/main.py diff --git a/Bit-manipulationTechniques/GetIthBit/main.py b/Bit-manipulationTechniques/GetIthBit/main.py new file mode 100644 index 0000000..3128cca --- /dev/null +++ b/Bit-manipulationTechniques/GetIthBit/main.py @@ -0,0 +1,14 @@ +def get_ith_bit(n: int, i: int): + mask = 1 << i + return 1 if (n & mask) > 0 else 0 + + +def main(): + n = 5 + i = int(input()) + + print(f"Bit at {i}th position: {get_ith_bit(n, i)}") + + +if __name__ == "__main__": + main()