Set Specific Bit in Byte Array

Nevets

I want to know how to set a specific bit in a 16 byte array (128 bits).

For example ... if I wanted to set the 9th bit in the the array I would expect:

{00, 80, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}

If I wanted to set the 125th bit ...

{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 08}

I have looked into using bit shifting but got confused on how to bit shift with an array consisting of 128 bits. Is there a way to break down an array this size and evaluate in smaller chunks of bytes? Any help would be appreciated.

Sergey Kalinichenko

The process of selecting a specific bit consists of two steps:

  • Picking the byte, and then
  • Picking the bit.

Picking the byte is simple: all you need to do is dividing the bit index by the number of bits in a byte - i.e. dividing by eight:

int byteIndex = bitIndex / 8;

Now that you know what byte to use, calculate which bit you want to access. For that you need to compute a remainder of a division by eight, like this:

int bitInByteIndex = bitIndex % 8;

With these two indexes in hand, it is easy to access the bit: use 1 << bitInByteIndex as a mask, like this:

byte mask = (byte)(1 << bitInByteIndex);
bool isSet = (bytes[byteIndex] & mask) != 0;
// set to 1
bytes[byteIndex] |= mask;
// Set to zero
bytes[byteIndex] &= ~mask;
// Toggle
bytes[byteIndex] ^= mask;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modify specific bit in byte

From Dev

Bit expand byte array

From Dev

Byte to "Bit"array

From Dev

Set specific byte in DWORD

From Dev

Set a specific bit in an int

From Dev

Count bit/byte size of array

From Dev

Set a double to byte array

From Dev

How to access a specific bit given a byte in C?

From Dev

How to flip a specific bit in a byte in C?

From Dev

How to flip a specific bit in a byte in C?

From Dev

Set 4 bits of a specific byte

From Dev

Hexadecimal: Set an 8-bit byte in an int

From Dev

Convert byte-array to a bit byte-array

From Dev

Bit shift operations on a byte array in Java

From Dev

Fast bit shift of a byte array - CMAC subkeys

From Dev

Byte array to an 8 bit truecolor image

From Dev

circular bit shifting in byte array in java

From Dev

Way to get the bit in position X of a byte array

From Dev

Is it safe to read and write on an array of 32 bit data byte by byte?

From Dev

How to put a specific byte from a byte array into a single byte

From Dev

Add byte array to another byte array at specific position java

From Dev

fast way to convert integer array to byte array (11 bit)

From Dev

Check whether a specific bit is set in Python 3

From Dev

Manually set NX bit of specific PTE

From Dev

VHDL Is there a cleaner way to set specific bit, given the bit number?

From Dev

VHDL Is there a cleaner way to set specific bit, given the bit number?

From Dev

Check if byte has more than one bit set

From Dev

Counting zero b-bit subsequences in a byte array

From Dev

How to convert string to a 32-bit aligned byte array in java?