Hello Friends, I had written a blog post on Quora on Bitwise tricks...So I thought I should share those tricks here on the my Blog also.

Here are some cool Bitwise operations, that you may not be knowing!!!

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
Axact

Axact

Vestibulum bibendum felis sit amet dolor auctor molestie. In dignissim eget nibh id dapibus. Fusce et suscipit orci. Aliquam sit amet urna lorem. Duis eu imperdiet nunc, non imperdiet libero.

Post A Comment:

0 comments: