Skip to main content

Bit Manipulation |Get Bit | Set Bit | Update Bit | Lecture 14

Java - Introduction to Programming

Lecture 14



Bit Manipulation


Get Bit

import java.util.*;

 

public class Bits {

   public static void main(String args[]) {

      int n = 5; //0101

      int pos = 3;

      int bitMask = 1<<pos;

 

      if((bitMask & n) == 0) {

          System.out.println("bit was zero");

      } else {

          System.out.println("bit was one");

      }

   }

}

 



Set Bit

import java.util.*;

 

public class Bits {

   public static void main(String args[]) {

      int n = 5; //0101

      int pos = 1;

      int bitMask = 1<<pos;

 

      int newNumber = bitMask | n;

      System.out.println(newNumber);

   }

}


Clear Bit

import java.util.*;

public class Bits {

  public static void main(String args[]) {

     int n = 5; //0101

     int pos = 2;

     int bitMask = 1<<pos;

     int newBitMask = ~(bitMask);

     int newNumber = newBitMask & n;

     System.out.println(newNumber);

  }

}

 



Update Bit


import java.util.*;

 

public class Bits {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int oper = sc.nextInt();

       // oper=1 -> set; oper=0 -> clear

      int n = 5;

      int pos = 1;

     

      int bitMask = 1<<pos;

      if(oper == 1) {

          //set

          int newNumber = bitMask | n;

          System.out.println(newNumber);

      } else {

       //clear

       int newBitMask = ~(bitMask);

       int newNumber = newBitMask & n;

       System.out.println(newNumber);

      }

     

   }

}

 


Homework Problems

  1. Write a program to find if a number is a power of 2 or not.

  2. Write a program to toggle a bit a position = “pos” in a number “n”.

  3. Write a program to count the number of 1’s in a binary representation of the number.

  4. Write 2 functions => decimalToBinary() & binaryToDecimal() to convert a number from one number system to another. [BONUS]

 

Comments

Popular posts from this blog

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

2D Arrays | Java Complete Placement Course | Lecture 11

Java - Introduction to Programming Lecture 11 2D Arrays In Java It is similar to 2D matrices that we studied in 11th and 12th class. Creating a 2D Array - with new keyword int [][] marks = new int [ 3 ][ 3 ] ; Taking a matrix as an input and printing its elements. import java . util .*;   public class TwoDArrays {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int rows = sc . nextInt ();         int cols = sc . nextInt ();           int [][] numbers = new int [ rows ][ cols ];           //input         //rows         for ( int i = 0 ; i < rows ; i ++) {             //columns  ...

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...