# Bitwise Operators

0

& → AND operator

| → OR operator

^ → XOR operator

~ → NOT operator

>> → Shift Right

<< → Shift Left

& → Two numbers in base ten sent through the AND operator may seem to return a random number, but when you see the binary representation of those numbers, it makes sense. It checks to see if both of the values are on, or if both values are 1's. So 0 & 0 returns 0, 0 & 1 returns 0, 1 & 0 returns 0, and 1 & 1 returns zero. For larger binary numbers, it iterates this for each of the digits.

| → This operator checks if the binary representations of two number contains at least 1 '1' in it. So 0 | 0 returns 0, 0 | 1 returns 1, 1 | 0 returns 1, and 1 | 1 returns 1. For larger numbers, it checks each sequential digit in each for this.

^ → This operator checks to see if there is only 1 '1' and 1 '0' in the binary representations two numbers. So 0 ^ 0 returns 0, 0 ^ 1 returns 1, 1 ^ 0 returns 1, and 1 ^ 1 returns 0. For larger numbers, it iterates through all of the digits and checks each one sequentially.

~ → This takes in one number and switches all 1's in it's binary representation to 0's and all 0's to ones. This of course also switches signs, since this is how negative binary numbers work (2 in 3-bit binary is 010, so -2 is then 110). For example, ~1 in 3-bit (~001) is -2 (110).

>> → Numbers in base ten (say, 10) shifted right 1 represents being divided by 2 once, which can be represented as a shift left in binary (10 >> 1 or 0b1010 >> 1 returns 0b101 or 5). Shifting it by more than 1 shifts it right that many times.

<<  → Numbers in base ten shifted left 1, you guessed it, multiplies the  number by 2 once, which can be represented as a shift right in binary (5 << 1 or 0b101 << 1 returns 0b1010 or 10). Shifting it by more than 1 shifts it left by that many times.

That one guy that's always on

Share:

Working