Bitwise and Bitshift Operators in Different Languages

NameLanguagesOperatorExplanationExample
Bitwise ANDJava /Python/C/PHP/Javascript /SQL & The & operator compares corresponding bits of two operands. If both bits are 1, it gives 1. If either of the bits is not 1, it gives 0.  a = 55 & 21;
Bitwise ORJava /Python/C /PHP/Javascript /SQL | The | operator compares corresponding bits of two operands. If either of the bits is 1, it gives 1. If not, it gives 0. result = 22|9;
Bitwise ComplementJava /Python/C /PHP/Javascript ~Bitwise complement is an unary operator (works on only one operand). It is denoted by ~.
The ~ operator inverts the bit pattern.
result = ~25;
Bitwise XORJava /Python /C/PHP/Javascript ^ The ^ operator compares corresponding bits of two operands. If corresponding bits are different, it gives 1. If corresponding bits are same, it gives 0. 
result = 12 ^ 65;
Left Shift
or
Zero fill left shift
Java/Python/C /PHP/Javascript <<The left shift operator << shifts a bit pattern to the left by certain number of specified bits, and zero bits are shifted into the low-order positions. 212 (In binary: 11010100)
212 << 1
evaluates to 424 (In binary: 110101000)
Right ShiftJava/Python/C/PHP/Javascript >> The right shift operator >> shifts a bit pattern to the right by certain number of specified bits.
212 (In binary: 11010100)
212 >> 1
evaluates to 106 (In binary: 01101010)
Unsigned Right Shift
or
Zero fill right shift
Java /Javascript >>> The unsigned right shift operator >>> shifts zero into the leftmost position.
number1 = 5;

number1 >>> 1 will give 2
Bitwise Exclusive ORSQL^ The ^ performs a bitwise logical exclusive OR between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if either (but not both) bits (for the current bit being resolved) in the input expressions have a value of 1. If both bits are 0 or both bits are 1, the bit in the result is cleared to a value of 0.
If the left and right expressions have different integer data types (for example, the left expression is smallint and the right expression is int), the argument of the smaller data type is converted to the larger data type.

SELECT value_a ^ value_b FROM tbl_bitwise;
GO
Bitwise NOTSQL~ The ~ performs a bitwise logical NOT for the expression, taking each bit in turn SELECT ~ a, ~ b FROM tbl_bitwise;
Bitwise AND Assignment SQL&= The &= performs a bitwise logical AND operation between two integer values, and sets a value to the result of the operation.
X &= Y means X = X & Y;
Bitwise OR Assignment SQL|= The |= performs a bitwise logical OR operation between two specified integer values as translated to binary expressions within Transact-SQL statements, and sets a value to the result of the operation. X |=Y means X=X |Y;
Bitwise Exclusive OR Assignment SQL^= The ^= performs a bitwise exclusive OR operation between two integer values, and sets a value to the result of the operation.
X ^=Y means X=X ^Y;

One thought on “Bitwise and Bitshift Operators in Different Languages

Add yours

  1. Wow that was unusual. I just wrote an very long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say excellent blog!

    Like

Leave a reply to Salvatore Sirbaugh Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started