forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR IntelRealSense#12760 from SamerKhshiboun: add byte manipulation fu…
…nctions
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
third-party/rsutils/include/rsutils/number/byte-manipulation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2024 Intel Corporation. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
namespace rsutils | ||
{ | ||
namespace number | ||
{ | ||
uint8_t reverse_bits(uint8_t b); | ||
void reverse_byte_array_bits(uint8_t* byte_array, int size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2024 Intel Corporation. All Rights Reserved. | ||
|
||
#include <rsutils/number/byte-manipulation.h> | ||
|
||
namespace rsutils | ||
{ | ||
namespace number | ||
{ | ||
// A helper function that takes a byte and reverses its bits | ||
// e.g. intput: 00110101 output: 10101100 | ||
|
||
// First the left four bits are swapped with the right four bits. | ||
// Then all adjacent pairs are swapped and then all adjacent single bits. | ||
// This results in a reversed order. | ||
uint8_t reverse_bits(uint8_t b) | ||
{ | ||
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; | ||
b = (b & 0xCC) >> 2 | (b & 0x33) << 2; | ||
b = (b & 0xAA) >> 1 | (b & 0x55) << 1; | ||
return b; | ||
} | ||
|
||
// A helper function that takes a byte array of a given size, | ||
// and reverses the bits order of for each byte in this array | ||
// e.g. intput: byteArray = {byte0=00110101, byte1=11110000}, size = 2 | ||
// output: byteArray [ byte0=10101100, byte1=00001111] | ||
void reverse_byte_array_bits(uint8_t* byte_array, int size) { | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
byte_array[i] = reverse_bits(byte_array[i]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2024 Intel Corporation. All Rights Reserved. | ||
|
||
//#cmake:dependencies rsutils | ||
|
||
#include <unit-tests/test.h> | ||
#include <rsutils/number/byte-manipulation.h> | ||
|
||
using namespace rsutils::number; | ||
|
||
namespace { | ||
|
||
|
||
TEST_CASE( "test reversing single byte - case 1" ) | ||
{ | ||
uint8_t byte = 0x55; // b'01010101 | ||
uint8_t reversed_byte = 0xAA; // b'10101010 | ||
CHECK(reverse_bits(byte) == reversed_byte); // little-endian | ||
} | ||
|
||
TEST_CASE("test reversing single byte - case 2") | ||
{ | ||
uint8_t byte = 0x1; // b'00000001 | ||
uint8_t reversed_byte = 0x80; // b'10000000 | ||
CHECK(reverse_bits(byte) == reversed_byte); | ||
} | ||
|
||
TEST_CASE("test reversing single byte - case 3") | ||
{ | ||
uint8_t byte = 0x0; // b'00000000 | ||
uint8_t reversed_byte = 0x0; // b'00000000 | ||
CHECK(reverse_bits(byte) == reversed_byte); | ||
} | ||
|
||
TEST_CASE("test reversing single byte - case 4") | ||
{ | ||
uint8_t byte = 0xFF; // b'11111111 | ||
uint8_t reversed_byte = 0xFF; // b'11111111 | ||
CHECK(reverse_bits(byte) == reversed_byte); | ||
} | ||
|
||
TEST_CASE("test reversing array of bytes of size 1") | ||
{ | ||
std::vector<uint8_t> bytes_arr = { 0x55 }; // { b'01010101 } | ||
uint8_t reversed_byte_at_0 = 0xAA; // b'10101010 | ||
reverse_byte_array_bits(bytes_arr.data(), 1); | ||
CHECK(bytes_arr[0] == reversed_byte_at_0); // little-endian | ||
} | ||
|
||
TEST_CASE("test reversing array of bytes of size 4") | ||
{ | ||
std::vector<uint8_t> bytes_arr = { 0x55, 0x01, 0x80, 0xF0 }; | ||
// bytes_arr = { b'01010101, b'00000001, b'10000000, b'11110000 } | ||
std::vector<uint8_t> expected_reversed_bytes = { 0xAA, 0x80, 0x01, 0xF }; | ||
// expected_reversed_bytes = { b'10101010, b'10000000, b'00000001, b'00001111} | ||
|
||
reverse_byte_array_bits(bytes_arr.data(), static_cast<int>(bytes_arr.size())); | ||
for (int i = 0; i < bytes_arr.size(); ++i) | ||
{ | ||
CHECK(bytes_arr[i] == expected_reversed_bytes[i]); | ||
} | ||
} | ||
|
||
|
||
} |