-
Notifications
You must be signed in to change notification settings - Fork 4
/
basicbitwise.c
56 lines (49 loc) · 997 Bytes
/
basicbitwise.c
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
/* check set clear and toggle */
#include<stdio.h>
#include<stdlib.h>
int setbit(int n,int pos)
{
n = n | ( 1 << pos);
return n;
}
int clearbit(int n,int pos)
{
n = n & (~( 1 << pos));
return n;
}
int checkbit(int n,int pos)
{
int res;
res = n & (1 << pos);
return res;
}
int togglebit(int n,int pos)
{
n = n ^ ( 1 << pos);
return n;
}
void main()
{
int n,set,clr,tog,res,check;
printf("Enter the number:\n");
scanf("%d",&n);
printf("Position to set:\n");
scanf("%d",&set);
printf("Position to clear:\n");
scanf("%d",&clr);
printf("Position to toggle:\n");
scanf("%d",&tog);
printf("Position to check:\n");
scanf("%d",&check);
res=setbit(n,set);
printf("Result after set is :%d \n",res);
res=clearbit(n,clr);
printf("Result after clear is :%d \n",res);
res=togglebit(n,tog);
printf("Result after toggle is :%d \n",res);
res=checkbit(n,check);
if(res)
printf("The %d th bit in %d is set\n",check,n);
else
printf("The %d th bit in %d is not set\n",check,n);
}