# Flags, the Binary system and Logic operators.

This article teaches how and why to use "flags", and goes through the Binary System and Logic Operators to explain this. Please give feedback and/or vote if you think this could be of benefit to alot of programmers. I got the idea for this article while making an ActiveX DLL for a card game, so the I promise article is applicable to many real programming problems and I do practice what I preach. - Simon Price http://www.VBgames.co.uk

Original Author: Simon Price

`Flags`

# `Flags, the Binary system and Logic operators.`

`Introduction"Flags" are often used in programming when somethingcan be either "on" or "off", in other words, a Booleanvariable. This tutorial describes how and why to use flags. It uses Visual Basicfor example code, but this technique can be applied to all languages.An example situationHere is an imaginary form about the features of a futuristiccar. Please choose an option to show which feature you would like on your new car: [1] Jet boost [2] Invulnerability shield [3] Anti-Gravity generator [4] Missile proof glass [5] Instant stop brakes [6] Super grip tires [7] 10000 horsepower engine [8] Time warp drive We can think of each feature as a Boolean value, it is either onthe car, or not on the car.Lets imagine you want to store the results to your questionnairein 1 byte of data. This is simple, you just label the features from [1] throughto [8] and save that number. Job done? Not yet! Unfortunately, this method isnot very clever.The problemOne of your customers, Mr. Richguy, who has a lot of money tospend on his new car decides he wants TWO features on his car! Oh no, betterredesign that form with checkboxes instead of radio buttons. You slave awaydesigning your new form so that it looks like this. Please choose any number of option/s to show which feature/s you would like on your new car: [1] Jet boost [2] Invulnerability shield [3] Anti-Gravity generator [4] Missile proof glass [5] Instant stop brakes [6] Super grip tires [7] 10000 horsepower engine [8] Time warp drive Great! Now we can choose to have 1 feature, several features, orno features at all!The quick-fix solutionHow do we save this information? We could use an array of 8Boolean variables and save each one separately. That would work, but assumingyour code uses a whole byte to store a Boolean variable (as in C/C++/Delphietc., and VB I have heard uses 2 bytes!), that would take 8 bytes to store yourresults rather than just the 1 we had before. What's that you say? You have512MB SD RAM and 100GB hard drive? So you don't care about this memory wastage?Then I shall show you another problem with this method.Lets imagine we want to pass this information into a function.Maybe the function would calculate the cost of the car, given it's features. Youcould pass the Boolean variable for each feature into the function. It mightlook a bit like this (using VB code). Function CostOfCar(Feature1 As Boolean, Feature2 As Boolean, Feature3 As Boolean, Feature4 As Boolean, Feature5 As Boolean, Feature6 As Boolean, Feature7 As Boolean, Feature8 As Boolean) As Integer Dim x As Integer ?á?á ' do something with the feature list to calculate the cost of the car ?á?á CostOfCar = x End Function Doesn't look like a very neat function does it? Rather ugly in fact.The smart solutionThis is how it should be done! Using a whole byte to store a Boolean is awaste. We really only need 1 bit, a 0 representing False (or off) and a 1representing True (or on). Many of you will know how the Binary system and logicoperations work, but I will explain it here because it is important youunderstand these first.The Binary systemA bit can either be a 0 or a 1, on or off, true or false. A byte consists of8 bits. Each bit in the byte represents a different power of 2. In this way, abyte can store any number from 0 to 255. This table shows some examples. Notethat the "^" sign stands for "to the power of". Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Byte 2^0=1 2^1=2 2^2=4 2^3=8 2^4=16 2^5=32 2^6=64 2^7=128 Total 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 21 0 1 1 1 1 0 0 0 30 1 0 0 0 0 0 0 1 129 0 1 1 0 0 0 0 1 134 0 0 0 0 0 0 1 1 194 1 0 0 1 1 1 1 1 249 1 1 1 1 1 1 1 1 255 Logical OperationsThe logical operators I will demonstrate here (there are more) are And &Or. All logic operators work on a bit level.The And operator takes two bits (we will call Bit1 and Bit2), and returns a 1only if Bit1 AND Bit2 are 1. This table shows all the possible outcomes of theAnd operator. Bit1 Bit2 Bit1 And Bit2 0 0 0 0 1 0 1 0 0 1 1 1 The Or operator takes two bits (again, Bit1 and Bit2), and returns a 1 ifeither Bit1 OR Bit2 are 1. This table shows all the possible outcomes of the Oroperator. Bit1 Bit2 Bit1 Or Bit2 0 0 0 0 1 1 1 0 1 1 1 1 As I said before, logical operators work on bits. So how comes this code isvalid? ' declare some byte variables Dim Byte1 As Byte Dim Byte2 As Byte Dim ByteResult As Byte ' give the bytes some values Byte1 = 51 Byte2 = 219 ' use the or operator ByteResult = ( Byte1 Or Byte2) ' show the result in the debug window Debug.Print ByteResult ' use the and operator ByteResult = ( Byte1 And Byte2) ' show the result in the debug window Debug.Print ByteResult This code works because the Or & And operators are working on each bit ofeach byte. Here is how the and Or operation worked. Bit Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Total Value [1] [2] [4] [8] [16] [32] [64] [128] [255] Byte1 1 1 0 0 1 1 0 0 51 Byte2 1 1 0 1 1 0 1 1 219 Byte1 Or Byte2 1 1 0 1 1 1 1 1 251 And this is how the And operation worked. Bit Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Total Value [1] [2] [4] [8] [16] [32] [64] [128] [255] Byte1 1 1 0 0 1 1 0 0 51 Byte2 1 1 0 1 1 0 1 1 219 Byte1 Or Byte2 1 1 0 0 1 0 0 0 19 If you have VB and ran this code, you should find the results 251 and 19 inyour debug window.Applying your new knowledge!Getting a bit bored of all these tables of 0's and 1's? Don't worry, youdidn't read all that for nothing! Lets go back to the problem of storing carfeatures. Instead of assigning each feature a value from [1] to [8], we insteaduse [2^0] to [2^7], like this. Please choose any option/s to show which feature/s you would like on your new car: [2^0=1] Jet boost [2^1=2] Invulnerability shield [2^2=4] Anti-Gravity generator [2^3=8] Missile proof glass [2^4=16] Instant stop brakes [2^5=32] Super grip tires [2^6=64] 10000 horsepower engine [2^7=128] Time warp drive Now, we use an enumeration to store the possible features. In Visual Basic,it would look something like this. Enum CAR_FEATURE ?á?á CF_JETBOOST = 1 ?á?á CF_SHIELD = 2 ?á?á CF_ANTIGRAVITY = 4 ?á?á CF_GLASS = 8 ?á?á CF_BRAKES = 16 ?á?á CF_TIRES = 32 ?á?á CF_ENGINE = 64 ?á?á CF_TIMEWARP = 128 End Enum Now we can store 8 features into just 1 byte! To do this, we just add all thefeatures up, using the + operator. For example, Mr. Richguy comes back to youronline car showroom now you have improved it to allow for multiple features, anddecides to buy a new car with jet boost, a shield, super grip tires and a timewarp.?á Dim Features As Byte Features = CF_JETBOOST + CF_SHIELD + CF_TIRES + CD_TIMEWARP Debug.Print Features This code stores all the features Mr. Richguy requested in a single byte, ofvalue 163, which could be saved in a file, or sent to function to add up thecost, or anything else you'd like to do with it.You new function to calculate the cost would look much simpler, somethinglike this. Function CostOfCar(Features As Byte) As Integer Dim x As Integer ?á?á ' do something with the features byte to calculate the cost of the car ?á?á CostOfCar = x End Function Note how each feature is actually represented by one of the bits in yourbyte.However, there comes a time when you want to reverse the operation and getthe features back out of the byte variable, such as when you load a file, or thefunction parses the features etc. How is this done? I didn't mention logicoperators for nothing!Imagine our function wants to test if there are super grip tires to be putonto the new car. What we would need to do is test whether the CF_TIRES was inthe byte. The number 32 (=2^5) is represented by Bit6 of your byte. To check isBit6 is present (=1), you can use the And operator. For example: ' see whether the car has super grip tires If (Features And CF_TIRES = CF_TIRES) Then ?á?á ' do something, like add some cost according to the size of the wheels etc. End If So how did this work? Well, what we did was to see whether And'ing thefeatures with tires caused the result of tires. To see why this works, look atthe table. ?á Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Total Features 1 1 0 0 0 1 0 1 163 CF_TIRES 0 0 0 0 0 1 0 0 32 Features And CF_TIRES 0 0 0 0 0 1 0 0 32 In a similar way, the Or operator could have been used to find whether tireswere in the features byte. ' see whether the car has super grip tires If (Features Or CF_TIRES) = Features Then ?á?á ' do something, like add some cost according to the size of the wheels etc. End If So how did it work again? Well, what we did was to see whether Or'ing thefeatures with tires caused the result of features. To see why this also works,look at the table. ?á Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Total Features 1 1 0 0 0 1 0 1 163 CF_TIRES 0 0 0 0 0 1 0 0 32 Features And CF_TIRES 1 1 0 0 0 1 0 1 163 It doesn't really matter whether you use the And operator or the Or operatorto do this, but this process is often called "Masking" bits.SummaryYou have learnt about storing multiple Boolean variables ("flags")in a byte, why you should do that, and how to apply that to a real programmingsituation.More Ideas...What's that? Your new super high-tech futuristic cars have up to 16 availablefeatures? No problem!You can apply all the same techniques using 16 bit (= 2 byte) numbers, oftencalled integers.And so on... you could store 32 flags in a 32 bit (= 4 byte) integer (oftencalled a long integer).If you want another example of how to apply these techniques, I will tell youabout why I got the idea for this tutorial. I am working on several card gamesby contract and my main task is to write a general, reusable ActiveX DLL whichcan be used in all the card games. One of the features is to provide variedanimations on the cards, such as translation, spinning, resizing etc. Sometimesyou will want to apply more than one animation at once. To call the DLL toperform an animation, you can write code as simple as this:StartAnimation (MOVE + SPIN + RESIZE)The DLL can then figure out which flags were passed to it all in one go.Notice how you have just told the card to move, spin and resize in just one lineof code. So apart from the run time advantages of using flags, this also makesit easier for programmers using the DLL.Tutorial by Simon Price 31/07/01Simon@VBgames.co.uk`

`http://www.VBgames.co.uk`

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 104 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Loading Comments ...

## Comments

No comments have been added for this post.

You must be logged in to make a comment.