Bit Manipulation
Bit Manipulation
presented by group 1
Ai My, Nguyen Tin, Mai Hien,
Van Tanh, Tan Phuc, Quoc Tien
GROUP MEMBERS
Some
Introduction examples
2 4
Bitwise Pros
Operators &
Cons
INTRODUCTION
Bit Manipulation is a technique used in a variety of
implementation fast.
BITWISE OPERATORS
1 2 3
4 5 6
AND
• The result is 1 if both bits are 1;
otherwise, it is 0.
• A&B
• Truth table:
AND
For instance: Check a number if it is
even or odd
AND
Principle:
• If n & 1 == 0, the number is
even (last bit is 0).
• If n & 1 == 1, the number is
odd (last bit is 1).
OR
• NOT (~):
• Bit inversion (0 to 1, 1 to 0).
• ¬A
NAND
• Invert the result of the AND
operation.
• A NAND B=¬(A&B)
• Truth table:
NOR
• Invert the result of the OR operation.
• A NOR B=¬(A∣B)
• Truth table:
BIT SHIFT
Left Shift (<<) Right Shift (>>)
• a << b: Shifts the bits of a to • a >> b: Shifts the bits of a to
the left by b positions. the right by b positions.
• New bits may appear on the • The bits shifted to the right will
right side of the number, and disappear, and the empty bits on
they will be 0. the left will become 0.
• Example: • Example: 10 >> 2 = 1010 >>
10 << 2 = 1010 << 2 = 2 = 10
101000 •a > > b = a/(2^b)
• a < < b = a*(2^b) • 1 >> k = ½^k
•1 << k = 2^k •K >> 1 = k/2 (integer division)
•K << 1 = k * 2
PRIORITY
Problem:
Analysis: Transformati
● Observation: This is
equivalent to representing n in on
binary form.
● In fact, the problem can be
solved using a simple method
with a time complexity of O(log
n), but that is not the most
Optimized solution:
+ Essentially, the O(logn)
approach is just about finding the
positions where the bits are equal
to 1.
l v e t h e + Observation: Since n≤10^9, the
o ss i b l e t o s o
Is i t p maximum bit length is only 32
ro b l e m i n
above p ) t i m e bits.
x i m a t e l y O ( 3 2 -> We can iterate through each bit
appr o ) ?
y ( w i t h n ≤ 1 e 9
co m p l e x i t to check because the time
complexity is very low.
+ So, is there a way to determine
whether the ii-th bit is 1 in O(1)
time?
-> Based on bitwise properties, it
is easy to check if the ii-th bit is 1
using the condition: if ((n >> i) &
n >> i is to shift i
bits to the right
For example: n=10
(binary
representation is
00001101)
-> n >> 1 =
explain
00000110
n >> 2 =
00000011
n >> 3 =
Bitwise operations 2
Summarize the
problem
Given two numbers a and b, we obtain a number c through
the following operations:
● Bits 0 to 4 of c are the result of the XOR operation on the
corresponding bits of a and b. For example, bit 0 of c is the
value of bit 0 of a XOR bit 0 of b.
● Bits 5 to 9 are the result of the AND operation on the
corresponding bits of a and b.
● Bits 10 to 14 are the result of the OR operation on the
corresponding bits of a and b.
Constraints:
● 1 <= q <= 10^5
● 0 <= a < b <= 2^15
We can solve this problem using a basic
iteration approach. Let the final result of
the query be the variable answer.
Idea Iterate from 0 to the highest bit position
of a and b minus 1. For each position,
extract the corresponding bits of a and
b, called bit_a and bit_b. Then, based
on the current position, use an if
condition to determine the value of a
variable res, which is calculated using
bit_a XOR bit_b, bit_a AND bit_b, or
bit_a OR bit_b.
Once res is obtained, assign its value to
the corresponding position in the
answer variable.
One important thing to note is that the
Operations
CAPTIAN
PROBLEM
GROUP 1
problem
LEFT section: A soldier will work on the left side of the ship if:
When the digits on the left of the id are progressively removed in
order, the remaining numbers are all prime numbers.
RIGHT section: A soldier will work on the right side of the ship if:
When the digits on the right of the id are progressively removed in
order, the remaining numbers are all prime numbers.
DEAD:
A soldier will be thrown into the sea if they do not qualify to work in
any section of the ship.
This only significantly improves storage memory, but the time efficiency is still not optimal, so further
improvements are needed.
Since all prime numbers except 2 are odd, we can skip all even numbers and only store the state of
odd numbers. Thus, prime[0] will store the states of 1, 3, 5, 7, … instead of all numbers from 1 to N.
This approach saves more memory and significantly increases processing speed.
Example: