mirror of
https://github.com/aicodix/code.git
synced 2026-04-27 14:30:36 +00:00
added bit manipulation helpers
This commit is contained in:
parent
1f354379d3
commit
abe4edc419
2 changed files with 49 additions and 0 deletions
45
bitman.hh
Normal file
45
bitman.hh
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
Bit manipulation of byte arrays
|
||||
|
||||
Copyright 2018 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#ifndef BITMAN_HH
|
||||
#define BITMAN_HH
|
||||
|
||||
namespace CODE {
|
||||
|
||||
void xor_be_bit(uint8_t *buf, int pos, bool val)
|
||||
{
|
||||
buf[pos/8] ^= val<<(7-pos%8);
|
||||
}
|
||||
|
||||
void xor_le_bit(uint8_t *buf, int pos, bool val)
|
||||
{
|
||||
buf[pos/8] ^= val<<(pos%8);
|
||||
}
|
||||
|
||||
void set_be_bit(uint8_t *buf, int pos, bool val)
|
||||
{
|
||||
buf[pos/8] = (~(1<<(7-pos%8))&buf[pos/8])|(val<<(7-pos%8));
|
||||
}
|
||||
|
||||
void set_le_bit(uint8_t *buf, int pos, bool val)
|
||||
{
|
||||
buf[pos/8] = (~(1<<(pos%8))&buf[pos/8])|(val<<(pos%8));
|
||||
}
|
||||
|
||||
bool get_be_bit(uint8_t *buf, int pos)
|
||||
{
|
||||
return (buf[pos/8]>>(7-pos%8))&1;
|
||||
}
|
||||
|
||||
bool get_le_bit(uint8_t *buf, int pos)
|
||||
{
|
||||
return (buf[pos/8]>>(pos%8))&1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue