-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitwise.h
72 lines (51 loc) · 1.53 KB
/
bitwise.h
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
//
// Created by bear on 2021-03-30.
//
#ifndef ECS_BITWISE_H
#define ECS_BITWISE_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef uint64_t bits64;
#define BITWISE_IS_COVER(bits,b) ((bits | b) == bits)
#define BITWISE_SET_TRUE_AT(bits,index) (bits | (((bits64)1) << index))
#define BITWISE_SET_FALSE_AT(bits,index) (bits & (~(((bits64)1) << index)))
#define BITWISE_IS_TRUE_AT(bits,index) (bitwise_set_true_at(bits, index) == bits)
#define BITWISE_IS_FALSE_AT(bits,index) (!bitwise_is_true_at(bits,index))
#define BITWISE_VALUE_AT(bits,index) (bitwise_is_true_at(bits,index))
static inline bits64 char_to_bits(const char *s)
{
bits64 i = 0;
while (*s) {
i <<= 1;
i += *s++ - '0';
}
return i;
}
static inline bits64 bits_to_char(bits64 bits)
{
for (int bit_index = sizeof(bits)-1; bit_index >= 0; --bit_index)
{
bool bit = bits >> bit_index & true;
printf("%d", bit);
}
}
bits64 bitwise_contain(bits64 bits, bits64 b){
return (bits | b) == bits;
}
bits64 bitwise_set_true_at(bits64 bits, int index){
return bits | (((bits64)1) << index);
}
bits64 bitwise_set_false_at(bits64 bits, int index){
return bits & (~(((bits64)1) << index));
}
bool bitwise_is_true_at(bits64 bits, int index){
return bitwise_set_true_at(bits, index) == bits;
}
bool bitwise_is_false_at(bits64 bits, int index){
return !bitwise_is_true_at(bits,index);
}
bool bitwise_value_at(bits64 bits, int index){
return bitwise_is_true_at(bits,index);
}
#endif //ECS_BITWISE_H