-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwise.c
127 lines (101 loc) · 2.93 KB
/
bitwise.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
//////////////////////////
// Bitwise Operators
// AND – & – Sets each bit to 1 if both bits are 1
// OR – | – Sets each bit to 1 if any of the bits is set to 1
// XOR – ^ – Sets each bit to 1 if only one of the bits is set to 1
// NOT – ~ – Inverts all the bits
// << – Zero fill left shift
// >> – Signed right shift
/////////////////////////
// Shift Operators
// << – Left Shift
// >> – Right Shift
/////////////////////////
// Bitwise Assignment Operators
// |= – Bitwise OR assignment
// &= – Bitwise AND assignment
// ^= – Bitwise XOR assignment
// <<= – Left shift assignment
// >>= – Right shift assignment
//////////////////////////
/* bits union: represents a float as its parts according to the IEEE
* floating-point standard */
union bits {
float f; /* assumes sizeof(float) == sizeof(int) */
unsigned int u;
struct {
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} parts;
};
void print_float_parts(float number) {
union bits num;
num.f = number; // Store the float in the union
// Extracting the parts
unsigned int sign = num.parts.sign;
unsigned int exponent = num.parts.exponent;
unsigned int mantissa = num.parts.mantissa;
// Print the parts
printf("Float: %f\n", number);
printf("Sign: %u (0 for positive, 1 for negative)\n", sign);
printf("Exponent: %u (Stored Exponent)\n", exponent);
printf("Mantissa: %u (Fraction part)\n", mantissa);
}
// 00001111
// &
// 00000111
//--------
// 00000111
////////////////////////////////////
// 00001111
// |
// 00000111
//--------
// 00001111
////////////////////////////////////
// 00001111
//^
// 00000111
//--------
// 00001000
////////////////////////////////////
/* xorSwap: swap *x and *y by XORing them */
void xorSwap(int *x, int *y) {
if (x != y) {
*x ^= *y; //<==> One = One ^ Two;
*y ^= *x; //<==> Two = Two ^ One = Two ^ (One ^ Two) = One;
*x ^= *y; //<==> One = One ^ Two = (One ^ Two) ^ One = Two;
}
}
/* shift: demonstrates the left shift operator */
void shift() {
unsigned int Value = 4; /* 4 = 0000 0100 */
unsigned int Shift = 2;
Value = Value << Shift; /* 16 = 0001 0000 */
Value <<= Shift; /* 64 = 0100 0000 */
printf("%d\n", Value); /* Prints 64 */
}
/* mask and toggle: demonstrates the bitwise AND operator */
void mask_and_toggle() {
unsigned int Value = 0x0F; /* 15 = 0000 1111 */
unsigned int Mask = 0x06; /* 6 = 0000 0110 */
Value &= Mask; /* 6 = 0000 0110 */
printf("%d\n", Value); /* Prints 6 */
}
int main() {
int x = 7;
int y = 15;
int z = x & y;
printf("x&y=%d\n", z);
z = x | y;
printf("x|y=%d\n", z);
z = x ^ y;
printf("x^y=%d\n", z);
printf("x&y=%d\n", z);
float test_number = -123.456f;
print_float_parts(test_number);
mask_and_toggle();
return 0;
}