Skip to content

Commit 40622f1

Browse files
Update bitwise-operator.md
1 parent 0429c4a commit 40622f1

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

lessons/bitwise-operator.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ order: "10B"
55
section: "Maths for DSA"
66
description: "learn maths required in DSA"
77
---
8+
In this lesson, we are going to learn about the bit-wise operators, and some Bit-manipulation techniques to get optimized solutions. These concepts are very important from competitive programming as well as interviews point of view. Okay, so let's learn these techniques:
89

9-
# Bit Manipulation
10+
## Bit Manipulation
1011

11-
## What is Bit Manipulation?
1212
Bit manipulation is the process of applying logical operations on a sequence of bits, the smallest form of data in a computer, to achieve a required result. Bit manipulation has constant time complexity and process in parallel, meaning it is very efficient on all systems.
1313

1414
Most programming languages will have you work with abstractions, like objects or variables, rather than the bits they represent. However, direct bit manipulation is needed to improve performance and reduce error in certain situations.
@@ -30,7 +30,7 @@ Bit manipulation is also a common topic in coding interviews, especially with FA
3030
If you’re applying for a role that will work with embedded systems or other low-level systems, you’ll encounter more bit questions. In short, the closer your role is to machine level, the more bit manipulation questions you’ll encounter.
3131

3232
The best way to prepare for bit manipulation questions is to practice using each bitwise operator and brush up on your binary to decimal conversions.
33-
# Bitwise Operator In Java
33+
# Bitwise Operators In Java
3434
You are no doubt familiar with the arithmetic operators such as + - * / or %. You are also aware of logical operators such as & or |. Turns out there is another, a slightly less known set of operators, which manipulate numbers on bit level. Internally, every number is stored in a binary format - that is 0 and 1. Bitwise operators are used for performing manipulation of bits of a number. These can be used with any type of integer data types (char, short, int, etc).
3535

3636
These operators can be performed on integer types -
@@ -40,7 +40,8 @@ These operators can be performed on integer types -
4040
* int (32 bit)
4141
* long (64 bit), and even
4242
* char (16 bit)
43-
43+
44+
Now let's see its type one by one:
4445
## 1. Unary bitwise complement [~]
4546
This fancy name basically means bit negation. It takes every single bit of the number and flips its value,i.e, ~0 becomes 1 and vice versa. It is the 1's complement of the number. Unary means that it needs just one operand. The operator is `~` and it is just placed before the number:
4647
```java
@@ -146,7 +147,7 @@ a= 5
146147
## 5. Signed Left Shift [<<]
147148
Signed Left Shift takes two operands. It takes the bit pattern of the first operand and shifts it to the left by the number of places given by the second operand. For example 5 << 3: What happens in this case - Every bit in the binary representation of the integer 5 is shifted by 3 positions to the left. All the places on the left are padded by zeros. That is: `00000101` becomes `00101000`.
148149

149-
You can note that the integer result of 5 << 3 is 40. That shows that shifting a number by one is equivalent to multiplying it by 2, or more generally left shifting a number by n positions is equivalent to multiplication by `2^n` .
150+
You can note that the integer result of 5 << 3 is 40. That shows that shifting a number by one is equivalent to multiplying it by 2, or more generally left shifting a number by n positions is equivalent to multiplication by `2^n`. In this case, it is 5*2^3 = 40.
150151

151152
* Even though you can use shifting of byte, short or char, they are promoted to 32-bit integer before the shifting
152153
* Bit-shift operators never throw an exception
@@ -178,22 +179,33 @@ Unlike the signed shift, the unsigned one does not take sign bits into considera
178179
a = 10
179180
a>>>1 = 5
180181

181-
Example 2:
182182
a = -10
183183
a>>>1 = 2147483643
184184
DOES NOT preserve the sign bit.
185185
```
186+
This operator shifts the first operand to the specified number of bits to the right. Excess bits shifted off to the right are `discarded`. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is `always non-negative`. Unlike the other bitwise operators, zero-fill right shift returns an unsigned 32-bit integer.
187+
188+
For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, 9 >>> 2 yields 2, the same as 9 >> 2:
186189
## 8. Unsigned Left Shift [<<<]
187190
Unlike unsigned Right Shift, there is no “<<<” operator in Java, because the logical (<<) and arithmetic left-shift (<<<) operations are identical.
191+
```java
192+
9 (base 10): 00000000000000000000000000001001 (base 2)
193+
9 >>> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
194+
```
195+
However, this is not the case for negative numbers. For example, -9 >>> 2 yields 1073741821, which is different than -9 >> 2 (which yields -3):
196+
```java
197+
-9 (base 10): 11111111111111111111111111110111 (base 2)
198+
-9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)
199+
```
188200

