Bit Basics¶
Table of Contents¶
- 3370. Smallest Number With All Set Bits (Easy)
- 3226. Number of Bit Changes to Make Two Integers Equal (Easy)
- 1356. Sort Integers by The Number of 1 Bits (Easy)
- 461. Hamming Distance (Easy)
- 2220. Minimum Bit Flips to Convert Number (Easy)
- 476. Number Complement (Easy)
- 1009. Complement of Base 10 Integer (Easy)
- 868. Binary Gap (Easy)
- 3211. Generate Binary Strings Without Adjacent Zeros (Medium)
- 2917. Find the K-or of an Array (Easy)
- 693. Binary Number with Alternating Bits (Easy)
- 2657. Find the Prefix Common Array of Two Arrays (Medium)
- 231. Power of Two (Easy)
- 342. Power of Four (Easy)
- 191. Number of 1 Bits (Easy)
- 2595. Number of Even and Odd Bits (Easy)
- 338. Counting Bits (Easy)
3370. Smallest Number With All Set Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, bit manipulation
3226. Number of Bit Changes to Make Two Integers Equal¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
1356. Sort Integers by The Number of 1 Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, bit manipulation, sorting, counting
461. Hamming Distance¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
2220. Minimum Bit Flips to Convert Number¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
476. Number Complement¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
1009. Complement of Base 10 Integer¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
868. Binary Gap¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
3211. Generate Binary Strings Without Adjacent Zeros¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, backtracking, bit manipulation
2917. Find the K-or of an Array¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, bit manipulation
693. Binary Number with Alternating Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
2657. Find the Prefix Common Array of Two Arrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, bit manipulation
231. Power of Two¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, bit manipulation, recursion
342. Power of Four¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: math, bit manipulation, recursion
191. Number of 1 Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: divide and conquer, bit manipulation
# Bit Manipulation
def hammingWeight1(n: int) -> int:
res = 0
while n != 0:
n = n & (n - 1) # Unset the rightmost 1-bit
res += 1
return res
def hammingWeight2(n: int) -> int:
return bin(n).count("1")
def hammingWeight3(n: int) -> int:
def decimalToBinary(n: int) -> str:
if n == 0:
return "0"
binary = ""
while n > 0:
binary = str(n % 2) + binary
n //= 2
return binary
binary = decimalToBinary(n)
return binary.count("1")
n = 11
print(hammingWeight1(n)) # 3
print(hammingWeight2(n)) # 3
n = 47
print(bin(n))
2595. Number of Even and Odd Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: bit manipulation
- Topic: Bit Manipulation
- Difficulty: Easy
You are given a positive integer n. Let even denote the number of even indices in the binary representation of n with value 1. Let odd denote the number of odd indices in the binary representation of n with value 1. Note that bits are indexed from right to left in the binary representation of a number. Return the array [even, odd].
338. Counting Bits¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: dynamic programming, bit manipulation
from typing import List
# Bit Manipulation
def countBits(n: int) -> List[int]:
bits = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
bits[i] = bits[i >> 1] + (i & 1)
return bits
n = 5
print(countBits(n)) # [0, 1, 1, 2, 1, 2]