From 2e77b6823dc58d327f2babe03ede2fadd9635acb Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 18:45:31 +0530 Subject: [PATCH 01/16] Create a.md --- Bit Manipulation/a.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Bit Manipulation/a.md diff --git a/Bit Manipulation/a.md b/Bit Manipulation/a.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Bit Manipulation/a.md @@ -0,0 +1 @@ + From 3845ba0e9e137311f150a439811b005955200680 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 19:18:16 +0530 Subject: [PATCH 02/16] Create Bitwise AND.md --- Bit Manipulation/Bitwise AND.md | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Bit Manipulation/Bitwise AND.md diff --git a/Bit Manipulation/Bitwise AND.md b/Bit Manipulation/Bitwise AND.md new file mode 100644 index 0000000..5d98fa8 --- /dev/null +++ b/Bit Manipulation/Bitwise AND.md @@ -0,0 +1,36 @@ +### Bitwise AND Operation +The bitwise AND operation is a fundamental operation in computer programming that works on individual bits of binary numbers. + +In this operation, if both input bits are 1, the resulting bit will be 1. Otherwise, if either or both of the input bits are 0, the corresponding resulting bit will be 0. Consequently, the bitwise AND operation can be seen as a way to extract common 1-bits from two binary numbers while setting all other bits to 0. + +Truth table for **Bitwise AND** Operation:
+

+ Image +

+ +Example:
+If A=10100, B=10101, then +

+ Image +

+ +## Code: +```cpp +#include +using namespace std; +int main(){ + int a=5, b=4; + int c=a&b; + cout< Date: Mon, 24 Jul 2023 19:19:06 +0530 Subject: [PATCH 03/16] Create Bitwise OR.md --- Bit Manipulation/Bitwise OR.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Bit Manipulation/Bitwise OR.md diff --git a/Bit Manipulation/Bitwise OR.md b/Bit Manipulation/Bitwise OR.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Bit Manipulation/Bitwise OR.md @@ -0,0 +1 @@ + From 8ffadb1669f86228e242e9efa4682ae7247cd7a3 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 19:26:01 +0530 Subject: [PATCH 04/16] Update Bitwise OR.md --- Bit Manipulation/Bitwise OR.md | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Bit Manipulation/Bitwise OR.md b/Bit Manipulation/Bitwise OR.md index 8b13789..6e057a1 100644 --- a/Bit Manipulation/Bitwise OR.md +++ b/Bit Manipulation/Bitwise OR.md @@ -1 +1,36 @@ +### Bitwise OR Operation +The bitwise OR operation is a fundamental operation in computer programming that works on individual bits of binary numbers. +In this operation, if at least one of the input bits is 1, the resulting bit will be 1. Only when both input bits are 0 will the corresponding resulting bit be 0. Consequently, the bitwise OR operation can be seen as a way to combine the 1-bits from two binary numbers, while setting all other bits to 0 if they are both 0. + +Truth table for **Bitwise OR** Operation:
+

+ Image +

+ +Example:
+If A=10100, B=10101, then +

+ Image +

+ +## Code: +```cpp +#include +using namespace std; +int main(){ + int a=5, b=4; + int c=a|b; + cout< Date: Mon, 24 Jul 2023 19:28:50 +0530 Subject: [PATCH 05/16] Update Bitwise OR.md --- Bit Manipulation/Bitwise OR.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bit Manipulation/Bitwise OR.md b/Bit Manipulation/Bitwise OR.md index 6e057a1..ea8c88f 100644 --- a/Bit Manipulation/Bitwise OR.md +++ b/Bit Manipulation/Bitwise OR.md @@ -5,13 +5,13 @@ In this operation, if at least one of the input bits is 1, the resulting bit wil Truth table for **Bitwise OR** Operation:

- Image + Image

Example:
If A=10100, B=10101, then

- Image + Image

## Code: From 987ec609303727dabfa6f5c4445b5ef6890e1365 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 19:29:57 +0530 Subject: [PATCH 06/16] Create Bitwise NOT.md --- Bit Manipulation/Bitwise NOT.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Bit Manipulation/Bitwise NOT.md diff --git a/Bit Manipulation/Bitwise NOT.md b/Bit Manipulation/Bitwise NOT.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Bit Manipulation/Bitwise NOT.md @@ -0,0 +1 @@ + From e6bd82c49eabeab2cfba9209240afccc03c46310 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 19:46:25 +0530 Subject: [PATCH 07/16] Update Bitwise NOT.md --- Bit Manipulation/Bitwise NOT.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Bit Manipulation/Bitwise NOT.md b/Bit Manipulation/Bitwise NOT.md index 8b13789..d56c77f 100644 --- a/Bit Manipulation/Bitwise NOT.md +++ b/Bit Manipulation/Bitwise NOT.md @@ -1 +1,34 @@ +### Bitwise NOT Operation +The bitwise NOT operation is a unary operation that works on individual bits of a binary number. +Also known as bitwise complement, it flips each bit of the input number, changing 0s to 1s and 1s to 0s. The result is a new binary number with the complement of each bit from the original number. +Truth table for **Bitwise NOT** Operation:
+

