-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmanipulation.java
More file actions
67 lines (56 loc) · 1.23 KB
/
Copy pathBitmanipulation.java
File metadata and controls
67 lines (56 loc) · 1.23 KB
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
61
62
63
64
65
66
67
import java.util.Scanner;
public class Bitmanipulation {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int n=5;
// int pos=2;
// int bitmask=1<<pos;
//
// //1.get bit
//
// //for that particular bit
// if((bitmask&n)==0)
// {
// System.out.println("bit is zero");
// }
// else
// {
// System.out.println("bit is one");
// }
//set bit
//if we find posn at 1 then
// int n=5;
// int pos=1;
// int bitmask=1<<pos; //left shift i.e multiplt by 2
// int newnumber=bitmask | n;
// System.out.println(newnumber);
//clear bit i.e bit a that posn to make 0
//bitmask= 1<<pos
//opern= !(bitmask)&n
// int n=5;
// int pos=2;
// int bitmask=1<<pos; //left shift i.e multiplt by 2
// int notbitmask=~bitmask;
// int newnumber=notbitmask&n;
// System.out.println(newnumber);
//update bit
Scanner sc=new Scanner(System.in);
int opr=sc.nextInt();
int n=5;
int pos=2;
if(opr==0)
{ //clear oprn
int bitmask=1<<pos; //left shift i.e multiplt by 2
int notbitmask=~bitmask;
int newnumber=notbitmask&n;
System.out.println(newnumber);
}
else
{
//set oprn
int bitmask=1<<pos;
int newnumber=bitmask|n;
System.out.println(newnumber);
}
}
}