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

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 improvements 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;


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

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

Managing Bit Strings in Byte Arrays

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 - Testing Program

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials


 Full Version in PDF/EPUB