Skip to content

Commit dcdc4b9

Browse files
committed
0.1.3 bitHelpers
1 parent 1592481 commit dcdc4b9

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed

libraries/bitHelpers/README.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Arduino library with functions on bit level
1010

1111
## Description
1212

13-
This library contains functions to manipulate bits and bitpatterns in an
13+
This library contains functions to manipulate bits and bit patterns in an
1414
efficient way.
1515
For most functions a 8 - 64 bit optimized version exist.
1616

@@ -25,34 +25,36 @@ New bit functions can be added or investigated, please post an issue.
2525

2626
### 0.1.0
2727
BitCount, several implementations to compare performance.
28-
- **bitCountReference(uint32_t val)** returns nr of bits set in a value.
29-
- **bitCountKR(uint32_t val)** Kerningham bitCount
30-
- **bitCountArray(uint32_t val)** count per nybble with lookup table
31-
- **bitCountF1(uint32_t val)** SWAG algorithm variant
32-
- **bitCountF2(uint32_t val)** SWAG algorithm variant
28+
- **uint8_t bitCountReference(uint32_t val)** returns nr of bits set in a value.
29+
- **uint8_t bitCountKR(uint32_t val)** Kerningham Ritchie bitCount
30+
- **uint8_t bitCountArray(uint32_t val)** count per nybble with lookup table
31+
- **uint8_t bitCountF1(uint32_t val)** SWAG algorithm variant
32+
- **uint8_t bitCountF2(uint32_t val)** SWAG algorithm variant
3333

34-
BitCount - fastest version: uint8_t .. uint64_t
35-
- **bitCount(val)**
34+
BitCount - fastest version, SWAG algorithm
35+
- **uint8_t bitCount(uint8_t val)** available for 16, 32 and 64 bit.
3636

3737
Reverse: uint8_t .. uint64_t
38-
- **bitReverse()** reverses bits in a uint8_t .. uint64_t
39-
- **nybbleReverse()** reverses nybbles (4 bit) in a uint8_t .. uint64_t
40-
- **byteReverse()** reverses bytes (8 bit) in a uint16_t .. uint64_t
41-
- **wordReverse()** reverses words (16 bit) in uint32_t and uint64_t
38+
- **T bitReverse(T val)** reverses bits in a uint8_t .. uint64_t
39+
- **T nybbleReverse(T val)** reverses nibbles (4 bit) in a uint8_t .. uint64_t
40+
- **T byteReverse(T val)** reverses bytes (8 bit) in a uint16_t .. uint64_t
41+
- **T wordReverse(T val)** reverses words (16 bit) in uint32_t and uint64_t
4242

4343
Swap upper and lower half: uint8_t .. uint64_t
44-
- **swap()** 0x12345678 ==> 0x56781234
44+
- **T swap(T val)** 0x12345678 ==> 0x56781234
4545

4646
Rotate Left / Right: uint8_t .. uint64_t
47-
- **bitRotateLeft(value, pos)**
48-
- **bitRotateRight(value, pos)**
47+
if pos larger than # bits original value is returned.
48+
- **T bitRotateLeft(T value, uint8_t pos)**
49+
- **T bitRotateRight(T value, uint8_t pos)**
4950

5051
BitFlip: uint8_t .. uint64_t a.k.a toggle
51-
- **bitFlip(value, pos)** flips a single bit at pos
52+
if pos larger than # bits original value is returned.
53+
- **T bitFlip(T value, uint8_t pos)** flips a single bit at pos
5254

5355
BitRot: uint8_t .. uint64_t
54-
- **bitRot(value, chance)** random damage to a single bit of a value, chance = float 0.0 .. 1.0
55-
that one random bit is toggled.
56+
- **T bitRot(T value, float chance = 0.5)** random damage to a single bit of a value,
57+
chance = float 0.0 .. 1.0 that one random bit is toggled.
5658
**bitRot()** is a function that can be used to mimic single bit errors in communication protocols.
5759
*Note: a chance of 50% for 2 uint8_t is not equal to 50% chance for 1 uint16_t.*
5860

@@ -64,11 +66,11 @@ How many bits are needed to store / transmit a number?
6466

6567
The following functions are made as the normal **bitset()** etc do not work for 64 bit.
6668
These functions are optimized for speed for **AVR**, **ESP32** and **ESP8266**.
67-
- **bitSet64(x, bit)** set bit of uint64_t
68-
- **bitClear64(x, bit)** clear bit of uint64_t
69-
- **bitToggle64(x, bit)** toggle bit of uint64_t
70-
- **bitWrite64(x, bit, value)** set bit of uint64_t to 0 or 1
71-
- **bitRead64(x, bit)** reads bit from uint64_t
69+
- **void bitSet64(uint64 & x, uint8_t bit)** set bit of uint64_t
70+
- **void bitClear64(uint64 & x, uint8_t bit)** clear bit of uint64_t
71+
- **void bitToggle64(uint64 & x, uint8_t bit)** toggle bit of uint64_t
72+
- **void bitWrite64(uint64 & x, uint8_t bit, uint8_t value)** set bit of uint64_t to 0 or 1
73+
- **void bitRead64(uint64 & x, uint8_t bit)** reads bit from uint64_t
7274

