Skip to content

Commit

Permalink
add byte manipulation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamerKhshiboun committed Mar 17, 2024
1 parent f3724c4 commit e55aa58
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions third-party/rsutils/include/rsutils/byte-manipulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.

#pragma once

namespace rsutils
{
namespace byte
{
unsigned char reverse_bits(unsigned char b);
void reverse_byte_array_bits(unsigned char* byte_array, int size);
}
}
36 changes: 36 additions & 0 deletions third-party/rsutils/src/byte-manipulation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2024 Intel Corporation. All Rights Reserved.

#include <rsutils/byte-manipulation.h>

namespace rsutils
{
namespace byte
{
// 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.
unsigned char reverse_bits(unsigned char 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 rray of a given size,
// and reverses the bits order of for each byte of this array
// e.g. intput: byteArray = {byte0=00110101, byte1=11110000}, size = 2
// output: byteArray [ byte0=10101100, byte1=00001111]
void reverse_byte_array_bits(unsigned char* byte_array, int size) {
for (int i = 0; i < size; ++i)
{
byte_array[i] = reverse_bits(byte_array[i]);
}
}
}
}

0 comments on commit e55aa58

Please sign in to comment.