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

non sequitor picture non sequitor · Oct 7, 2009 · Viewed 33.3k times · Source

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?

Answer

CSᵠ picture CSᵠ · Apr 23, 2013

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