Bit Manipulation-DSA Flashcards

1
Q

how to find binary equivalent of any decimal number

A
  1. divide it by 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to convert binary to decimal

A
  1. 1101 to decimal
  2. 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…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

write a function that will take decimal and return its binary

A

string count2Binary(int n){
res = “”

while(n != 1){
if (n % 2 == 1) res += “1”

else res += “0”;

n = n/2;

}

reverse(res);

return res;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write a function that takes binary and return its decimal equivalent

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what happens when we write
int x = 13

A

13 gets converted to binary, but 13 requires only 4 bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

13 requires only 13 bits, what happens with other remaining bits

A

remaining bits will be stored with zero’s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how many bits required to store int

A

32 bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if x is stored as binary, when we print int, how does we get int, like 13 in this example

A

reverse conversion of binary / bits happens, 13 gets converted to binary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

int is 32 bits, how many bits for long long

A

64 bits

===

most of the problems of long long can be solved if you know int concept

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is ones complement.
Que. what is ones complement of 13

A

13 => (1101)2 base 2
flip all the bits
=> 0010

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is twos complement

A

step 1: figure out ones complement of number
step 2: add one to it

Que. what is 13’s two complement

  1. 13 => (1101)base 2
  2. ones complement => 0010
  3. add one => 0011
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

AND operator explain

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

OR operator

A

one true => true
all false => false

x = 13 | 7

1 1 0 1
0 1 1 1
———-
1 1 1 1 => 15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

XOR

A

number of 1s => odd -> one
number of 1s => even -> zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

right shift operator

A

x = 13&raquo_space; 1

1101

last one goes off the clip
0000000…110

=> o/p will be 6

x = 13&raquo_space; 2
last two numbers, will go off the clip
000000…11

o/p => 3

x = 13&raquo_space; 4

00000000….1101
0000000000000

all of them go off the clip

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

formula for right shift

A

x&raquo_space; k = ( x / 2 power k )

x divide by 2 and twos power will be K

17
Q

how does computer stores minus and positive numbers, particular positive / negative thing

A

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

18
Q

What bit for positive

A

zero is used for positive sign

19
Q

what bit is used for negative

A

one is used for negative

20
Q

x = 13, how this will be stored

A

0 0 ……. 1101
|
|

for sign, positive

21
Q

x = -13, how this will be stored, how does the computer stores it

A

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

22
Q

what is largest integer that int data type can save

A

last is reserved for sign, 31 bits are remaining

2 power 31 - 1

this is the largest value we can store in int variable

23
Q

what is INT_MAX you seen in code

A

INT_MAX is that largest value

24
Q

what is smallest number that int variable data type can save

A

-2 power 31

INT_MIN

=> 1 0 0 0 0 …….. 0

25
Q

what is left shift operator, how to represent left shift operator

A

«

26
Q

13 &laquo_space;1

A

=13 &laquo_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
27
Q

how does it work mathematically

A

num &laquo_space;k
= num * 2 power k

13 &laquo_space;1
= 13 * 2 power 1

28
Q

when does overflow causes

A

34:01

29
Q

NOT ~ operator

A
30
Q
A