23. Bit manipulation
Note:-
1. decimal to binary & binary to decimal conversion
----------------
2. int type variable stores data upto 32 bits, so if value is of x bits only then remaining 32-x bits are 0
-----------------
3. One's complement- flip all bits of the number in binary form
Two's complement- add 1 to the one's complement.
------------------
0 for (+) and 1 for (-)⭐
-------------------------
5. XOR: ⭐
1^1=0, 1^0=1
a^a = 0, a^0 = a
-------------------------
6. NOT:⭐
~x = 1s complement of x
in decimal form: (~x = -x-1)
converting ~x from binary to decimal:-
- if (MSB=0), then this is the required answer, just convert to decimal form
--------------------------
7.1 Right shift :-
fills empty leftmost bit based on sign⭐
n>>k = n / (2^k)
--------------------------
7.2 Left shift :-
fill the empty rightmost bit with 0⭐
n<<k = n* (2^k)
--------------------------
8. (INT_MAX << 1) => overflow
10. no. of bits in a number = logN(base 2)⭐
------------------------------------------------------------------------------------------------------------
1. using strings
2. using vectors
---------------------------------------------------------------------------------------------------------------
Note- BITSET (ignore for interviews)
1. bitset<x>b(n) displays binary form of n in x-bit
2. b[0] gives LSB
3. cout<< b , prints binary form of n, from MSB to LSB
b.count() => count set bits
b.flip(i) => flips i-th bit
b.test(i) => if true then i-th bit is set
5. fix size array of bits- max: 1e7
------------------------------------------------------------------------------------------------------------
(w/o temp)
-------------------------------------------------------------------------------------------------------------
2. Check if i-th bit is set-
Brute- time: logN, space: logN
convert given decimal number to binary form & then check if i-th bit is set or not
-------------------------
Better- time: O(1) space: O(N)
using bitset
b[i] will return value at i-th bit position
------------------------
Optimal- time: O(1) space: O(1)
using bit manipulation
4. Toggle i-th bit-⭐
take xor with 1 at the i-th position
as XOR with 1 toggles the bit
3. Even number: only 1 set bit⭐
4. Odd number: LSB = 1, always
6. we assume 0 based indexing from LSB
5. Clear rightmost set bit⭐
take AND of (n) with (n-1)
--------------------------------------------------------------------------------------------------------------
(check if number is even or not)
hence if we clear that bit, then number reduces to 0
modifying the decimal to binary conversion logic to count set bits
using popcount function which directly returns number of set bits in a number
repeatedly remove rightmost set bit and keep a track of number of set bits using a counter
---------------------------------------------------------------------------------------------------------------
answer = number of set bits in A^B
(using Bit Masking)⭐
(n <= 32)
Power set = all subsets = all subsequences
---------------------------------
since, no. of subsets = 2^n
Hence, we can associate each subset with a number from 0 to (2^n)-1, each number representing a bit mask of size n
e.g., in {a,b,c} total subsets are 2^3 = 8, hence we create 8 bit masks each of size 3,
where for example- {a,c} is represented as 101
1. 0 se (2^n)-1 tak saare numbers lelo
2. har number mein jaha jaha set bit hai, vo wale corresponding elements lelo array se for current subset
- truncate the decimal part to 0 and then return the quotient
- Clamp result if it exceeds 32 bit signed integers limits
Brute- time: O(n), where n= dividend
Division is REPEATED SUBTRACTION of divisor from dividend
using SHIFT SUBTRACTION⭐
At any iteration this qty is max stuff which can be subtracted from current dividend
Note-⭐
XOR in [1,n] follows a pattern based on n%4
xor in [L,R] = (xor in [1,L-1] ) ^ (xor in [1,R])
1. for every bit position, traverse the array & count how many numbers have this bit set
2. this bit will be 1 for the answer only if, the count is not a multiple of 3
Extracting rightmost set bit-
take xor of (original number) with (this number having rightmost set bit cleared)
2. (A^B!=0) => there is atleast one bit where both A & B differ (or, there is atleast one set bit in A^B)
4. Split array into 2 groups- one having this bit=1, other having this bit=0
5. A and B go to different groups
6. Now xor inside each of the groups, thus we get A and B
----------
Concepts used :
- clear rightmost set bit
- extract rightmost set bit
- if i-th bit is set or not
---------
Comments
Post a Comment