Skip to main content

Command Palette

Search for a command to run...

Bit Manipulation

Updated
•3 min read•View as Markdown
F

Assalamualaikum warahmatullah wabarakatuh( traditional Islamic greeting in Arabic "Assalamu alaikum": "Peace be upon you." "Wa rahmatullahi": "And the mercy of Allah." "Wa barakatuh": "And His blessings.") I’m Faraz Alam, and I’m documenting my journey through the world of software technology. Despite earning a master’s degree in Computer Applications and having access to opportunities provided by my tier-3 college, I struggled to take full advantage of them due to poor management and a less productive environment. This led to joblessness, primarily due to a lack of upskilling. Now, I am dedicated to enhancing my skills and knowledge with the aim of securing a valuable job offer from leading product-based companies, including those in the FAANG group (Facebook, Amazon, Apple, Netflix, Google) and other prominent tech giants. This documentation is not for self-promotion; rather, it is for anyone who is waiting for an opportunity but feels they lack the tools and skills required to overcome challenges. It’s a testament to the effort and responsibility needed to navigate the journey towards success when you take charge of your own path. Date: 31 July 2024, 07:25 AM This page will be updated regularly to reflect new achievements and milestones as I continue to build my career.

i) a<<b (a left shift b):- a * 2^b;

ii) a>>b(a right shift b):- a/2^b;

  1. Write a program to check number is even or odd using bit manipulation.

     public class Main
     {
         public static void oddEven(int n){
             int bitMask=1;
             if((n&bitMask)==0){
                 System.out.println("even");
             }else{
                 System.out.println("odd");
              }
         }
         public static void main(String[] args) {
             oddEven(2);
             oddEven(3);
             oddEven(5);
         }
     }
    

  2. Write a program to get the ith bit in a number.

     public class Main
     {
         public static int getithBit(int n, int i)
         {
             int bitMask=1<<i;
             if((n&bitMask)==0){
                 return 0;
             }else{
                 return 1;   
             }
         }
         public static void main(String[] args) {
             System.out.println(getithBit(15,2));//1
         }
     }
    

  3. Write a program to set ith bit in a number.

     public class Main
     {
         public static int setithBit(int n, int i){
             int bitmask= 1<<i;
             return n|bitmask;
         }
         public static void main(String[] args) {
             System.out.println(setithBit(7,3)); //15
             System.out.println(setithBit(10,2)); //14
             System.out.println(setithBit(9,2)); //13
         }
     }
    

  4. Write a program to clear the ith bit of a number.

     public class Main
     {
         public static int clear(int n, int i){
             int bitMask= (~(1<<i));
             return n&bitMask;
         }
         public static void main(String[] args) {
             System.out.println(clear(13,2));//9
             System.out.println(clear(4,1));//4
             System.out.println(clear(4,2));//0
         }
     }
    
  5. Write a program to update the ith bit of a number with 1 or 0.

     public class Main
     {
         public static int update(int n, int i, int newBit){
             int bitMask=newBit<<i;
             return n|bitMask;
         }
         public static void main(String[] args) {
             System.out.println(update(4,2,0));//4
             System.out.println(update(4,2,1));//4
             System.out.println(update(4,1,1));//6
         }
     }
    
  6. Write a program to clear the last ith bit of a number

     public class Main
     {
         public static int lastBitClear(int n, int i){
             int bitMask= ((-1)<<i);
             return n & bitMask;
         }
         public static void main(String[] args) {
             System.out.println(lastBitClear(7,2));//4
             System.out.println(lastBitClear(12,2));//12
         }
     }
    
  7. Write a program to clear the range of bits in a number.

     public class Main
     {
         public static int update(int n, int i, int j){
             int a=((-1)<<(j+1));
             int b= (1<<i)-1;
             return  n & (a | b);
         }
         public static void main(String[] args) {
             System.out.println(update(10,1,3));//0
             System.out.println(update(7,2,3));//3
         }
     }
    
  8. Write a program to check if a number is power of two or not.

     public class Main
     {
         public static boolean isPowerOfTwo(int n){
             return ((n&(n-1))==0);
         }
         public static void main(String[] args) {
             System.out.println(isPowerOfTwo(2));
             System.out.println(isPowerOfTwo(16));
             System.out.println(isPowerOfTwo(5));
         }
     }
    
  9. Write a program to count the set bits in a number.

     public class Main
     {
         public static int count(int n){
             int count=0;
             while(n>0){
                 if((n&1)!=0){
                     count++;
                 }
                 n=n>>1;
             }
             return count;
         }
         public static void main(String[] args) {
             System.out.println(count(15));//4
             System.out.println(count(7));//3
         }
     }
    

  10. Write a program to perform exponentiation using bit manipulation.

    public class Main
    {
        public static int exponentiation(int a, int n){
            int ans=1;
            while(n>0){
                if((n&1)!=0){
                    ans=ans*a;
                }
                a=a*a;
                n=n>>1;
            }
            return ans;
        }
        public static void main(String[] args) {
            System.out.println(exponentiation(5,3));//125
            System.out.println(exponentiation(2,4));//16
        }
    }
    

Data Structures And Algorithms

Part 6 of 8

This series is a comprehensive guide to mastering data structures and algorithms, spanning from fundamental concepts to advanced levels, equipping you with the problem-solving skills which are solved during SDE preparations of product based company.

Up next

Recursion

Write a program to print numbers from n to 1 using recursion. import java.util.*; public class Main { public static void print(int n){ if(n==1){ System.out.print(n); return; } System.out.pr...