FastArduino  v1.8
C++ library to build fast but small Arduino/AVR projects
bits.h
Go to the documentation of this file.
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
21 #ifndef BITS_HH
22 #define BITS_HH
23 
24 #include <stdint.h>
25 
33 namespace bits
34 {
41  static constexpr uint8_t BV8(uint8_t bit)
42  {
43  return uint8_t(1 << bit);
44  }
45 
54  static constexpr uint8_t BV8(uint8_t bit1, uint8_t bit2)
55  {
56  return uint8_t(BV8(bit1) | BV8(bit2));
57  }
58 
69  static constexpr uint8_t BV8(uint8_t bit1, uint8_t bit2, uint8_t bit3)
70  {
71  return uint8_t(BV8(bit1, bit2) | BV8(bit3));
72  }
73 
86  static constexpr uint8_t BV8(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4)
87  {
88  return uint8_t(BV8(bit1, bit2) | BV8(bit3, bit4));
89  }
90 
105  static constexpr uint8_t BV8(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4, uint8_t bit5)
106  {
107  return uint8_t(BV8(bit1, bit2, bit3) | BV8(bit4, bit5));
108  }
109 
126  static constexpr uint8_t BV8(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4, uint8_t bit5, uint8_t bit6)
127  {
128  return uint8_t(BV8(bit1, bit2, bit3) | BV8(bit4, bit5, bit6));
129  }
130 
137  static constexpr uint8_t CBV8(uint8_t bit)
138  {
139  return uint8_t(~BV8(bit));
140  }
141 
148  static constexpr uint16_t BV16(uint8_t bit)
149  {
150  return uint16_t(1 << bit);
151  }
152 
161  static constexpr uint16_t BV16(uint8_t bit1, uint8_t bit2)
162  {
163  return uint16_t(BV16(bit1) | BV16(bit2));
164  }
165 
176  static constexpr uint16_t BV16(uint8_t bit1, uint8_t bit2, uint8_t bit3)
177  {
178  return uint16_t(BV16(bit1, bit2) | BV16(bit3));
179  }
180 
193  static constexpr uint16_t BV16(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4)
194  {
195  return uint16_t(BV16(bit1, bit2) | BV16(bit3, bit4));
196  }
197 
212  static constexpr uint16_t BV16(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4, uint8_t bit5)
213  {
214  return uint16_t(BV16(bit1, bit2, bit3) | BV16(bit4, bit5));
215  }
216 
233  static constexpr uint16_t BV16(uint8_t bit1, uint8_t bit2, uint8_t bit3, uint8_t bit4, uint8_t bit5, uint8_t bit6)
234  {
235  return uint16_t(BV16(bit1, bit2, bit3) | BV16(bit4, bit5, bit6));
236  }
237 
244  static constexpr uint16_t CBV16(uint8_t bit)
245  {
246  return uint16_t(~BV16(bit));
247  }
248 
253  static constexpr uint8_t COMPL(uint8_t value)
254  {
255  return uint8_t(~value);
256  }
257 
261  static constexpr uint8_t LOW_BYTE(uint16_t value)
262  {
263  return uint8_t(value & uint16_t(UINT8_MAX));
264  }
265 
269  static constexpr uint8_t HIGH_BYTE(uint16_t value)
270  {
271  return uint8_t(value >> 8);
272  }
273 }
274 
275 #endif /* BITS_HH */
276 
bits::COMPL
static constexpr uint8_t COMPL(uint8_t value)
Return the uint8_t 2-complement of a byte.
Definition: bits.h:253
bits::CBV16
static constexpr uint16_t CBV16(uint8_t bit)
Create a uint16_t inverted bitmask for the given bit number.
Definition: bits.h:244
bits::BV8
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
Definition: bits.h:41
bits::HIGH_BYTE
static constexpr uint8_t HIGH_BYTE(uint16_t value)
Extract the high byte (aka Most Significant Byte, MSB) of a uint16_t value.
Definition: bits.h:269
bits::CBV8
static constexpr uint8_t CBV8(uint8_t bit)
Create a uint8_t inverted bitmask for the given bit number.
Definition: bits.h:137
bits
Defines utility methods for bits manipulation.
Definition: bits.h:34
bits::BV16
static constexpr uint16_t BV16(uint8_t bit)
Create a uint16_t bitmask for the given bit number.
Definition: bits.h:148
bits::LOW_BYTE
static constexpr uint8_t LOW_BYTE(uint16_t value)
Extract the low byte (aka Least Significant Byte, LSB) of a uint16_t value.
Definition: bits.h:261