- Testing Program

This section provides tutorial example program to test setBit(), getBit(), and rotateLeft() to manage bit strings in byte arrays.

To test my bit string methods, I wrote the following test program,

 - Copyright (c) 2014,, All Rights Reserved.
public class BitStringTest {
   public static void main(String[] arg) {
      byte[] inString = new byte[5]; 
      inString[0] = (byte) 0x00FF; // binary: 11111111  
      inString[1] = (byte) 0x0000; // binary: 00000000  
      inString[2] = (byte) 0x00FF; // binary: 11111111  
      inString[3] = (byte) 0x0000; // binary: 00000000  
      inString[4] = (byte) 0x0000; // binary: 00000000
      int length = 5*8; // 40 bits in total 

      int step = 6; // steps to rotate
      byte[] outString = rotateLeft(inString, length, step);
      printBytes(inString, " Input");
      printBytes(outString, "Output");
   private static byte[] rotateLeft(byte[] in, int len, int step) {
      int numOfBytes = (len-1)/8 + 1;
      byte[] out = new byte[numOfBytes];
      for (int i=0; i<len; i++) {
         int val = getBit(in,(i+step)%len);
      return out;
   private static int getBit(byte[] data, int pos) {
      int posByte = pos/8; 
      int posBit = pos%8;
      byte valByte = data[posByte];
      int valInt = valByte>>(8-(posBit+1)) & 0x0001;
      return valInt;
   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;
   private static void printBytes(byte[] data, String name) {
      for (int i=0; i<data.length; i++) {
         System.out.print(byteToBits(data[i])+" ");
   private static String byteToBits(byte b) {
      StringBuffer buf = new StringBuffer();
      for (int i=0; i<8; i++)
         buf.append((int)(b>>(8-(i+1)) & 0x0001));
      return buf.toString();

Here is the output of the test program:

11111111 00000000 11111111 00000000 00000000

11000000 00111111 11000000 00000000 00111111

I think getBit(), setBit(), and rotateLeft() are working correctly. What do you think?

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 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

 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

 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

 Outdated Tutorials


 PDF Printing Version