-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbithacks.c
More file actions
116 lines (103 loc) · 2.04 KB
/
bithacks.c
File metadata and controls
116 lines (103 loc) · 2.04 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
#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() {
short i = 0x0001;
char* mem_block = (char*)&i;
printf("byte[0]=%.2X\n",mem_block[0]); //first byte
printf("byte[1]=%.2X\n",mem_block[1]); //second byte
printf("byte[0]=%.2X\n",*mem_block);
printf("byte[0]=%.2X\n",mem_block);
if(mem_block[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
*/
}
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
*/
}
int main() {
detect_endian1();
int x = 10;
int y = 45;
swap_numbers(&x,&y);
presentation();
count_no_of_ones(&y);
return 0;
}