-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit-manipulation.c
58 lines (44 loc) · 945 Bytes
/
bit-manipulation.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
#include <stdint.h>
#include <stdio.h>
#define BINSTRLEN sizeof(unsigned long)*8
static char _binstr[BINSTRLEN+1];
typedef struct {
char vals[BINSTRLEN];
int len;
} binStack;
int bsPush(binStack *st, char val) {
if (st->len >= BINSTRLEN)
return -1;
st->vals[st->len++] = val;
return 0;
}
int bsPop(binStack *st) {
if (!st->len)
return -1;
return (int)st->vals[--st->len];
}
char *ulongToBin(unsigned long n) {
static binStack bs;
bs.len = 0;
int pos = 0;
int c;
while (n != 0) {
bsPush(&bs, (char)(n & 1) + '0');
n >>= 1;
}
while ((c = bsPop(&bs)) != -1)
_binstr[pos++] = (char)c;
_binstr[pos] = '\0';
return _binstr;
}
int32_t insertInto(int32_t M, int32_t N, int i, int j) {
int32_t mask = ((1 << (j + 1 - i)) - 1) << i;
return (N & ~mask) | (M<<i);
}
int main() {
int32_t N = 1<<10;
int32_t M = 19;
int32_t result = insertInto(M, N, 2, 6);
printf("Result: %s\n", ulongToBin(result));
return 0;
}