---
▎Bitwise Tricks in Python
▎1. Check Even/Odd
Try: “Check if a Number is Odd or Even” on GeeksforGeeks (GfG)
---
▎2. Swap Two Numbers Without a Temp Variable
Note: In real Python, you can simply use
---
▎3. Turn Off the Rightmost Set Bit
Try: “Count Set Bits in an Integer” on GfG (use this in a loop)
---
▎4. Isolate the Rightmost Set Bit
Try: “Find Position of Rightmost Different Bit” on GfG
---
▎5. Check Power of Two
Try: “Check if a Number is a Power of Two” on GfG
---
▎6. Count Set Bits (Brian Kernighan’s Algorithm)
Try: “Count Set Bits” on GfG (Python)
---
▎7. Add Two Numbers Without ‘+’
Try: “Sum of Two Integers” on LeetCode (Python)
---
▎8. Test Opposite Signs
Try: “Check if Two Numbers Have Opposite Signs” on GfG
---
▎9. Reverse Bits of a 32-bit Integer
Try: “Reverse Bits” on LeetCode (Python)
---
▎10. Gray Code Conversion
Try: “Gray Code” on LeetCode (Python)
---
▎🎯 Next Steps:
1. Pick one or two of these tricks.
2. Find the corresponding GfG problem in Python and implement it.
3. Time yourself and compare it to a straightforward solution.
▎Bitwise Tricks in Python
▎1. Check Even/Odd
def is_odd(n: int) -> bool:
return bool(n & 1)
# Usage:
n = 7
print("Odd" if is_odd(n) else "Even") # Output: Odd
Try: “Check if a Number is Odd or Even” on GeeksforGeeks (GfG)
---
▎2. Swap Two Numbers Without a Temp Variable
def xor_swap(a: int, b: int) -> tuple[int, int]:
a ^= b
b ^= a
a ^= b
return a, b
# Usage:
x, y = 5, 9
x, y = xor_swap(x, y)
print(x, y) # Output: 9 5
Note: In real Python, you can simply use
x, y = y, x.---
▎3. Turn Off the Rightmost Set Bit
def clear_lowest_bit(n: int) -> int:
return n & (n - 1)
# Usage:
n = 0b10110 # 22
print(bin(clear_lowest_bit(n))) # Output: 0b10100 (20)
Try: “Count Set Bits in an Integer” on GfG (use this in a loop)
---
▎4. Isolate the Rightmost Set Bit
def lowest_bit(n: int) -> int:
return n & -n
# Usage:
n = 0b10110 # 22
print(bin(lowest_bit(n))) # Output: 0b10 (2)
Try: “Find Position of Rightmost Different Bit” on GfG
---
▎5. Check Power of Two
def is_power_of_two(n: int) -> bool:
return n > 0 and (n & (n - 1)) == 0
# Usage:
for x in [1, 2, 3, 16, 18]:
print(x, is_power_of_two(x))
Try: “Check if a Number is a Power of Two” on GfG
---
▎6. Count Set Bits (Brian Kernighan’s Algorithm)
def count_set_bits(n: int) -> int:
count = 0
while n:
n &= (n - 1)
count += 1
return count
# Usage:
print(count_set_bits(29)) # Output: 4, since 29 is 11101₂
Try: “Count Set Bits” on GfG (Python)
---
▎7. Add Two Numbers Without ‘+’
def add(a: int, b: int) -> int:
MASK = 0xFFFFFFFF
while b != 0:
carry = (a & b) & MASK
a = (a ^ b) & MASK
b = (carry << 1) & MASK
return a if a <= 0x7FFFFFFF else ~(a ^ MASK)
# Usage:
print(add(15, 27)) # Output: 42
print(add(-5, 3)) # Output: -2
Try: “Sum of Two Integers” on LeetCode (Python)
---
▎8. Test Opposite Signs
def opposite_signs(a: int, b: int) -> bool:
return (a ^ b) < 0
# Usage:
print(opposite_signs(5, -3)) # Output: True
print(opposite_signs(-4, -2)) # Output: False
Try: “Check if Two Numbers Have Opposite Signs” on GfG
---
▎9. Reverse Bits of a 32-bit Integer
def reverse_bits(n: int) -> int:
result = 0
for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1
return result & 0xFFFFFFFF
# Usage:
x = 0b00000010100101000001111010011100
print(bin(reverse_bits(x)))
Try: “Reverse Bits” on LeetCode (Python)
---
▎10. Gray Code Conversion
def to_gray(n: int) -> int:
return n ^ (n >> 1)
def gray_to_binary(g: int) -> int:
mask = g
while mask:
mask >>= 1
g ^= mask
return g
# Usage:
for i in range(8):
g = to_gray(i)
print(f"{i:3} → Gray {g:3} → Back {gray_to_binary(g):3}")
Try: “Gray Code” on LeetCode (Python)
---
▎🎯 Next Steps:
1. Pick one or two of these tricks.
2. Find the corresponding GfG problem in Python and implement it.
3. Time yourself and compare it to a straightforward solution.
✍4
Forwarded from Hanix
Summer Camp and Internships
1. INSA Cyber Talent Challenge – 2017 E.C.
Tracks: Cybersecurity | Development | Embedded Systems | Aerospace
Apply: Here
2.iCog Labs AI Internship – 2025 Batch 1
Tracks: AI | ML | Robotics | Bioinformatics | Blockchain
Deadline: July 31
Apply:Here
3. Kuraz Technologies Hybrid Internship
Type: Summer | Unpaid | Hybrid
Deadline: May 31
Apply: Here
4. Tewanay Engineering Summer Internship
Tracks: Front-End | Back-End | Mobile App Development
Deadline: May 31
Apply: Here
@HanixJourney
#Internship
1. INSA Cyber Talent Challenge – 2017 E.C.
Tracks: Cybersecurity | Development | Embedded Systems | Aerospace
Apply: Here
2.iCog Labs AI Internship – 2025 Batch 1
Tracks: AI | ML | Robotics | Bioinformatics | Blockchain
Deadline: July 31
Apply:Here
3. Kuraz Technologies Hybrid Internship
Type: Summer | Unpaid | Hybrid
Deadline: May 31
Apply: Here
4. Tewanay Engineering Summer Internship
Tracks: Front-End | Back-End | Mobile App Development
Deadline: May 31
Apply: Here
@HanixJourney
#Internship
⚡3👍2
can u solve this question
▎Title: 201. Bitwise AND of Numbers Range
Difficulty: Medium
Denoscription:
Given two integers
Example 1:
Explanation:
Bitwise AND of all numbers in the range [5, 7] is 4 (binary 100).
Example 2:
Example 3:
Explanation:
There is no common bit position that stays 1 throughout the range, so the result is 0.
Constraints:
▎Title: 201. Bitwise AND of Numbers Range
Difficulty: Medium
Denoscription:
Given two integers
left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.Example 1:
Input: left = 5, right = 7
Output: 4
Explanation:
5 = 101
6 = 110
7 = 111
Bitwise AND of all numbers in the range [5, 7] is 4 (binary 100).
Example 2:
Input: left = 0, right = 0
Output: 0
Example 3:
Input: left = 1, right = 2147483647
Output: 0
Explanation:
There is no common bit position that stays 1 throughout the range, so the result is 0.
Constraints:
0 <= left <= right <= 2^31 - 1
can u solve this question? with bit manipulation in constant extra space and linear time complexity
▎Title: 137. Single Number II
Difficulty: Medium
Denoscription:
Given an integer array
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input:
Output:
Example 2:
Input:
Output:
Constraints:
•
•
• Each element in
▎Title: 137. Single Number II
Difficulty: Medium
Denoscription:
Given an integer array
nums where every element appears three times except for one, which appears exactly once, find the single element and return it.You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input:
nums = [2,2,3,2] Output:
3Example 2:
Input:
nums = [0,1,0,1,0,1,99] Output:
99Constraints:
•
1 <= nums.length <= 3 * 10^4•
-2^31 <= nums[i] <= 2^31 - 1• Each element in
nums appears exactly three times except for one element which appears once.❤3