Update Bit operation || Bit manipulation in java language.

■》 Input->

import java.util.*;
public class Bits {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
    System.out.println("enter a number=");
int n = sc.nextInt();
System.out.println("enter a position=");    
int pos=sc.nextInt();
int bitMask =1<<pos;
System.out.println("enter the updatation bit=");    
int oper = sc.nextInt();
// oper=1 -> set; oper=0 -> clear
if(oper == 1) {
//set
int newNumber = bitMask | n;
System.out.println("after updation the number is=");
System.out.println(newNumber);
}
    else {
//clear
int newBitMask = ~(bitMask);
int newNumber = newBitMask & n;
 System.out.println("after updation the number is=");
System.out.println(newNumber);
}
}
}

■》output->

enter a number= 5
enter a position= 1
enter the updatation bit= 1
after updation the number is=7

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.