-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram46.java
More file actions
24 lines (20 loc) · 940 Bytes
/
Program46.java
File metadata and controls
24 lines (20 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
public class Program46 {
//BIT MANIPULATION( update the 2nd bit(position=1)of a number n to 1.(n=0101))
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int oper = sc.nextInt(); //operation
//oper=1:SET and oper=0:CLEAR
int n = sc.nextInt(); //number
int pos = sc.nextInt(); //position
int bitmask = 1 << pos; //create a bitmask with 1 at the given position
if (oper == 1) {
int newNumber = n | bitmask;
System.out.println("The new number after setting the bit is: " + newNumber);
} else {
int Notbitmask = ~bitmask; //create a bitmask with 0 at the given position
int newNumber = n & Notbitmask; //clear the bit at the given position
System.out.println("The new number after clearing the bit is: " + newNumber);
}
}
}