189201
|Operator |Example |Is equivalent to |
190202
|----------|------------|------------------|
191-
|OR= |OR= 5 |x = x OR 5 |
192-
|^= |x ^= 5 |x = x ^ 5 |
203+
|OR= |x OR= 5 |x = x OR 5 |
204+
|^= |x ^= 5 |x = x ^ 5 |
193205
|&= |x &= 5 |x = x & 5 |
194-
|<<= |x <<= 5 |x = x << 5 |
195-
|>>= |x >>= 5 |x = x >> 5 |
196-
|>>>= |x >>>= 5 |x = x >>> 5 |
206+
|<<= |x <<= 5 |x = x << 5 |
207+
|>>= |x >>= 5 |x = x >> 5 |
208+
|>>>= |x >>>= 5 |x = x >>> 5 |
197209

198210
```java
199211
// Java program to illustrate
@@ -226,11 +238,11 @@ a<<2 = 20
226238
b>>2 = -3
227239
b>>>2 = 1073741821
228240
```
229-
# Bitwise Tricks And Some Questions
241+
## Bitwise Tricks And Some Questions
230242
Now, let’s look at a few tricks you can do using bitwise operators.
231243

232244
These are often used as interview questions to check if you’ve reviewed basic bit manipulation and can apply it to day-to-day coding tasks.
233-
## Q1. Check for EVEN / ODD
245+
### Q1. Check for EVEN / ODD
234246
To check a number is even or odd, we need to look at the number in its binary form first, lets take an eg, 4 ,i.e, `0100`, if we look carefully then we will find out that the 1 present in the number is at `power of 2` position, lets take another example now, 6 ,i.e, `0110`, here both the 1's are at `power of 2` position and now we can conclude that in binary form if the number is at power of 2 position the it is even. Lets check for odd no. now, eg, 5 ,i.e, `0101` here the least significant digit(or `LSD`) is `not` at the `power of 2` position and thus it is odd.
235247

236248
Now, lets look how to solve this,
@@ -242,7 +254,7 @@ if (x & 1 ) == 0
242254
else
243255
return false;
244256
```
245-
## Q2. Convert characters to uppercase / lowercase
257+
### Q2. Convert characters to uppercase / lowercase
246258
This trick tests your knowledge of uppercase and lowercase characters in binary. You can convert any character, `ch`, to the opposite case using `ch ^= 32`.
247259

248260
This is because the binary representation of lowercase and uppercase letters are nearly identical, with only 1 bit of difference.
@@ -285,7 +297,7 @@ public class Test
285297
Toggle case: cHErRy
286298
Original string: CheRrY
287299
```
288-
## Q3. Find Number of Setbits
300+
### Q3. Find Number of Setbits
289301
```java
290302
class CountSetBit {
291303
private static int helper(int n) {
@@ -312,7 +324,7 @@ In this approach, we count only the set bits. So,
312324

313325
* If a number has 2 set bits, then the while loop runs two times.
314326
* If a number has 4 set bits, then the while loop runs four times.
315-
## Q4. Single Number
327+
### Q4. Single Number
316328
You are given an array in which every number appears twice except one number, return that number.
317329
```java
318330
class SingleNumber {
@@ -341,7 +353,7 @@ This solution relies on the following logic:
341353
* For n numbers, the below math can be applied: a ^ b ^ a = (a ^ a) ^ b = 0 ^ b = b
342354

343355
Therefore, we can XOR all bits together to find the unique number.
344-
## Q5. Get First Set Bit
356+
### Q5. Get First Set Bit
345357
```java
346358
class FirstSetBitPosition {
347359
private static int helper(int n) {

0 commit comments

Comments
 (0)