A Bit Unwise

Was "Calvin code" genius or tomfoolery?

"Wait a second -- only after the price of diesel reaches four bucks a gallon does anybody notice a bug in the fuel efficiency reports?" John C. asked incredulously.

"Yep. We're talking about old Calvin code," his manager said.

John's employer sold vehicle fleet tracking units. The company's founders hired Calvin to write the firmware that ran on the vehicles and the receiving applications on the server side.

Forbidden Territory
John wished that the problem was with the reports, or any part of the code developed after the Calvin era. No such luck: The problem was Calvin's C# server-side code -- specifically, the part responsible for determining the engine's running status.

Everybody agreed that Calvin was smart, but he liked to show off.

Why limit yourself to true and false when magic numbers like 319 and 329 would do? And despite his "wizard" status, he seemed to have a tenuous grip on the concepts of binary, hex and bitwise logic.

After tracking down the code responsible for detecting the ignition status, John knew he would be in for a headache later on:

public string inputDescription(int iDec) {
  int numbase = 2, base10 = 10, MaxBit = 32;
  if (iDec == 0)
    return "Engine OFF";
  char[] cHexa = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
  int[] iHexaNumeric = new int[] { 10, 11, 12, 13, 14, 15 };
  int[] iHexaIndices = new int[] { 0, 1, 2, 3, 4, 5 };
  string strBin = "", description = "Engine OFF";;
  int[] result = new int[32];
  for (; iDec > 0; iDec /= numbase)
    int rem = iDec % numbase;
    result[--MaxBit] = rem;
  for (int i = 0; i < result.length;="" i++)="" if="" ((int)result.getvalue(i)="">= base10)
      strBin += cHexa[(int)result.GetValue(i) % base10];
      strBin += result.GetValue(i);
  strBin = strBin.TrimStart(new char[] { '0' });

  bool ignition = false, input4 = false, input5 = false;
  if (strBin.Length == 2 || strBin.Length > 2)
    if (strBin.Substring(strBin.Length - 2, 1) == "1")
      ignition = true;
      ignition = false;
  if (strBin.Length == 4 || strBin.Length > 4)
    if (strBin.Substring(strBin.Length - 4, 1) == "1")
      input5 = true;
      input5 = false;
  if (strBin.Length == 5 || strBin.Length > 5)
    if (strBin.Substring(strBin.Length - 5, 1) == "1")
      input4 = true;
      input4 = true;
  if (input4 == true && input5 == true && ignition == false)
    description = "Engine Stopped, Oil Pressure Low (Mechanical Failure)";
  else if (input4 == false && input5 == true && ignition == false)
    description = "Engine Stopped, Ignition ON (High water temperature)";
  else if (input4 == true && input5 == false && ignition == false)
    description = "Engine Switched OFF";
  else if (input4 == true && input5 == true && ignition == true)
    description = "Engine Runnning";
  return description;

Real Magic
John dove into the logic as pressure from the customers mounted. Every time he made some headway on regression testing -- different vehicles had their own magic numbers, of course -- something would break. On the verge of throwing his PC out of the nearest window and making a break for it, John experienced a breakthrough. Could there have been some real genius after all in Calvin's magic numbers?

John grabbed a calculator and a list of ignition codes, and sure enough, the solution was easy. Calvin had just taken the long route:

public static string inputDescription (int iDec)
  bool ignition = (iDec & 2) != 0;
  bool input4 = (iDec & 8) != 0;
  bool input5 = (iDec & 16) != 0;

  String description = "Engine OFF";

  if (input4 && input5 && !ignition)
    description = "Engine Stopped, Oil Pressure Low (Mechanical Failure)";
  else if (!input4 && input5 && !ignition)
    description = "Engine Stopped, Ignition ON (High water temperature)";
  else if (input4 && !input5 && !ignition)
    description = "Engine Switched OFF";
  else if (input4 && input5 && ignition)
    description = "Engine Running";

  return description;

After testing, John was satisfied his own "magic" had won the day. Unfortunately, that was only one function down -- there were hundreds to go.

About the Author

Mark Bowytz is a contributor to the popular Web site The Daily WTF. He has more than a decade of IT experience and is currently a systems analyst for PPG Industries.

comments powered by Disqus


  • .NET Core Ranks High Among Frameworks in New Dev Survey

    .NET Core placed high in a web-dominated ranking of development frameworks published by CodinGame, which provides a tech hiring platform.

  • Here's a One-Stop Shop for .NET 5 Improvements

    Culled from reams of Microsoft documentation, here's a high-level summary of what's new for performance, networking, diagnostics and more, along with links to the nitty-gritty details for those wanting to dig in more.

  • Azure SQL Database Ranked Among Top 3 Databases of 2020

    Microsoft touted the inclusion of Azure SQL Database among the top three databases of 2020 in a popularity ranking by DB-Engines, which collects and manages information about database management systems, updating its lists monthly.

  • Time Tracker Says VS Code Is No. 1 Editor for Devs, Some Working 15+ Hours Per Day

    WakaTime, which does time tracking for programmers, released data for 2020 showing that Visual Studio Code is by far the top editor/IDE used by its coders, some of whom are hacking away for more than 15 hours per day.

Upcoming Events