-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitManipulation
More file actions
135 lines (99 loc) · 4.3 KB
/
BitManipulation
File metadata and controls
135 lines (99 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Left Shift operator - << - Shifts bits to left towards MSB(most significant bit)
n << 1 --- i) it will shift all the bits by one towards MSB
ii) n will become n*2toThePower(1)
iii) for eg.- n << x , it will multiply n with 2 to the power x
iv) Binary rep of 60 is 0000 0000 0011 1100, after applying << 1 , it will become 0000 0000 0111 1000 (120) i.e. getting
multiplied by 2 to the power 1.
Right Shift Operator - >> - Shifts bits to right towards LSB(least significant bit)
n >> x ---- same as above, only diff will be instead of multiplying by 2 to the power x here it will divide by 2 to the power x.
if MSB is 1 it will add 1 from right else it will add 0
n >>> x --- in triple op always 0 is added
for eg - Binary rep of 60 is 0000 0000 0011 1100, after applying >> 1 , it will become 0000 0000 0001 1110 (30) i.e. getting divided
by 2 to the power 1.
AND op(&) - gives 1 if both bits are 1.
n & 0 = 0, n & 1 = n
OR op(|) - gives 1 if either of the bits is 1.
n | 0 = n, n | 1 = 1
XOR op(^) - same bits - 0, diff bits - 1.
n ^ 1 = ~n, n ^ 0 = n , where ~ is toggle op
TO OFF A BIT = & 0 (and with 0)
TO ON A BIT = | 1 (or with 1)
TO TOGGLE A BIT = ^ 1 (xor with 1)
=> CHECK whether a kth bit is set or not ---- it will check whether kth bit of n is set or not(set the kth bit)
Number is n
let k = 3
int mask = 1 << k; // i.e. 1 << 3 (0000 0000 0000 1000) // only kth bit will be one rest will be zero
if((n & mask) == 0) SOP("NOT SET");
else SOP("SET");
=> TURN ON Kth BIT --- it will turn kth bit of n ON i.e. it will become 1 or set ( | with mask(1 << k))
Number is n
let k = 3
SOP(Interger.toBinaryString(n)); //before turning on the kth bit
int mask = 1 << k; // i.e. 1 << 3 (0000 0000 0000 1000) // only kth bit will be one rest will be zero
n = n | mask;
SOP(Interger.toBinaryString(n)); //after turning on the kth bit
=> TURN OFF Kth BIT --- it will turn kth bit on n OFF i.e. it'll become 0 or not set ( & with ~mask(~(1 <<k)))
Number is n
let k = 3
SOP(Interger.toBinaryString(n)); //before turning off the kth bit
int mask = 1 << k; // i.e. 1 << 3 (0000 0000 0000 1000) // only kth bit will be one rest will be zero
int comp_mask = ~mask;
n = n & comp_mask;
SOP(Interger.toBinaryString(n)); //after turning off the kth bit
=> TOGGLE Kth BIT ---- it will change the kth bit from 0 to 1 or vice-versa ( ^ with mask (1 << k))
Number is n
let k = 3
SOP(Interger.toBinaryString(n)); //before toggling the kth bit
int mask = 1 << k; // i.e. 1 << 3 (0000 0000 0000 1000) // only kth bit will be one rest will be zero
n = n ^ mask;
SOP(Interger.toBinaryString(n)); //after toggling the kth bit
=> CONVERT INTEGER TO BINARY (Without wrapper class Interger.toBinaryString())
int n = 3456789567;
for(int i=31;i>=0;i--){
int mask = 1 << i;
if((n & mask)==0)
SOP(0);
else
SOP(1);
}
=> Given an array, all no. appear even number of times except two nos. Return both the numbers. (req. TC - O(n) SC - O(1))
Array - {4, 1, 2, 3, 2, 1, 5, 4}
nos will be 3 and 5
If we take XOR of all nos., then result will be 3^5 = 6
3 - 011
5 - 101
3^5 - 110 => 6
Now we will check all the nos of array across any set bit position of XOR of whole array i.e. either 2nd or 3rd bit postion from right in case of 110
It will divide the array in two set of arrays having one odd occuring in each set
now we'll take the xor of both individually to get the answer
CODE ---
public class Main
{
static boolean kthIsSet(int k, int n){
return (n & (1 << k)) > 0;
}
public static void main(String[] args) {
int[] arr = {4, 1, 2, 3, 2, 1, 5, 4};
int xorOfTwo = 0; //3^5
for(int i = 0; i < arr.length; i++){
xorOfTwo = xorOfTwo ^ arr[i];
}
int pos = 0;
for(int i=0;i<32;i++){
if(kthIsSet(i, xorOfTwo)){
pos = i;
break;
}
}
int firstNum = 0, secondNum = 0;
for(int i = 0; i < arr.length; i++){
if(kthIsSet(pos, arr[i])){
firstNum = firstNum ^ arr[i];
}
else{
secondNum = secondNum ^ arr[i];
}
}
System.out.println("firstNum: "+firstNum+" secondNum: "+secondNum);
}
}