Skip to content

Commit 1ac138e

Browse files
committed
Adds bitwise notes
1 parent 11fd1af commit 1ac138e

5 files changed

Lines changed: 171 additions & 2 deletions

File tree

Lectures/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
|Counting|[Lecture 13](data-structures/Lecture15.md)|
2020
|Dynamic Programming|[Lecture 14](data-structures/Lecture16.md)|
2121
|Greedy Algorithm|[Lecture 15](data-structures/Lecture17.md)|
22-
|Bitwise algorithm|[Algo Lecture 01](algorithm/Lecture01.md)|
22+
|Bit Magic -- Bitwise Algorithm|[Lecture 20](data-structures/Lecture20.md)|
2323

2424
## CS97SI Slides
2525

Lectures/data-structures/Lecture02-1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ for i in 0 to m-1
8181
Cij += aik*bkj
8282
```
8383

84-
![Matrices multiplications](../../images/matrices-multiplication.png)
84+
![Matrices-multiplications](../../images/matrices-multiplication.png)
8585
###### Source picture: mathisfun.com
8686

8787
* Multiplication of matrices is non-commutative which means A*B ≠ B*A.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
## Bitwise
2+
3+
* **Bitwise algorithms:** used to perform operatiosn at bit-level.
4+
5+
## Bitwise Operatos
6+
7+
* &(bitwise AND): takes two numbers as operands: Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. Suppose A = 5 and B = 3, therefore A & B = 1.
8+
* |(bitwise OR): Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1. Suppose A = 5 and B = 3, therefore A | B = 7.
9+
* ^ (bitwise XOR): takes to numbers and return the XOR results of the them
10+
* << (left shift): left shift the bits of the first operand, the second indicates the number of places to shif.
11+
* >> (right shift): right shift the bits of the first operand, the second indicates the number of places to shif.
12+
* ~ (bitwise NOT): it inverts all the bits. (Es: A = 4; ~A = 3)
13+
14+
## 'XOR' operator.
15+
1^1 = 0
16+
0^0 = 0
17+
1^0 = 1
18+
0^1 = 1
19+
20+
## Even and Odd
21+
22+
* Even: has first bit (from right hand side):set to 0;
23+
* Odd: has first bit (from right hand side): set to 1;
24+
25+
## Arithmetic operations:
26+
27+
* Divide by 2:
28+
```
29+
x = 10;
30+
x = x >> 1; // (x is now 5)80
31+
```
32+
* Multiply by 2:
33+
```
34+
x = 20;
35+
print(x << 1); // x is now 40
36+
print(x << 2); // x is now 80
37+
print(x << 3); // x is now 160
38+
39+
/*
40+
// Ex by: Geeks for geeks
41+
x = 18(00010010)
42+
x << 1 = 36 (00100100)
43+
*/
44+
```
45+
46+
* Find log base of a integer
47+
```
48+
int log2_calculator(int num)
49+
{
50+
int counter = 0;
51+
while( x>>=1 )
52+
counter++;
53+
// The number of times that we have applied the shift operator is the log2 of our number
54+
return res;
55+
}
56+
```
57+
58+
## Bit operations and problems
59+
60+
* Set n'th bit of a number:
61+
We need to shift 1 k times to its left and the perform bitwise OR operation with the number and result of left shift performed just before.
62+
```
63+
// function to set the kth bit
64+
int setKthBit()
65+
{
66+
int n = 4; // Integer were we need to set the nth bit
67+
int k =1; // Starting from the 0-based index at the right hand side, we will set the
68+
// bit at the first position (Position k)
69+
// kth bit of n is being set by this operation
70+
71+
// The result will not be 6
72+
// 4 in binary: 1 0 0
73+
// After have set the kth bit: 1 1 0
74+
return ((1 << k) | n);
75+
}
76+
```
77+
* Unseat n'th position of a number:
78+
```
79+
// Do & of n with a number with all set bits except
80+
// the k'th bit
81+
return (n & ~(1 << (k - 1)));
82+
```
83+
* Check if a bit at nth position is set (1) or unset(0)
84+
85+
1. Left shift given number 1 by k-1 to create
86+
a number that has only set bit as k-th bit.
87+
temp = 1 << (k-1)
88+
2. If bitwise AND of n and temp is non-zero,
89+
then result is SET else result is NOT SET.
90+
```
91+
bool isSet(int number, int position)
92+
{
93+
if (number & (1 << (position - 1)))
94+
return true;
95+
else
96+
return false;
97+
}
98+
```
99+
* Flip bits of a given number
100+
It can be done by a simple way, just simply subtract the number from the value obtained when all the bits are equal to 1 .
101+
102+
* Find most significant set bit in the given number
103+
104+
The most-significant bit in binary representation of a number is the highest ordered bit, that is it is the bit-position with highest value.
105+
106+
107+
* Given a number N, the task is to check whether the given number is a power of 2 or not
108+
```
109+
// Function to check if x is power of 2
110+
bool isPowerOfTwo(int number)
111+
{
112+
if(number==0)
113+
return false;
114+
115+
return (ceil(log2(number)) == floor(log2(number)));
116+
}
117+
```
118+
OR
119+
```
120+
/* Function to check if the given number is power of 2*/
121+
bool isPowerOfTwo (int number)
122+
{
123+
124+
return number && (!(number&(number-1)));
125+
}
126+
```

Leetcode/linked-list-cycle.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/linked-list-cycle/submissions/
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
if(head == NULL)
6+
return false;
7+
8+
ListNode *slow = head;
9+
ListNode *fast = head;
10+
11+
while(slow && fast && fast->next)
12+
{
13+
slow = slow->next;
14+
fast = fast->next->next;
15+
if(slow == fast)
16+
return true;
17+
}
18+
return false;
19+
}
20+
};

Leetcode/palindrome.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(int x) {
4+
string a = to_string(x);
5+
if(a.size() ==1)
6+
return true;
7+
if(a.size()==2)
8+
{
9+
if(a[0] == a[1])
10+
return true;
11+
return false;
12+
}
13+
14+
int end = a.size()-1;
15+
for(int i =0; i <a.size()/2; i++)
16+
{
17+
if(a[i] != a[end])
18+
return false;
19+
end--;
20+
}
21+
return true;
22+
}
23+
};

0 commit comments

Comments
 (0)