libschc
bit_operations.h
Go to the documentation of this file.
1 /*
2  * (c) 2018 - idlab - UGent - imec
3  *
4  * Bart Moons
5  *
6  * This file is part of the SCHC stack implementation
7  *
8  */
9 #ifndef _SCHC_BIT_H_
10 #define _SCHC_BIT_H_
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include <stdint.h>
17 
18 #include "schc.h"
19 
20 #define BYTES_TO_BITS(x) (x * 8)
21 #define BITS_TO_BYTES(x) (((x) == 0) ? 0 : (((x) - 1) / 8 + 1)) // bytes required for a number of bits
22 
23 // sets bits at a certain position in a bit array
24 void set_bits(uint8_t A[], uint32_t pos, uint32_t len);
25 
26 // get bits at a certain position in a bit array
27 uint32_t get_bits(const uint8_t A[], uint32_t pos, uint8_t len);
28 
29 // clear bits at a certain position in a bit array
30 void clear_bits(uint8_t A[], uint32_t pos, uint32_t len);
31 
32 // copy bits to a certain position in a bit array from another array
33 void copy_bits(uint8_t DST[], uint32_t dst_pos, const uint8_t SRC[], uint32_t src_pos, uint32_t len);
34 // void copy_bits_BIG_END(uint8_t DST[], uint32_t dst_pos, const uint8_t SRC[], uint32_t src_pos, uint32_t len);
35 
36 // compare two bit arrays
37 uint8_t compare_bits(const uint8_t SRC1[], const uint8_t SRC2[], uint32_t len);
38 uint8_t compare_bits_aligned(const uint8_t SRC1[], uint16_t pos1, const uint8_t SRC2[], uint16_t pos2, uint32_t len);
39 uint8_t compare_bits_BIG_END(uint8_t SRC1[], uint8_t SRC2[], uint32_t len);
40 
41 // shift a number of bits to the left
42 void shift_bits_left(uint8_t SRC[], uint16_t len, uint32_t shift);
43 
44  // shift a number of bits to the right
45 void shift_bits_right(uint8_t SRC[], uint16_t len, uint32_t shift);
46 
47 // logic xor two bit arrays
48 void xor_bits(uint8_t DST[], uint8_t SRC1[], uint8_t SRC2[], uint32_t len);
49 
50 // logic and two bit arrays
51 void and_bits(uint8_t DST[], uint8_t SRC1[], uint8_t SRC2[], uint32_t len);
52 
53 // print an array of bits
54 void print_bitmap(const uint8_t bitmap[], uint32_t length);
55 
56 // get the ceiled length in bytes
57 uint8_t get_number_of_bytes_from_bits(uint16_t number_of_bits);
58 
59 // return the number of 1-bits in the value
60 uint32_t get_required_number_of_bits(uint32_t value);
61 
62 // return the starting bit of a value
63 uint8_t get_position_in_first_byte(uint8_t value);
64 
65 // remove padding
66 uint8_t padded(schc_bitarray_t* bit_array);
67 
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif
shift_bits_right
void shift_bits_right(uint8_t SRC[], uint16_t len, uint32_t shift)
shift a number of bits to the right
Definition: bit_operations.c:202
schc_bitarray_t
Definition: schc.h:32
copy_bits
void copy_bits(uint8_t DST[], uint32_t dst_pos, const uint8_t SRC[], uint32_t src_pos, uint32_t len)
copy bits to a certain position in a bit array from another array big endian
Definition: bit_operations.c:82
set_bits
void set_bits(uint8_t A[], uint32_t pos, uint32_t len)
sets bits at a certain position in a bit array big endian
Definition: bit_operations.c:25
compare_bits
uint8_t compare_bits(const uint8_t SRC1[], const uint8_t SRC2[], uint32_t len)
compare two bit arrays
Definition: bit_operations.c:108
clear_bits
void clear_bits(uint8_t A[], uint32_t pos, uint32_t len)
clear bits at a certain position in a bit array big endian
Definition: bit_operations.c:63
and_bits
void and_bits(uint8_t DST[], uint8_t SRC1[], uint8_t SRC2[], uint32_t len)
logical AND two bit arrays
Definition: bit_operations.c:244
get_bits
uint32_t get_bits(const uint8_t A[], uint32_t pos, uint8_t len)
get bits at a certain position in a bit array
Definition: bit_operations.c:42
get_number_of_bytes_from_bits
uint8_t get_number_of_bytes_from_bits(uint16_t number_of_bits)
get the number of bytes required to store this amount of bits
Definition: bit_operations.c:274
schc.h
get_position_in_first_byte
uint8_t get_position_in_first_byte(uint8_t value)
get the starting bit of a value
Definition: bit_operations.c:305
compare_bits_BIG_END
uint8_t compare_bits_BIG_END(uint8_t SRC1[], uint8_t SRC2[], uint32_t len)
shift_bits_left
void shift_bits_left(uint8_t SRC[], uint16_t len, uint32_t shift)
shift a number of bits to the left
Definition: bit_operations.c:180
get_required_number_of_bits
uint32_t get_required_number_of_bits(uint32_t value)
get the number of bits required to store a value
Definition: bit_operations.c:288
padded
uint8_t padded(schc_bitarray_t *bit_array)
remove padding
Definition: bit_operations.c:322
xor_bits
void xor_bits(uint8_t DST[], uint8_t SRC1[], uint8_t SRC2[], uint32_t len)
logical XOR two bit arrays
Definition: bit_operations.c:227
compare_bits_aligned
uint8_t compare_bits_aligned(const uint8_t SRC1[], uint16_t pos1, const uint8_t SRC2[], uint16_t pos2, uint32_t len)
compare two bit arrays with starting point
Definition: bit_operations.c:133
print_bitmap
void print_bitmap(const uint8_t bitmap[], uint32_t length)
print a bitmap
Definition: bit_operations.c:259