Menu

Bashing Bits About

October 8, 2016 - .NET, C#, Programming

One of the old standby’s of software development is manipulating bits within bytes. While it used to be that this was necessary- when you only have 8K of RAM, you have to make the most of it, which often meant packing information. Nowadays, it’s not quite as necessary, since there is so much more RAM on a typical system and it’s generally not worth the loss of performance that would come from packing unpacking bits for the tiny memory savings that would be afforded.

There are, of course, exceptions. Sometimes, trying to use conventional data types together with functions or other operations that work with compact data representations (For example, generating a Product Key) can be as awkward as Hitler at a Bar Mitzvah. In those cases, it can be quite useful to be able to pack several Boolean true/false values into a single byte. But in order to do so, we need to do some bit bashing.

Bit Bashing

“Bit Bashing” is the rather crude term for Bit manipulation, which is effectively what it says on the tin- the manipulation of the bits making up the bytes. Even the oldest Microcomputer CPUs- the Intel 4004, for example- worked with more than one bit at a time, in the case of the 4004, it worked with 4-bits at a time. The original IBM PC worked with a full byte. This means that operations work at a higher level even then the bit, so it takes some trickery to work at that level.

The core concepts are easy- you can use bitwise operators on a byte in order to set or retrieve individual bits of the byte.

Setting a Bit

Setting a bit is a different operation depending on whether the bit is being cleared or set (0 or 1). If the bit is being set, one can do so by using a bitwise “or” operation with a shifted value based on the desired bit to change:

Straightforward- create a byte based on the specified bit index (0 through 7) the bitwise or that against the original to force that bit to be set in the result.

The converse is similar, though a teensy bit more complicate. to forcibly set a bit to 0, we need to perform a bitwise “and” operation not against the shifted value, but against the bitwise complement of the shifted value:

Retrieving a bit

Retrieving a bit is as simple as seeing if the bitwise and between the value and the shifted byte is non-zero:

Being able to encode and decode bits from a byte can be a useful capability for certain tasks even if it’s necessity due to memory constraints may have long since passed.

Have something to say about this post? Comment!