Skip to content

One Integer instead of many Booleans?

August 23, 2011
tags:

Here is some Software Development stuff!

Solving some issues today I have came across to unusuall (for me) code block. It had to check some parameters as booleans, but instead used a single integer to do it:

iLeavingTime = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_HOURLY_SPEED) == 0) ? (7 * 3600) : ((iLeavingTime / 3600) * 3600);
dHeight_mm = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_VEHICLE_HEIGHT) == 0) ? (3000) : (((int)dHeight_mm / 1000) * 1000);
dWeight_kg = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_VEHICLE_WEIGHT) == 0) ? (10000) : (((int)dWeight_kg / 1000) * 1000);
bHasHazardous = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_HAZARDOUS_LOAD) == 0) ? false : bHasHazardous;
bVehicleIsVignetteValid = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_TOLL_ROAD) == 0) ? true : bVehicleIsVignetteValid;
iMaxSpeed = (rDistanceControl.m_lRegardMapConstraints & long(REGARD_VEHICLE_MAX_SPEED) == 0) ? (90) : ((iMaxSpeed / 10) * 10);

At first I was a bit confused, why would it have

(rDistanceControl.m_lRegardMapConstraints & long(REGARD_VEHICLE_MAX_SPEED) == 0)

instead of simple boolean? Well, someone was very cheap on memory I guess. I would never write a code like that, but it’s fun to encounter something that reminds me ASM class in first semester, where it was a goal to optimize everything no matter what.

So here is how it is implemented.

First we have an enum of some properties that would be booleans in normal cases:

typedef	enum {
	REGARD_HOURLY_SPEED = 1,
	REGARD_VEHICLE_MAX_SPEED = 2,
	REGARD_VEHICLE_WEIGHT = 4,
	REGARD_VEHICLE_HEIGHT = 8,
	REGARD_HAZARDOUS_LOAD = 16,
	REGARD_TOLL_ROAD = 32
} rtRegardMapConstraints;

Note that they are all 2 in power of an integer (2^0, 2^1, 2^2, …,  2^5). Otherwise it wouldn’t work. Why? Well if you ask this, maybe this post is useful for someone (you to be precise). Go out and find out and don’t write any code more until you do (here are some hints: check binary operations and find out how integer is stored in computer memory).

Then to pass true/false value to the parameter, you would use something like this:

DistControl.m_lRegardMapConstraints = lLocalRegardHourlySpeed!= 0 ? (DistControl.m_lRegardMapConstraints | long(REGARD_HOURLY_SPEED)) : (DistControl.m_lRegardMapConstraints & ~long(REGARD_HOURLY_SPEED));

where

lLocalRegardHourlySpeed

is just some local variable that is passed to this DistControl object. Then continue same stuff for all the other parameters.

Is it worth it? Well in my opinion — definitely not. Code gets much more complicated (just look at this crap!) and I doubt it is any more effective than 6 booleans. And even if it is, the difference is so small no one would ever notice, it will be hidden under heavy computations of VRP.

But should I rewrite it? Well, there are many other tasks, so I will not in the end.

Advertisements
2 Comments leave one →
  1. October 24, 2011 20:47

    I’ve stuck upon one C++ article about handling boolean flags in more elegant way:
    http://www.codeproject.com/Tips/268433/Removing-Ugly-Flags-in-Cplusplus

    And your post was the first thing which instantly poped out of my memories of recent past=)
    Wouldn’t it help you, don’t mark me as the spammer, please:)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: