This commit is contained in:
@ -4,6 +4,7 @@ subtitle: "Tips and trick for embedded C/C++"
|
||||
summary: "Tips and trick for embedded C/C++"
|
||||
date: 2020-06-13T19:01:19+02:00
|
||||
highlight: true
|
||||
math: true
|
||||
---
|
||||
|
||||
# Set a bit
|
||||
@ -53,12 +54,21 @@ output = input << 3; // Multiply by 8
|
||||
output = (0b1 << X) -1;
|
||||
```
|
||||
|
||||
# Minimum required bits
|
||||
$$bits = { log(number) \over log(2) }$$
|
||||
Since you can not use fractions of a bit the number should be rounded up.
|
||||
|
||||
# Miscellaneous
|
||||
```c
|
||||
// Check if number is ODD
|
||||
if( (num & 1) == 1) {};
|
||||
// Flip signed integer
|
||||
num = ~num + 1;
|
||||
// Power of two
|
||||
// Is an integer a power of two?
|
||||
num > 0 && (num & (num - 1)) == 0;
|
||||
//Clear all bits from 0 to N
|
||||
mask = ~((1 << n+1 ) - 1);
|
||||
x &= mask;
|
||||
|
||||
|
||||
```
|
Reference in New Issue
Block a user