0% found this document useful (0 votes)
229 views15 pages

Bit Manipulation

The document presents a comprehensive overview of bit manipulation, including binary representation and various bitwise operators such as AND, OR, XOR, NOT, left shift, and right shift. It outlines properties of XOR and its applications in checking even/odd numbers, swapping, and counting set bits, along with methods for calculating the XOR of a given range. Additionally, it provides practice problems and links to relevant coding challenges.

Uploaded by

Archisman Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views15 pages

Bit Manipulation

The document presents a comprehensive overview of bit manipulation, including binary representation and various bitwise operators such as AND, OR, XOR, NOT, left shift, and right shift. It outlines properties of XOR and its applications in checking even/odd numbers, swapping, and counting set bits, along with methods for calculating the XOR of a given range. Additionally, it provides practice problems and links to relevant coding challenges.

Uploaded by

Archisman Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BIT

MANIPULATION
PRESENTED BY SUBH RAJ and PRITHIRAJ
Mentors:
• Abhinav Verma
• Rishiraj Mukherjee
• Mithun
• Yasharth Shukla
• Subh Raj
• Ravi Shankar
• Prithiraj
If you have any doubts regarding a problem or anything relevant to the
bootcamp , feel free to ask in the group or message any of the admins directly
— all the admins are mentors. All information regarding the bootcamp —
including problems, sessions, and schedules — will be shared in the group
Binary Representation:
For positive numbers:
• Decimal 5 → Binary 101
• Decimal 10 → Binary 1010
BITWISE OPERATORS:
1. Bitwise AND (&)
● Sets each bit to 1 if both bits are 1.

Example: 5 & 3 → 0101 & 0011 = 0001 → 1

2. Bitwise OR (|)
● Sets each bit to 1 if at least one bit is 1.

Example: 5 | 3 → 0101 | 0011 = 0111 → 7

3. Bitwise XOR (^)


● Sets each bit to 1 if the bits are different.

Example : 5 ^ 3 → 0101 ^ 0011 = 0110 → 6


4. Bitwise NOT (~)
● Flips all the bits (1 → 0 and 0 → 1).

Example : ~(101101) → 010010

5. Left Shift (<<)


● Shifts bits to the left, adds 0s at the right.

● Equivalent to multiplying by 2ⁿ.

Example : (5 << 1) → 101 → 1010 = 10

6. Right Shift (>>)


● Shifts bits to the right.

● Equivalent to dividing by 2ⁿ.

Example : (8 >> 2) → 1000 → 0010 = 2


PROPERTIES OF XOR
1. Identity property : a ^ 0 = a

2. Self-inverse : a ^ a = 0

3. Commutative : a^b=b^a

4. Associative : (a ^ b) ^ c = a ^ (b ^ c)
Applications
1. Checking if a number is even/odd: (n&1)

2. Swapping two numbers: a ^= b; b ^= a; a ^= b

3. Checking if a number is a power of 2: n & (n - 1) == 0

4. Set ith bit: n | (1 << i)


5. Clear ith bit : n & ~(1 << i)

6. Count set bits: builtin_popcount(n)

7. Toggle i-th bit : n ^ (1 << i)

8. Check if i-th bit is set : (n >> i) & 1


XOR OF A GIVEN RANGE
First consider a simple case where given range is of form [1 , n]

Approach 1: Brute force

Time complexity : O(n)

Space complexity : O(1)


Approach 2: Observation

Number Binary form XOR from 1 to i

1 0001 [0001]

2 0010 [0011]

3 0011 [0000]

4 1000 [0100] <----- Equals to n

5 0101 [0001]

6 0110 [0111]

7 0111 [0000] <----- We get 0

8 1000 [1000] <----- Equals to n


Optimized code :

Time complexity : O(1)

Space complexity : O(1)


GENERAL CASE :
When the given range is of form [L, R].
We can use properties of XOR and previous observation to efficiently
calculate it

1. Calculate XOR from 1 to R

2. Calculate XOR from 1 to L-1

3. XOR the values obtained in step 1 and 2 to get the actual answer
QUESTIONS

1. Problem - 579A - Codeforces

2. Single Number - LeetCode

3. Problem - 1559A - Codeforces


PRACTICE PROLEMS
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

You might also like