Coding: Visualising a bitmap
Over the last month or so I’ve spent some time each day reading a new part of the Neo4j code base to get more familiar with it, and one of my favourite classes is the Bits class which does all things low level on the wire and to disk.
In particular I like its toString method which returns a binary representation of the values that we’re storing in bytes, ints and longs.
I thought it’d be a fun exercise to try and write my own function which takes in a 32 bit map and returns a string containing a 1 or 0 depending if a bit is set or not.
The key insight is that we need to iterate down from the highest order bit and then create a bit mask of that value and do a bitwise and with the full bitmap. If the result of that calculation is 0 then the bit isn’t set, otherwise it is.
For example, to check if the highest order bit (index 31) was set our bit mask would have the 32nd bit set and all of the others 0’d out.
java> (1 << 31) & 0x80000000
java.lang.Integer res5 = -2147483648
If we wanted to check if lowest order bit was set then we’d run this computation instead:
java> (1 << 0) & 0x00000001
java.lang.Integer res7 = 0
java> (1 << 0) & 0x00000001
java.lang.Integer res8 = 1
Now let’s put that into a function which checks all 32 bits of the bitmap rather than just the ones we define:
private String asString( int bitmap )
{
StringBuilder sb = new StringBuilder();
sb.append( "[" );
for ( int i = Integer.SIZE - 1; i >= 0; i-- )
{
int bitMask = 1 << i;
boolean bitIsSet = (bitmap & bitMask) != 0;
sb.append( bitIsSet ? "1" : "0" );
if ( i > 0 && i % 8 == 0 )
{
sb.append( "," );
}
}
sb.append( "]" );
return sb.toString();
}
And a quick test to check it works:
@Test
public void shouldInspectBits()
{
System.out.println(asString( 0x00000001 ));
// [00000000,00000000,00000000,00000001]
System.out.println(asString( 0x80000000 ));
// [10000000,00000000,00000000,00000000]
System.out.println(asString( 0xA0 ));
// [00000000,00000000,00000000,10100000]
System.out.println(asString( 0xFFFFFFFF ));
// [11111111,11111111,11111111,11111111]
}
Neat!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.