Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What USEFUL bitwise operator code tricks should a developer know about? [closed]

I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more efficiently?

like image 705
non sequitor Avatar asked Oct 07 '09 17:10

non sequitor


People also ask

How are bitwise operators useful?

The bitwise shift operators are used to move all of the bits in the operand left or right a given number of times. They come in quite handy when you need to divide or multiply integer values.

Which bitwise operator is used to check on or off?

Yaa, & operator is perfect for find out on or off. Hai frndz. How & operator is used to check whether a bit is on or off is as follows let us conceive that there is a binary num 10101000 if you want to check third bit is on or off then perform and operation to 00001000 then if 3rd bit is 1 its on else its turned off.

Which bitwise operator is suitable for turning off a particular bit in a number question?

The idea is to use bitwise <<, & and ~ operators.


2 Answers

Using bitwise operations on strings (characters)

Convert letter to lowercase:

  • OR by space => (x | ' ')
  • Result is always lowercase even if letter is already lowercase
  • eg. ('a' | ' ') => 'a' ; ('A' | ' ') => 'a'

Convert letter to uppercase:

  • AND by underline => (x & '_')
  • Result is always uppercase even if letter is already uppercase
  • eg. ('a' & '_') => 'A' ; ('A' & '_') => 'A'

Invert letter's case:

  • XOR by space => (x ^ ' ')
  • eg. ('a' ^ ' ') => 'A' ; ('A' ^ ' ') => 'a'

Letter's position in alphabet:

  • AND by chr(31)/binary('11111')/(hex('1F') => (x & "\x1F")
  • Result is in 1..26 range, letter case is not important
  • eg. ('a' & "\x1F") => 1 ; ('B' & "\x1F") => 2

Get letter's position in alphabet (for Uppercase letters only):

  • AND by ? => (x & '?') or XOR by @ => (x ^ '@')
  • eg. ('C' & '?') => 3 ; ('Z' ^ '@') => 26

Get letter's position in alphabet (for lowercase letters only):

  • XOR by backtick/chr(96)/binary('1100000')/hex('60') => (x ^ '`')
  • eg. ('d' ^ '`') => 4 ; ('x' ^ '`') => 25

Note: using anything other than the english letters will produce garbage results

like image 145
CSᵠ Avatar answered Sep 30 '22 19:09

CSᵠ



  • Bitwise operations on integers(int)

Get the maximum integer

int maxInt = ~(1 << 31); int maxInt = (1 << 31) - 1; int maxInt = (1 << -1) - 1; 

Get the minimum integer

int minInt = 1 << 31; int minInt = 1 << -1; 

Get the maximum long

long maxLong = ((long)1 << 127) - 1; 

Multiplied by 2

n << 1; // n*2 

Divided by 2

n >> 1; // n/2 

Multiplied by the m-th power of 2

n << m; // (3<<5) ==>3 * 2^5 ==> 96 

Divided by the m-th power of 2

n >> m; // (20>>2) ==>(20/( 2^2) ==> 5 

Check odd number

(n & 1) == 1; 

Exchange two values

a ^= b; b ^= a; a ^= b; 

Get absolute value

(n ^ (n >> 31)) - (n >> 31); 

Get the max of two values

b & ((a-b) >> 31) | a & (~(a-b) >> 31); 

Get the min of two values

a & ((a-b) >> 31) | b & (~(a-b) >> 31); 

Check whether both have the same sign

(x ^ y) >= 0; 

Calculate 2^n

2 << (n-1); 

Whether is factorial of 2

n > 0 ? (n & (n - 1)) == 0 : false; 

Modulo 2^n against m

m & (n - 1); 

Get the average

(x + y) >> 1; ((x ^ y) >> 1) + (x & y); 

Get the m-th bit of n (from low to high)

(n >> (m-1)) & 1; 

Set the m-th bit of n to 0 (from low to high)

n & ~(1 << (m-1)); 

n + 1

-~n 

n - 1

~-n 

Get the contrast number

~n + 1; (n ^ -1) + 1;  

if (x==a) x=b; if (x==b) x=a;

x = a ^ b ^ x; 
like image 38
Mohasin Ali Avatar answered Sep 30 '22 20:09

Mohasin Ali