1. Bitwise Operator #
Created Saturday 19 October 2019
- & - check even or odd → do k&1 → if answer is 1, then k is odd. Otherwise even.
- | checking for zero, do k | 0. If 0 comes, k is 0. else it is non-zero.
- ~ checking for 0, if the number after ~k == 0, then k is 0.
- ^ shortcut for checking equality, 0 if equal and 1 if unequal.
There are two bitwise shift operators:
- << operator. Shift to the left, pad with zeros.This does doubles the value for almost all numbers, except the ones with the second last MSB as 1 or 0, It flips the here.
-
floored division with not truncation of decimal. Padding with 0 or 1 as per compiler, g++17 pads negatives with 1 and, positives with 0. e.g -23/2 = -12
- Using shift operators with negative integers is termed an undefined operator coz we cannot predict the output for all cases.
Resources