7375
Also added are macro versions of these five functions.
7476
- **mbitSet64(x, bit)** set bit of uint64_t
@@ -79,7 +81,13 @@ Also added are macro versions of these five functions.
7981

8082
### 0.1.2 added
8183

82-
Added arduino-ci and unit tests
84+
Added Arduino-CI and unit tests
85+
86+
87+
### 0.1.3 added
88+
89+
- update readme.md
90+
- update unit tests
8391

8492

8593
## Future

libraries/bitHelpers/bitHelpers.h

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
//
33
// FILE: bitHelpers.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.2
5+
// VERSION: 0.1.3
66
// DATE: 2015-11-07
77
// PURPOSE: Arduino library with functions on bit level
88
// URL: https://github.com/RobTillaart/bitHelpers
99
//
1010
// 0.0.1 2015-11-07 initial version
11-
// 0.1.0 2020-07-29 intial release
11+
// 0.1.0 2020-07-29 initial release
1212
// 0.1.1 2020-08-10 added BitsNeeded, bitSet64 family
13-
// 0.1.2 2020-12-14 add Adruino-ci + unit tests
13+
// 0.1.2 2020-12-14 add Arduino-CI + unit tests
14+
// 0.1.3 2021-08-09 update readme.md + unit tests
15+
1416

1517
#include "Arduino.h"
1618

19+
1720
#define BH_BIG_NR 1000000000
1821

22+
1923
////////////////////////////////////////////////
2024
//
2125
// BIT COUNT TEST
@@ -130,6 +134,7 @@ uint8_t bitCount(uint64_t value)
130134
return v & 0x7F;
131135
};
132136

137+
133138
////////////////////////////////////////////////
134139
//
135140
// BIT REVERSE
@@ -176,6 +181,7 @@ uint64_t bitReverse(uint64_t val)
176181
return x;
177182
}
178183

184+
179185
////////////////////////////////////////////////
180186
//
181187
// NYBBLE REVERSE
@@ -242,6 +248,7 @@ uint64_t byteReverse(uint64_t val)
242248
return x;
243249
}
244250

251+
245252
////////////////////////////////////////////////
246253
//
247254
// WORD REVERSE
@@ -261,6 +268,7 @@ uint64_t wordReverse(uint64_t val)
261268
return x;
262269
}
263270

271+
264272
////////////////////////////////////////////////
265273
//
266274
// SWAP HI LO
@@ -285,11 +293,12 @@ uint64_t swap(uint64_t val)
285293
return (val << 32) | (val >> 32);
286294
}
287295

296+
288297
////////////////////////////////////////////////
289298
//
290299
// BIT ROTATE LEFT
291300
//
292-
uint8_t bitRotateLeft(uint8_t value, uint8_t pos)
301+
uint8_t bitRotateLeft(uint8_t value, uint8_t pos)
293302
{
294303
if (pos > 7) return value;
295304
return (value << pos) | (value >> (8 - pos));
@@ -313,6 +322,7 @@ uint64_t bitRotateLeft(uint64_t value, uint8_t pos)
313322
return (value << pos) | (value >> (64 - pos));
314323
}
315324

325+
316326
////////////////////////////////////////////////
317327
//
318328
// BIT ROTATE RIGHT
@@ -341,62 +351,65 @@ uint64_t bitRotateRight(uint64_t value, uint8_t pos)
341351
return (value << (64 - pos)) | (value >> pos);
342352
}
343353

354+
344355
////////////////////////////////////////////////////
345356
//
346357
// BIT FLIP
347358
//
348-
uint8_t bitFlip(uint8_t value, uint8_t pos)
359+
uint8_t bitFlip(uint8_t value, uint8_t pos)
349360
{
350361
if (pos > 7) return value;
351362
return value ^ (1 << pos);
352363
}
353364

354-
uint16_t bitFlip(uint16_t value, uint8_t pos)
365+
uint16_t bitFlip(uint16_t value, uint8_t pos)
355366
{
356367
if (pos > 15) return value;
357368
return value ^ (1 << pos);
358369
}
359370

360-
uint32_t bitFlip(uint32_t value, uint8_t pos)
371+
uint32_t bitFlip(uint32_t value, uint8_t pos)
361372
{
362373
if (pos > 31) return value;
363374
return value ^ (1UL << pos);
364375
}
365376