+ Image +

+ +Example:
+If A=10100, then +

+ Image +

+ +## Code: +```cpp +#include +using namespace std; +int main(){ + int a=5, b=~a; + cout< Date: Mon, 24 Jul 2023 19:55:35 +0530 Subject: [PATCH 08/16] Create Bitwise Shift Operation.md --- Bit Manipulation/Bitwise Shift Operation.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Bit Manipulation/Bitwise Shift Operation.md diff --git a/Bit Manipulation/Bitwise Shift Operation.md b/Bit Manipulation/Bitwise Shift Operation.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Bit Manipulation/Bitwise Shift Operation.md @@ -0,0 +1 @@ + From ba1929de85fe7e906ca2a8aca370d498351e8eb5 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:06:09 +0530 Subject: [PATCH 09/16] Update Bitwise Shift Operation.md --- Bit Manipulation/Bitwise Shift Operation.md | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Bit Manipulation/Bitwise Shift Operation.md b/Bit Manipulation/Bitwise Shift Operation.md index 8b13789..a6d6c6e 100644 --- a/Bit Manipulation/Bitwise Shift Operation.md +++ b/Bit Manipulation/Bitwise Shift Operation.md @@ -1 +1,44 @@ +### Bitwise Shift Operation +The bitwise shift operations are used to move the individual bits of a binary number left or right. There are two types of bitwise shift operations: bitwise left shift (<<) and bitwise right shift (>>). These operations are often used in low-level programming to perform efficient multiplication and division by powers of 2, as well as other bit manipulation tasks. +1. **Bitwise Left Shift (<<)**: + The bitwise left shift operation moves all the bits of a binary number to the left by a specified number of positions. The leftmost bits that are shifted out are discarded, and the rightmost bits are filled with zeros. + + For example, if we perform a left shift of 2 positions on the binary number 00101110, the result will be: + + Original binary number: 00101110
+ Left shift by 2: 10111000 + +2. **Bitwise Right Shift (>>)**: + The bitwise right shift operation moves all the bits of a binary number to the right by a specified number of positions. The rightmost bits that are shifted out are discarded, and the leftmost bits are filled based on the sign bit (for signed integers) or with zeros (for unsigned integers). + + For example, if we perform a right shift of 3 positions on the binary number 11011010, the result will be: + + Original binary number: 11011010
+ Right shift by 3: 00011011 + +## Code: +```cpp +#include +using namespace std; +int main(){ + int a=5; + a=a>>1; + int b=7; + b=b<<1; + cout<>1= 2 +14 // 7<<1= 14 + +``` +## Complexity: +``` +Time Complexity: O(1) +Auxiliary Space: O(1) +``` From d68e3a5ec5ed740ab858063221c9cb96264977d9 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:06:38 +0530 Subject: [PATCH 10/16] Update Bitwise NOT.md --- Bit Manipulation/Bitwise NOT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit Manipulation/Bitwise NOT.md b/Bit Manipulation/Bitwise NOT.md index d56c77f..cde8cce 100644 --- a/Bit Manipulation/Bitwise NOT.md +++ b/Bit Manipulation/Bitwise NOT.md @@ -19,7 +19,7 @@ If A=10100, then using namespace std; int main(){ int a=5, b=~a; - cout< Date: Mon, 24 Jul 2023 20:11:51 +0530 Subject: [PATCH 11/16] Update Bitwise Shift Operation.md --- Bit Manipulation/Bitwise Shift Operation.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Bit Manipulation/Bitwise Shift Operation.md b/Bit Manipulation/Bitwise Shift Operation.md index a6d6c6e..4264e9e 100644 --- a/Bit Manipulation/Bitwise Shift Operation.md +++ b/Bit Manipulation/Bitwise Shift Operation.md @@ -5,17 +5,19 @@ The bitwise shift operations are used to move the individual bits of a binary nu The bitwise left shift operation moves all the bits of a binary number to the left by a specified number of positions. The leftmost bits that are shifted out are discarded, and the rightmost bits are filled with zeros. For example, if we perform a left shift of 2 positions on the binary number 00101110, the result will be: - - Original binary number: 00101110
+ ```cpp + Original binary number: 00101110 Left shift by 2: 10111000 + ``` 2. **Bitwise Right Shift (>>)**: The bitwise right shift operation moves all the bits of a binary number to the right by a specified number of positions. The rightmost bits that are shifted out are discarded, and the leftmost bits are filled based on the sign bit (for signed integers) or with zeros (for unsigned integers). For example, if we perform a right shift of 3 positions on the binary number 11011010, the result will be: - - Original binary number: 11011010
+ ```cpp + Original binary number: 11011010 Right shift by 3: 00011011 + ``` ## Code: ```cpp From f416ecd3ed73b51fa8db8092c82c63c5f59fb3f1 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:14:34 +0530 Subject: [PATCH 12/16] Create Check ith bit set.md --- Bit Manipulation/Check ith bit set.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Bit Manipulation/Check ith bit set.md diff --git a/Bit Manipulation/Check ith bit set.md b/Bit Manipulation/Check ith bit set.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Bit Manipulation/Check ith bit set.md @@ -0,0 +1 @@ + From 6ee344a4a7384f9191061d642f69202f55660835 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:15:20 +0530 Subject: [PATCH 13/16] Update and rename Check ith bit set.md to Check kth bit set.md --- Bit Manipulation/Check ith bit set.md | 1 - Bit Manipulation/Check kth bit set.md | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 Bit Manipulation/Check ith bit set.md create mode 100644 Bit Manipulation/Check kth bit set.md diff --git a/Bit Manipulation/Check ith bit set.md b/Bit Manipulation/Check ith bit set.md deleted file mode 100644 index 8b13789..0000000 --- a/Bit Manipulation/Check ith bit set.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Bit Manipulation/Check kth bit set.md b/Bit Manipulation/Check kth bit set.md new file mode 100644 index 0000000..a6a08cd --- /dev/null +++ b/Bit Manipulation/Check kth bit set.md @@ -0,0 +1 @@ +### Check kth bit set or not From 8297c25d707333c3ca0b18dcebea7dbbdce95acb Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:18:15 +0530 Subject: [PATCH 14/16] Update Check kth bit set.md --- Bit Manipulation/Check kth bit set.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Bit Manipulation/Check kth bit set.md b/Bit Manipulation/Check kth bit set.md index a6a08cd..63cceca 100644 --- a/Bit Manipulation/Check kth bit set.md +++ b/Bit Manipulation/Check kth bit set.md @@ -1 +1,6 @@ ### Check kth bit set or not +In order to check whether the kth bit is set or not, we can perform a simple bitwise shift operations:
+1. **Left Shift Operation** : To check the kth bit, you can left-shift the number by (k-1) positions and then perform a bitwise AND operation with 1. If the result is non-zero, it means the kth bit is set (1); otherwise, the kth bit is not set (0). + +2. +3. From 0ad78c0119a07b3edfaa5d2413365b608eb93d48 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:24:48 +0530 Subject: [PATCH 15/16] Update Check kth bit set.md --- Bit Manipulation/Check kth bit set.md | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Bit Manipulation/Check kth bit set.md b/Bit Manipulation/Check kth bit set.md index 63cceca..c89620d 100644 --- a/Bit Manipulation/Check kth bit set.md +++ b/Bit Manipulation/Check kth bit set.md @@ -1,6 +1,31 @@ ### Check kth bit set or not -In order to check whether the kth bit is set or not, we can perform a simple bitwise shift operations:
-1. **Left Shift Operation** : To check the kth bit, you can left-shift the number by (k-1) positions and then perform a bitwise AND operation with 1. If the result is non-zero, it means the kth bit is set (1); otherwise, the kth bit is not set (0). +In order to check whether the kth bit is set or not, we use below 2 methods:
+1. **Right Shift Operation** : To check the kth bit, you can left-shift the number by (k-1) positions and then perform a bitwise AND operation with 1.
+If the result is non-zero, it means the kth bit is set (1); otherwise, the kth bit is not set (0).
+```cpp +#include +using namespace std; +int main() { + int n = 5, k = 3; + while(k--){ + n=n>>1; + } + if ((n & 1) != 0) { + cout << "YES" << endl; + } else { + cout << "NO" << endl; + } +} +``` -2. -3. +2. **Bitwise Mask** : Alternatively, you can create a bitmask with only the kth bit set (1), and then perform a bitwise AND operation with the number. If the result is non-zero, the kth bit is set (1); otherwise, it is not set (0).
+ +```cpp +#include +using namespace std; +int main() { + int n = 5, k = 3; + int m = 1 << (k - 1); + return (n & m) != 0; +} +``` From 1d5fb0d8e1cf7fc8ab19e56f8ac331d25657cf9f Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Singh Date: Mon, 24 Jul 2023 20:25:21 +0530 Subject: [PATCH 16/16] Delete a.md --- Bit Manipulation/a.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Bit Manipulation/a.md diff --git a/Bit Manipulation/a.md b/Bit Manipulation/a.md deleted file mode 100644 index 8b13789..0000000 --- a/Bit Manipulation/a.md +++ /dev/null @@ -1 +0,0 @@ -