-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_hacks.c
More file actions
137 lines (121 loc) · 2.58 KB
/
bit_hacks.c
File metadata and controls
137 lines (121 loc) · 2.58 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
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
128
129
130
131
132
133
134
135
136
137
#include<stdio.h>
//bit hacks
//1.swap two numbers
//2.take 1's complement
//3.detect no is power of 2 or not(the most efficient way)
//4.count no of 1's
void count_no_of_ones(int *x) {
int t = *x;
int count = 0;
while(t!=0) {
t &= (t-1);
count++;
}
printf("total number of 1's in %d= %d\n",*x,count);
}
/*
*negate all bits and add 1
**/
void get_twos_complement() {
int x = 1;
printf("2's complement in decimal = %d\n",-x);
}
void presentation() {
int i = -1;
int len = sizeof(int);
char *byte = (char*)&i;
int index = 0;
for(;index<len;index++) {
printf("byte[%d] = %.2X \n",index,byte[index]);
}
/**
*byte[0] = FFFFFFFF
*byte[1] = FFFFFFFF
*byte[2] = FFFFFFFF
*byte[3] = FFFFFFFF
**/
}
/**
** swap two numbers with XOR inverse property.
** A ^ A = 0;
**/
void swap_numbers(int *x,int *y) {
printf("before swap x=%d and y=%d \n",*x,*y);
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
printf("After swap x=%d and y=%d \n",*x,*y);
}
/**
* To detect endian use byte pointer to access byte level address.
* for the representation.
* TRY TO AVOID USE OF HEX FOR ASSIGNMENT.
**/
void detect_endian1() {
int word = 1;
char* byte = (char*)&word;
printf("byte[0]=%.2X\n",byte[0]); //first byte
printf("byte[1]=%.2X\n",byte[1]); //second byte
printf("byte[0]=%.2X\n",*byte);
printf("byte[0]=%.2X\n",byte);
if(byte[0] == 1) {
printf("big endian \n");
} else {
printf("little endian \n");
}
/**
*output of the function
*byte[0]=01
*byte[1]=00
*byte[0]=01
*byte[0]=BFF9B518
*big endian
*/
}
//wrong version
void detect_endian() {
//char byte = 0xFF;
unsigned char byte = 255;
int word = byte;
if(word == 0xFF) {
printf("big endian \n");
} else {
printf("little endian \n");
}
printf ("byte=%d and word=%d \n",byte,word);
}
void information_storage()
{
int a = 0xFFFF;
int b = 0xFFFFFFFF;
printf ("a= %d and b=%d\n",a,b);
/**
* a= 65535 and b=-1
*/
}
static void do_rev () {
unsigned int v = 219; // input bits to be reversed
//print_bin(v);
unsigned int r = v; // r will be reversed bits of v; first get LSB of v
int s = sizeof(v) * 8 - 1; // extra shift needed at end
while(v) {
v >>= 1; //TODO:Use this method for rotation (n rotation) n times or O.M. use left_shift | right_shift(K-n)
//K = total bits and n = no of rotation.
r <<= 1;
r |= v & 1; //get only one bit of v every time/
s--;
}
r <<= s; // shift when v's highest bits are zero
printf("%d \n",r);
//print_bin(r);
}
int main() {
detect_endian1();
int x = 10;
int y = 45;
swap_numbers(&x,&y);
presentation();
count_no_of_ones(&y);
do_rev();
return 0;
}