The abstractions we build in computer science are meant to sweep away painful implementation details for any program, to make them someone else's problem. On real systems it's not always that simple. In the real world, it is sometimes necessary to get down in the muck and reinvent conventions in order to reinforce the foundations of a particular project.
Take, for instance, the an example sent in by Corey.
I work on this fantastic system, where a particular DB table needs to store some 25 flags which are part of configuration data. Instead of using named bit fields, these flags are stored as "machine-hex" strings representing the bitmap. Machine-hex is like hex, except that it is [read] little-endian and ':', ';', '<', '=', '>', and '?' denote the digits after 9. Thus the string '4<1>:0' stores the flags 0000 1010 1110 0001 1100 0100.
Apparently, the master coder that conjured up machine-hex designed the user interface for the database; on top of that, he must have been very hungry because he was thinking about spaghetti. Also, he is no longer with the company, which is how Corey came to maintain the code.
To aid development, the hungry coder added many utility classes --- all slightly different --- to help other coders deal with machine-hex.
/// Base class for encoding 2 boolian values as 4 int values /// Uses default LOGAND internal class CodeBit4X2 { protected bool bBit0; protected bool bBit1; protected bool[,] bX= new bool[4, 2]; public CodeBit4X2() { InitBits(); } /// Override to provide custom configuation protected virtual void InitBits() { // Default configuation LOGAND bX[0, 0] = false; bX[0, 1] = false; bX[1, 0] = true; bX[1, 1] = false; bX[2, 0] = false; bX[2, 1] = true; bX[3, 0] = true; bX[3, 1] = true; } public char ByteCharVal { get { return Convert.ToChar(ByteVal + 48); } } /// 2 boolians as int value public int ByteVal { get { // Match against values set in array int ibyte = 0; for (int i=0; i < 4 ; i++ ) { if ( bBit0 == bX[i,0] && bBit1 == bX[i,1] ) { ibyte = i; break; } } return ibyte; } set { ///Set values from array bBit0 = bX[value,0]; bBit1 = bX[value,1]; } } }
CodeBit8x3 and CodeBit16x4 also make appearances. The really cool part is that there are a number of places where the configuration stores a true but the UI says false; in those cases, the solution is to subclass one of the base CodeBit classes and modify bX.