# [ACCEPTED]-What are bitwise operators?-boolean-logic

Since nobody has broached the subject of 38 why these are useful:

I use bitwise operations 37 a lot when working with flags. For example, if 36 you want to pass a series of flags to an 35 operation (say, `File.Open()`

, with Read mode and Write 34 mode both enabled), you could pass them 33 as a single value. This is accomplished 32 by assigning each possible flag it's own 31 bit in a bitset (byte, short, int, or long). For 30 example:

```
Read: 00000001
Write: 00000010
```

So if you want to pass read AND 29 write, you would pass (READ | WRITE) which 28 then combines the two into

```
00000011
```

Which then can 27 be decrypted on the other end like:

```
if ((flag & Read) != 0) { //...
```

which 26 checks

```
00000011 &
00000001
```

which returns

```
00000001
```

which is not 0, so the 25 flag does specify READ.

You can use XOR to 24 toggle various bits. I've used this when 23 using a flag to specify directional inputs 22 (Up, Down, Left, Right). For example, if 21 a sprite is moving horizontally, and I want 20 it to turn around:

```
Up: 00000001
Down: 00000010
Left: 00000100
Right: 00001000
Current: 00000100
```

I simply XOR the current 19 value with (LEFT | RIGHT) which will turn 18 LEFT off and RIGHT on, in this case.

Bit 17 Shifting is useful in several cases.

```
x << y
```

is the 16 same as

x * 2

^{y}

if you need to quickly multiply 15 by a power of two, but watch out for shifting 14 a 1-bit into the top bit - this makes the 13 number negative unless it's unsigned. It's 12 also useful when dealing with different 11 sizes of data. For example, reading an 10 integer from four bytes:

```
int val = (A << 24) | (B << 16) | (C << 8) | D;
```

Assuming that A 9 is the most-significant byte and D the least. It 8 would end up as:

```
A = 01000000
B = 00000101
C = 00101011
D = 11100011
val = 01000000 00000101 00101011 11100011
```

Colors are often stored 7 this way (with the most significant byte 6 either ignored or used as Alpha):

```
A = 255 = 11111111
R = 21 = 00010101
G = 255 = 11111111
B = 0 = 00000000
Color = 11111111 00010101 11111111 00000000
```

To find 5 the values again, just shift the bits to 4 the right until it's at the bottom, then 3 mask off the remaining higher-order bits:

```
Int Alpha = Color >> 24
Int Red = Color >> 16 & 0xFF
Int Green = Color >> 8 & 0xFF
Int Blue = Color & 0xFF
```

`0xFF`

is 2 the same as `11111111`

. So essentially, for Red, you 1 would be doing this:

```
Color >> 16 = (filled in 00000000 00000000)11111111 00010101 (removed 11111111 00000000)
00000000 00000000 11111111 00010101 &
00000000 00000000 00000000 11111111 =
00000000 00000000 00000000 00010101 (The original value)
```

It is worth noting that the single-bit truth 13 tables listed as other answers work on only 12 one or two input bits at a time. What happens 11 when you use integers, such as:

```
int x = 5 & 6;
```

The answer 10 lies in the binary expansion of each input:

```
5 = 0 0 0 0 0 1 0 1
& 6 = 0 0 0 0 0 1 1 0
---------------------
0 0 0 0 0 1 0 0
```

Each 9 pair of bits in each column is run through 8 the "AND" function to give the corresponding 7 output bit on the bottom line. So the answer 6 to the above expression is 4. The CPU has 5 done (in this example) 8 separate "AND" operations 4 in parallel, one for each column.

I mention 3 this because I still remember having this 2 "AHA!" moment when I learned about this 1 many years ago.

Bitwise operators are operators that work 13 on a bit at a time.

AND is 1 only if both 12 of its inputs are 1.

OR is 1 if one or more 11 of its inputs are 1.

XOR is 1 only if exactly 10 one of its inputs are 1.

NOT is 1 only if 9 its input are 0.

These can be best described 8 as truth tables. Inputs possibilities are 7 on the top and left, the resultant bit is 6 one of the four (two in the case of NOT 5 since it only has one input) values shown 4 at the intersection of the two inputs.

```
AND|0 1 OR|0 1
---+---- ---+----
0|0 0 0|0 1
1|0 1 1|1 1
XOR|0 1 NOT|0 1
---+---- ---+---
0|0 1 |1 0
1|1 0
```

One 3 example is if you only want the lower 4 2 bits of an integer, you AND it with 15 (binary 1 1111) so:

```
203: 1100 1011
AND 15: 0000 1111
------------------
IS 11: 0000 1011
```

These are the bitwise operators, all supported 24 in JavaScript:

`op1 & op2`

-- The`AND`

operator compares 23 two bits and generates a result of 1 if 22 both bits are 1; otherwise, it returns 0.`op1 | op2`

-- The 21`OR`

operator compares two bits and generates 20 a result of 1 if the bits are complementary; otherwise, it 19 returns 0.`op1 ^ op2`

-- The`EXCLUSIVE-OR`

