-
Notifications
You must be signed in to change notification settings - Fork 88
/
bit_manipn.java
60 lines (50 loc) · 1.43 KB
/
bit_manipn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
import javax.swing.event.DocumentEvent.ElementChange;
public class bit_manipn {
//**Get bit
// public static void main(String[] args){
// int n=5;
// int pos=2;
// int bitmask=1<<pos;
// if((bitmask & n)==0){
// System.out.println("bit was zero");
// }
// else{
// System.out.println("bit was one");
// }
// }
//**set bit
// public static void main(String[] args){
// int n=5;
// int pos=1;
// int bitmask=1<<pos;
// int newNumber= bitmask | n;
// System.out.println(newNumber);
// }
//**clear bit
// public static void main(String[] args){
// int n =5;
// int pos=1;
// int bitmask=1<<pos;
// int newbitmask=~(bitmask);
// int newNumber = newbitmask & n;
// System.out.println(newNumber);
// }
//**update bit
public static void main(String[] args){
int n=5;
int pos=1;
Scanner sc = new Scanner(System.in);
int upd = sc.nextInt();
int bitmask=1<<pos;
if(upd==1){
int newNumber = bitmask | n;
System.out.println(newNumber);
}
else{
int newbitmask=~(bitmask);
int newNumber=newbitmask & n;
System.out.println(newNumber);
}
}
}