366-
uint64_t bitFlip(uint64_t value, uint8_t pos)
377+
uint64_t bitFlip(uint64_t value, uint8_t pos)
367378
{
368379
if (pos > 63) return value;
369380
return value ^ (1ULL << pos);
370381
}
371382

383+
372384
////////////////////////////////////////////////////
373385
//
374386
// BIT ROT
375387
//
376-
uint8_t bitRot(uint8_t value, float chance = 0.5)
388+
uint8_t bitRot(uint8_t value, float chance = 0.5)
377389
{
378390
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
379391
return value ^ (1 << random(8));
380392
}
381393

382-
uint16_t bitRot(uint16_t value, float chance = 0.5)
394+
uint16_t bitRot(uint16_t value, float chance = 0.5)
383395
{
384396
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
385397
return value ^ (1UL << random(16));
386398
}
387399

388-
uint32_t bitRot(uint32_t value, float chance = 0.5)
400+
uint32_t bitRot(uint32_t value, float chance = 0.5)
389401
{
390402
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
391403
return value ^ (1UL << random(32));
392404
}
393405

394-
uint64_t bitRot(uint64_t value, float chance = 0.5)
406+
uint64_t bitRot(uint64_t value, float chance = 0.5)
395407
{
396408
if (random(BH_BIG_NR) > chance * BH_BIG_NR) return value;
397409
return value ^ (1ULL << random(64));
398410
}
399411

412+
400413
////////////////////////////////////////////////////
401414
//
402415
// BIT-SET64 -CLEAR64 -TOGGLE64 -READ64 -WRITE64
@@ -411,6 +424,7 @@ uint64_t bitRot(uint64_t value, float chance = 0.5)
411424
#define mbitRead64(value, bit) ( ((value) & ((sizeof(value)<5?1UL:1ULL) <<(bit))) ? 1 : 0)
412425
#define mbitWrite64(value, bit, bitvalue) (bitvalue ? mbitSet64(value, bit) : mbitClear64(value, bit))
413426

427+
414428
// FUNCTIONS
415429
#if defined(__AVR__)
416430

@@ -480,6 +494,7 @@ void bitToggle64(uint64_t & x, uint8_t bit)
480494

481495
#endif
482496

497+
483498
uint8_t bitRead64(uint64_t & x, uint8_t bit)
484499
{
485500
return x & (1ULL << bit);
@@ -542,6 +557,7 @@ uint8_t bitsNeeded(uint64_t x)
542557
return bitsNeeded((uint32_t)x);
543558
}
544559

560+
545561
////////////////////////////////////////////////////
546562
//
547563
// NEXT

libraries/bitHelpers/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/bitHelpers.git"
1717
},
18-
"version": "0.1.2",
18+
"version": "0.1.3",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*"

libraries/bitHelpers/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=bitHelpers
2-
version=0.1.2
2+
version=0.1.3
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library with functions on bit level

libraries/bitHelpers/test/unit_test_001.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,26 @@ unittest(test_swap)
7575

7676
unittest(test_bitRotateLeft)
7777
{
78-
// TODO
78+
assertEqual(0x8A, bitRotateLeft((uint8_t)0x51, 3));
79+
assertEqual(0x4682, bitRotateLeft((uint16_t)0x3412, 13));
80+
assertEqual(0x1A2B3C09, bitRotateLeft((uint32_t)0x56781234, 23));
81+
assertEqual(0x456789ABCDEF0123, bitRotateLeft((uint64_t)0x89ABCDEF01234567, 48));
7982
}
8083

8184
unittest(test_bitRotateRight)
8285
{
83-
// TODO
86+
assertEqual(0x2A, bitRotateRight((uint8_t)0x51, 3));
87+
assertEqual(0xA091, bitRotateRight((uint16_t)0x3412, 13));
88+
assertEqual(0xF02468AC, bitRotateRight((uint32_t)0x56781234, 23));
89+
assertEqual(0x0123456789ABCDEF, bitRotateRight((uint64_t)0x89ABCDEF01234567, 32));
8490
}
8591

8692
unittest(test_bitFlip)
8793
{
88-
// TODO
94+
assertEqual(0x59, bitFlip((uint8_t)0x51, 3));
95+
assertEqual(0x1412, bitFlip((uint16_t)0x3412, 13));
96+
assertEqual(0x56F81234, bitFlip((uint32_t)0x56781234, 23));
97+
assertEqual(0x89ABC5EF01234567, bitFlip((uint64_t)0x89ABCDEF01234567, 43));
8998
}
9099

91100
unittest(test_bitsNeeded)

0 commit comments

Comments
 (0)