Java Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 6.00

setBit() - Storing a Bit into a Byte Array

This section provides a tutorial example on how to set one bit into a byte array at a specific bit position setBit().

In many applications, information must be stored and operated as bit strings. Unfortunately, Java does not provide any built-in data types or classes to support bit strings. There are several third party BitString classes that you can use to store and manage your bit strings.

Another way to store and manage bit strings is to use byte arrays, byte[]. I have used them while implementing DES (Data Encryption Standard) encryption algorithm, see "Cryptography Tutorials - Herong's Tutorial Notes" at http://www.herongyang.com/crypto/.

Storing and managing bit strings in byte arrays is not that hard as long as you define a pair of setBit() and getBit() methods. Here is my set of methods used in my cryptography tutorials. Of course, there is room for improvement in these methods. Try it yourself.

1. setBit() - To set one bit to a bit string at the specified position with the specified bit value:

   private static void setBit(byte[] data, int pos, int val) {
      int posByte = pos/8; 
      int posBit = pos%8;
      byte oldByte = data[posByte];
      oldByte = (byte) (((0xFF7F>>posBit) & oldByte) & 0x00FF);
      byte newByte = (byte) ((val<<(8-(posBit+1))) | oldByte);
      data[posByte] = newByte;
   }

Explanations:

  • The method interface expects the caller to provide the byte array that stores the bit string, the position in the bit string where bit value needs to be set, and the new bit value given as a "int" value.
  • The first statement calculates the array index, "posByte", of the affected byte that holds the bit to be set.
  • The second statement calculates the bit position, "posBit", in the affected byte where the bit value needs to be set.
  • The third statement fetches the affected byte into a temporary variable, "oldByte".
  • The fourth statement sets a 0 value to "oldByte" at the bit position of "posBit".
  • The fifth statement sets the input "val" to "oldByte" at the bit position of "posBit" and assigns to "newByte".

To understand how the fourth statement works, let's assume that posBit = 2. Then the evaluation process of the statement can be described as:

0xFF7F    : 11111111 11111111 11111111 01111111 
>> 2
-----------------------------------------------
          = 11111111 11111111 11111111 11011111 
& oldByte : ???????? ???????? ???????? xxxxxxxx
-----------------------------------------------
          = ???????? ???????? ???????? xx0xxxxx
& 0x00FF  : 00000000 00000000 00000000 11111111
-----------------------------------------------
          = 00000000 00000000 00000000 xx0xxxxx

The fifth statement can be described as:

val       : 00000000 00000000 00000000 0000000y 
<< 5
-----------------------------------------------
          = 00000000 00000000 00000000 00y00000
| oldByte : 00000000 00000000 00000000 xx0xxxxx
-----------------------------------------------
          = 00000000 00000000 00000000 xxyxxxxx

Sections in This Chapter

setBit() - Storing a Bit into a Byte Array

getBit() - Retrieving a Bit from a Byte Array

rotateLeft() - Left Rotating All Bits in a Byte Array

bitStringTest.java - Testing Program

Dr. Herong Yang, updated in 2008
setBit() - Storing a Bit into a Byte Array