Bit Manipulation-DSA Flashcards
how to find binary equivalent of any decimal number
- divide it by 2
how to convert binary to decimal
- 1101 to decimal
- start from right, move towards left
3.
(that number * 2) raise to the power of that index + (that number * 2) raise to the power of that index + (that number * 2) raise to the power of that index
add all numbers
O/p is in decimal…
write a function that will take decimal and return its binary
string count2Binary(int n){
res = “”
while(n != 1){
if (n % 2 == 1) res += “1”
else res += “0”;
n = n/2;
}
reverse(res);
return res;
}
write a function that takes binary and return its decimal equivalent
int convertToDecimal(string x){
int len = x.length();
for(i =len - 1; i–){
if(n[i] == ‘1’)
num = num + p2;
p2 = p2 * 2;
}
return num;
}
===
TC => O(len)
SC => O(1)
what happens when we write
int x = 13
13 gets converted to binary, but 13 requires only 4 bits
13 requires only 13 bits, what happens with other remaining bits
remaining bits will be stored with zero’s
how many bits required to store int
32 bits
if x is stored as binary, when we print int, how does we get int, like 13 in this example
reverse conversion of binary / bits happens, 13 gets converted to binary
int is 32 bits, how many bits for long long
64 bits
===
most of the problems of long long can be solved if you know int concept
what is ones complement.
Que. what is ones complement of 13
13 => (1101)2 base 2
flip all the bits
=> 0010
what is twos complement
step 1: figure out ones complement of number
step 2: add one to it
Que. what is 13’s two complement
- 13 => (1101)base 2
- ones complement => 0010
- add one => 0011
AND operator explain
all true => true
one false => false
x = 13 & 7
= 5
1 1 0 1 => 13
0 1 1 1 => 7
———-
0 1 0 1 => 5
OR operator
one true => true
all false => false
x = 13 | 7
1 1 0 1
0 1 1 1
———-
1 1 1 1 => 15
XOR
number of 1s => odd -> one
number of 1s => even -> zero
right shift operator
x = 13»_space; 1
1101
last one goes off the clip
0000000…110
=> o/p will be 6
x = 13»_space; 2
last two numbers, will go off the clip
000000…11
o/p => 3
x = 13»_space; 4
00000000….1101
0000000000000
all of them go off the clip
formula for right shift
x»_space; k = ( x / 2 power k )
x divide by 2 and twos power will be K
how does computer stores minus and positive numbers, particular positive / negative thing
int are stored in 32 bits, and the first bit or the last bit if you count from end, 31st bit is reserved for sign
and rest of bits are used to store numbers
What bit for positive
zero is used for positive sign
what bit is used for negative
one is used for negative
x = 13, how this will be stored
0 0 ……. 1101
|
|
for sign, positive
x = -13, how this will be stored, how does the computer stores it
using two’s complement
first thing we do is we write down positive 13 bits
which is basically starting from 0.
0 0 0 0 ……. 1 1 0 1
now whenever computer has to store number into its negative form, computer converts it into it’s two’s complement
- to convert bits to its twos complement first we perform ones complement [flip the bits]
- in second step we add one to it
0000……1101
1111…….0010 => ones complement
0000…………..0 => add one
——————————————————-
11111……0011
|
|
|
the last bit represents negative sign
what is largest integer that int data type can save
last is reserved for sign, 31 bits are remaining
2 power 31 - 1
this is the largest value we can store in int variable
what is INT_MAX you seen in code
INT_MAX is that largest value
what is smallest number that int variable data type can save
-2 power 31
INT_MIN
=> 1 0 0 0 0 …….. 0
what is left shift operator, how to represent left shift operator
«
13 «_space;1
=13 «_space;1
=> 26
the last bit, or 31st bit goes off the clip
0 0 0 ………… 1 1 0 1
0 0 0 …………1 1 0 1 0
- the remaining space of first index bit is filled with zero bit, not matter what first bit was
- first i use to think that if zero is removed, then it gets added to first index position
how does it work mathematically
num «_space;k
= num * 2 power k
13 «_space;1
= 13 * 2 power 1
when does overflow causes
34:01
NOT ~ operator