operator compares two 18 bits and returns 1 if either of the bits 17 are 1 and it gives 0 if both bits are 0 16 or 1.`~op1`

-- The`COMPLEMENT`

operator is used to invert 15 all of the bits of the operand.`op1 << op2`

-- The`SHIFT LEFT`

operator 14 moves the bits to the left, discards the 13 far left bit, and assigns the rightmost 12 bit a value of 0. Each move to the left 11 effectively multiplies op1 by 2.`op1 >> op2`

-- The 10`SHIFT RIGHT`

operator moves the bits to the right, discards 9 the far right bit, and assigns the leftmost 8 bit a value of 0. Each move to the right 7 effectively divides op1 in half. The left-most 6 sign bit is preserved.`op1 >>> op2`

-- The`SHIFT RIGHT`

-`ZERO FILL`

operator 5 moves the bits to the right, discards the 4 far right bit, and assigns the leftmost 3 bit a value of 0. Each move to the right 2 effectively divides op1 in half. The left-most 1 sign bit is discarded.

To break it down a bit more, it has a lot 2 to do with the binary representation of 1 the value in question.

For example (in decimal): x = 8 y = 1 would come out to (in binary): x = 1000 y = 0001 From there, you can do computational operations such as 'and' or 'or'; in this case: x | y = 1000 0001 | ------ 1001 or...9 in decimal

Hope this helps.

When the term "bitwise" is mentioned, it 12 is sometimes clarifying that is is not a 11 "logical" operator.

For example 10 in JavaScript, bitwise operators treat their operands as a sequence of 32 bits (zeros and ones); meanwhile, logical operators are typically used with Boolean (logical) values but can work 9 with non-Boolean types.

Take expr1 && expr2 8 for example.

Returns expr1 if it can be converted to 7 false; otherwise, returns expr2. Thus, when 6 used with Boolean values, && returns 5 true if both operands are true; otherwise, returns 4 false.

```
a = "Cat" && "Dog" // t && t returns Dog
a = 2 && 4 // t && t returns 4
```

As others have noted, 2 & 4 is 3 a bitwise AND, so it will return 0.

You can 2 copy the following to test.html or something 1 and test:

```
<html>
<body>
<script>
alert("\"Cat\" && \"Dog\" = " + ("Cat" && "Dog") + "\n"
+ "2 && 4 = " + (2 && 4) + "\n"
+ "2 & 4 = " + (2 & 4));
</script>
```

In digital computer programming, a bitwise 7 operation operates on one or more bit patterns 6 or binary numerals at the level of their 5 individual bits. It is a fast, primitive 4 action directly supported by the processor, and 3 is used to manipulate values for comparisons 2 and calculations.

**operations**:

bitwise AND

bitwise OR

bitwise 1 NOT

bitwise XOR

etc

List item

```
AND|0 1 OR|0 1
---+---- ---+----
0|0 0 0|0 1
1|0 1 1|1 1
XOR|0 1 NOT|0 1
---+---- ---+---
0|0 1 |1 0
1|1 0
```

Eg.

```
203: 1100 1011
AND 15: 0000 1111
------------------
= 11: 0000 1011
```

**Uses of bitwise operator**

- The left-shift and right-shift operators are equivalent to multiplication and division by x * 2
^{y}respectively.

Eg.

```
int main()
{
int x = 19;
printf ("x << 1 = %d\n" , x <<1);
printf ("x >> 1 = %d\n", x >>1);
return 0;
}
// Output: 38 9
```

- The & operator can be used to quickly check if a number is odd or even

Eg.

```
int main()
{
int x = 19;
(x & 1)? printf("Odd"): printf("Even");
return 0;
}
// Output: Odd
```

- Quick find minimum of x and y without
`if else`

statement

Eg.

```
int min(int x, int y)
{
return y ^ ((x ^ y) & - (x < y))
}
```

- Decimal to binary conversion

Eg.

```
#include <stdio.h>
int main ()
{
int n , c , k ;
printf("Enter an integer in decimal number system\n " ) ;
scanf( "%d" , & n );
printf("%d in binary number
system is: \n " , n ) ;
for ( c = 31; c >= 0 ; c -- )
{
k = n >> c ;
if ( k & 1 )
printf("1" ) ;
else
printf("0" ) ;
}
printf(" \n " );
return 0 ;
}
```

- The XOR gate encryption is popular technique, because of its complixblity and reare use by the programmer.
- bitwise XOR operator is the most useful operator from technical interview perspective.

*bitwise shifting works only with +ve number*

**Also there is a wide range of use of bitwise logic**

It might help to think of it this way. This 8 is how AND (&) works:

It basically says 7 are both of these numbers ones, so if you 6 have two numbers 5 and 3 they will be converted 5 into binary and the computer will think

```
5: 00000101
3: 00000011
```

are 4 both one: 00000001 0 is false, 1 is true

So 3 the AND of 5 and 3 is one. The OR (|) operator 2 does the same thing except only one of the 1 numbers must be one to output 1, not both.

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.