From 482e65c296e1390fd9129137263c830f4a39c781 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 19 Mar 2013 15:49:12 -0700 Subject: [PATCH] move randomBoolean and randomColorValue methods to SharedUtil --- shared/src/SharedUtil.cpp | 28 ++++++++++++++++++++++++++-- shared/src/SharedUtil.h | 8 +++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/shared/src/SharedUtil.cpp b/shared/src/SharedUtil.cpp index 85a7187c0c..404ac9d5cb 100644 --- a/shared/src/SharedUtil.cpp +++ b/shared/src/SharedUtil.cpp @@ -24,7 +24,16 @@ float randFloat () { return (rand() % 10000)/10000.f; } -void outputBits(char byte) { + +unsigned char randomColorValue(uint8_t miniumum) { + return miniumum + (rand() % (255 - miniumum)); +} + +bool randomBoolean() { + return rand() % 2; +} + +void outputBits(unsigned char byte) { printf("%d: ", byte); for (int i = 0; i < 8; i++) { @@ -32,4 +41,19 @@ void outputBits(char byte) { } printf("\n"); -} \ No newline at end of file +} + +int8_t numberOfOnes(unsigned char byte) { + return (byte >> 7) + + ((byte >> 6) & 1) + + ((byte >> 5) & 1) + + ((byte >> 4) & 1) + + ((byte >> 3) & 1) + + ((byte >> 2) & 1) + + ((byte >> 1) & 1) + + (byte & 1); +} + +bool oneAtBit(unsigned char byte, int8_t bitIndex) { + return (byte >> (7 - bitIndex) & 1); +} diff --git a/shared/src/SharedUtil.h b/shared/src/SharedUtil.h index e0094e1cf8..352ff1d859 100644 --- a/shared/src/SharedUtil.h +++ b/shared/src/SharedUtil.h @@ -19,7 +19,13 @@ double usecTimestamp(timeval *time); double usecTimestampNow(); + float randFloat(); -void outputBits(char); +unsigned char randomColorValue(uint8_t minimum); +bool randomBoolean(); + +void outputBits(unsigned char byte); +int8_t numberOfOnes(unsigned char byte); +bool oneAtBit(unsigned char byte, int8_t bitIndex); #endif /* defined(__hifi__SharedUtil__) */