Friday, August 18, 2017

Some bitwise things

While doing multiplication using left shift operator we need to find out any of the operand must be multiple of 2.

 For ex : If we want to multiply (8 * 9) then we can left shift 9 by 3 as 8 is 2^3. We can consider any operand of the expression in case of multiplication but in case of division the denominator should be a multiple of 2 then only we can do the operation by right shifting the numerator.

For ex : If we want to divide (8 / 9), denominator (9) must be multiple of 2. Here we can not do this operation. But if we want to divide (9 / 8), now denominator (8) is a multiple of 2. Hence we can do the same operation by right shifting the numerator (9) by 3.

(8 * 9) == (9 << 3) and (9 / 8) == (9 >> 3)

You can do modulo operation as mentioned below :

Ex : (15 % 2) == (15 & 1),  (16 % 8) == (16 & 7) 

Note : Bitwise operators only works with integer type variables. (Ex : int, u_int, char, u_char, short)

No comments:

Post a Comment

Base and Bounds, Segmentation

Base & bounds relocation: Two hardware registers: base address for process, bounds register that indicates the last valid address t...