Friday 26 July 2019

Processing: Bitwise operators


Processing support operators that perform bit wise and bit shift operations on integral types.

Processing support below operators                      
Operator
Description
&
Bit wise AND
|
Bit wise OR
^
Bit wise Exclusive OR
~
Invert bit pattern
<< 
Perform Left shift
>> 
Perform Right shift
>>> 
Zero fill right shift

Bit Wise AND(&), OR(|)
Bit1
Bit2
&
|
^
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0

Operators.pde
byte a = 8;
byte b = 9;

println("a & b is " + (a&b));
println("a | b is " + (a|b));
println("a ^ b is " + (a^b));

Output
a & b is 8
a | b is 9
a ^ b is 1

Explanation
   a    = 0000 1000
   b    = 0000 1001
   a&b = 0000 1000

   a    = 0000 1000
   b    = 0000 1001
   a|b = 0000 10001

   a     = 0000 1000
   b     = 0000 1001
   a^b = 0000 0001

Note:
   Bit wise operators are applicable only to integer values like byte, short etc., these are not applicable to float, double, Boolean.

~: Invert bit pattern
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
byte a = 7;

a in binary format is       0 0 0 0 0 1 1 1
~(a) in binary format is  1 1 1 1 1 0 0 0
~(a) represents -8 in binary format.


Operators.pde
byte byteVar = 7;

println("Value of Byte variable is " + byteVar);
println("Value of Byte variable after inversion is " + ~(byteVar));

Output
Value of Byte variable is 7
Value of Byte variable after inversion is -8

Bit Wise Left Shift(<<)
Left shift operator simply shifts all the bits to one bit left, i.e, it simply doubles the value.

Syntax:
    variable << n

    left shift the variable by 'n' number of times.

    Variable << n is equals to variable = variable * 2 power n

        int var = 2;
        var << 1 = 2 * 2 power 1 = 4
        var << 2 = 2 * 2 power 2 = 8
        var << 9 = 2 * 2 power 9 = 1024


Operators.pde
byte byteVar = 2;

println("Value of Byte variable is " + byteVar);
println("After shifting 1 bit left " + (byteVar<<1));
println("After shifting 2 bits left " + (byteVar<<2));
println("After shifting 3 bits left " + (byteVar<<3));
println("After shifting 4 bits left " + (byteVar<<4));
println("After shifting 5 bits left " + (byteVar<<5));
println("After shifting 6 bits left " + (byteVar<<6));
println("Value of Byte variable is " + byteVar);

Output
Value of Byte variable is 2
After shifting 1 bit left 4
After shifting 2 bits left 8
After shifting 3 bits left 16
After shifting 4 bits left 32
After shifting 5 bits left 64
After shifting 6 bits left 128
Value of Byte variable is 2

Bit Wise Right Shift(>>)
Right shift operator simply shifts all the bits to one bit right, i.e, it simply half (divide by 2) the value

   Syntax:
   variable >> n
      right shift the variable by 'n' number of times.

   Variable >> n is equals to variable = variable / 2 power n

   int var = 128;
   var >> 1 = 128 / (2 power 1) = 64
   var >> 2 = 128 / (2 power 2) = 32


Operators.pde
int intVar = 128;

println("Value of int variable is " + intVar);
println("After shifting 1 bit Right " + (intVar>>1));
println("After shifting 2 bits Right " + (intVar>>2));
println("After shifting 3 bits Right " + (intVar>>3));
println("After shifting 4 bits Right " + (intVar>>4));
println("After shifting 5 bits Right " + (intVar>>5));
println("After shifting 6 bits Right " + (intVar>>6));
println("After shifting 6 bits Right " + (intVar>>7));
println("Value of int variable is " + intVar);

Output
Value of int variable is 128
After shifting 1 bit Right 64
After shifting 2 bits Right 32
After shifting 3 bits Right 16
After shifting 4 bits Right 8
After shifting 5 bits Right 4
After shifting 6 bits Right 2
After shifting 6 bits Right 1
Value of int variable is 128

Bit Wise unsigned Right Shift(>>>)
shifts all the bits to one bit right, and make the left most bit to zero.


Operators.pde
int intVar = -128;

println("Value of int variable is " + intVar);
println("After shifting 1 bit Right " + (intVar>>>1));

Output
Value of int variable is -128
After shifting 1 bit Right 2147483584

Explanation
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128>>>1) is like below sequence


01111111 11111111 11111111 11000000


Previous                                                    Next                                                    Home

No comments:

Post a Comment