## Posts

Showing posts from August, 2020

### BITWISE NOT

Bitwise NOT The NOT bitwise operation inverts bits. A 00 becomes a 11. A 11 becomes a 00.The NOT operator is often written as a tilde character ("~"): ~00000101=11111010CC#C++JavaJavaScriptObjective-CPHPPython 2.7Python 3.6Ruby

### BIT SHIFTING

Bit Shifting A bit shift moves each digit in a number's binary representation left or right. There are three main types of shifts: Left ShiftsWhen shifting left, the most-significant bit is lost, and a 00 bit is inserted on the other end.The left shift operator is usually written as "<<". 0010 << 1 → 0100 0010 << 2 → 1000 A single left shift multiplies a binary number by 2: 0010 << 1 → 0100 0010 is 2 0100 is 4 Logical Right ShiftsWhen shifting right with a logical right shift, the least-significant bit is lost and a 00 is inserted on the other end.

### BITWISE XOR

Bitwise XOR (eXclusive OR) The XOR operation (or exclusive or) takes two bits and returns 11 if exactly one of the bits is 11. Otherwise, it returns 00. 1 ^ 1 → 0 1 ^ 0 → 1 0 ^ 1 → 1 0 ^ 0 → 0 Think of it like a bag of chips where only one hand can fit in at a time. If no one reaches for chips, no one gets chips, and if both people reach for chips, they can't fit and no one gets chips either!When performing XOR on two integers, the XOR operation is calculated on each pair of bits (the two bits at the same index in each number). 5^6// gives 3// At the bit level:// 0101 (5)// ^ 0110 (6)// = 0011 (3)

### BITWISE OR

Bitwise OR The OR operation takes two bits and returns 11 if either of the bits are 11. Otherwise, it returns 00. 1 | 1 → 1 1 | 0 → 1 0 | 1 → 1 0 | 0 → 0 Think of it like a bucket with two holes in it. If both holes are closed, no water comes out. If either hole is open, or if both are open, water comes out.When performing OR on two integers, the OR operation is calculated on each pair of bits (the two bits at the same index in each number). 5|6// gives 7// At the bit level:// 0101 (5)// | 0110 (6)// = 0111 (7)

### BITWISE AND

Bitwise AND The AND operation takes two bits and returns 11 if both bits are 11. Otherwise, it returns 00. 1 & 1 → 1 1 & 0 → 0 0 & 1 → 0 0 & 0 → 0 Think of it like a hose with two knobs. Both knobs must be set to on for water to come out.When performing AND on two integers, the AND operation is calculated on each pair of bits (the two bits at the same index in each number). 5&6// gives 4// at the bit level:// 0101 (5)// & 0110 (6)// = 0100 (4)