Bit Manipulation Hacks

 
There are tons of algorithms which can be optimized by using bit manipulation. In this post, we will discuss few such interesting bit manipulation hacks.

 

  • Bit Hack #1. Check if an given integer is even or odd
     
  • Bit Hack #2. Detect if two integers have opposite signs or not
     
  • Bit Hack #3. Add 1 to a given integer
     
  • Bit Hack #4. Swap two numbers without using any third variable
     
  • Bit Hack #5. Turn off kth bit in a number
     
  • Bit Hack #6. Turn on kth bit in a number
     
  • Bit Hack #7. Check if kth bit is set for a number
     
  • Bit Hack #8. Toggle the kth bit
     
  • Bit Hack #9. Check if given positive integer is a power of 2 without using any branching or loop
     
  • Bit Hack #10. Find position of the rightmost set bit
     
  • Bit Hack #11. Find position of the only set bit in a number
     
  • Bit Hack #12. Computing parity of a number (Naive solution)
     
  • Bit Hack #13. Convert uppercase character to lowercase
     
  • Bit Hack #14. Convert lowercase character to uppercase
     
  • Bit Hack #15. Invert alphabet’s case
     
  • Bit Hack #16. Find letter’s position in alphabet
     
  • Bit Hack #17. Given an integer, compute its absolute value (abs) without branching
     
  • Bit Hack #18. Find number of bits needed to be flipped to convert given integer to another
     
  • Bit Hack #19. Conditionally negate a value without branching
     
  • Bit Hack #20. Find XOR of two numbers without using XOR operator
     
  • Bit Hack #21. Find if adjacent bits are 1 in binary representation of a number
     
  • Bit Hack #22. Check if binary representation of a number is palindrome or not
     



  •  
     

    Thank you for being with us. 🙂

     




    Leave a Reply

    Notify of
    avatar
    wpDiscuz