# Flags, the Binary system and Logic operators.

Email
 Submitted on: 1/18/2015 8:58:00 PM By: Simon Price (from psc cd) Level: Intermediate User Rating: By 19 Users Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel Views: 651

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

 ``` Flags Flags, the Binary system and Logic operators. Introduction "Flags" are often used in programming when something can be either "on" or "off", in other words, a Boolean variable. This tutorial describes how and why to use flags. It uses Visual Basic for example code, but this technique can be applied to all languages. An example situation Here is an imaginary form about the features of a futuristic car. 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 on the car, or not on the car. Lets imagine you want to store the results to your questionnaire in 1 byte of data. This is simple, you just label the features from [1] through to [8] and save that number. Job done? Not yet! Unfortunately, this method is not very clever. The problem One of your customers, Mr. Richguy, who has a lot of money to spend on his new car decides he wants TWO features on his car! Oh no, better redesign that form with checkboxes instead of radio buttons. You slave away designing 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, or no features at all! The quick-fix solution How do we save this information? We could use an array of 8 Boolean variables and save each one separately. That would work, but assuming your code uses a whole byte to store a Boolean variable (as in C/C++/Delphi etc., and VB I have heard uses 2 bytes!), that would take 8 bytes to store your results rather than just the 1 we had before. What's that you say? You have 512MB 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. You could pass the Boolean variable for each feature into the function. It might look 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 solution This is how it should be done! Using a whole byte to store a Boolean is a waste. We really only need 1 bit, a 0 representing False (or off) and a 1 representing True (or on). Many of you will know how the Binary system and logic operations work, but I will explain it here because it is important you understand these first. The Binary system A bit can either be a 0 or a 1, on or off, true or false. A byte consists of 8 bits. Each bit in the byte represents a different power of 2. In this way, a byte can store any number from 0 to 255. This table shows some examples. Note that 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 Operations The 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 1 only if Bit1 AND Bit2 are 1. This table shows all the possible outcomes of the And 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 if either Bit1 OR Bit2 are 1. This table shows all the possible outcomes of the Or operator. 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 is valid? ' 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 of each 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 in your debug window. Applying your new knowledge! Getting a bit bored of all these tables of 0's and 1's? Don't worry, you didn't read all that for nothing! Lets go back to the problem of storing car features. Instead of assigning each feature a value from [1] to [8], we instead use [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 the features up, using the + operator. For example, Mr. Richguy comes back to your online car showroom now you have improved it to allow for multiple features, and decides to buy a new car with jet boost, a shield, super grip tires and a time warp. 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, of value 163, which could be saved in a file, or sent to function to add up the cost, or anything else you'd like to do with it. You new function to calculate the cost would look much simpler, something like 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 your byte. However, there comes a time when you want to reverse the operation and get the features back out of the byte variable, such as when you load a file, or the function parses the features etc. How is this done? I didn't mention logic operators for nothing! Imagine our function wants to test if there are super grip tires to be put onto the new car. What we would need to do is test whether the CF_TIRES was in the byte. The number 32 (=2^5) is represented by Bit6 of your byte. To check is Bit6 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 the features with tires caused the result of tires. To see why this 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 0 0 0 0 0 1 0 0 32 In a similar way, the Or operator could have been used to find whether tires were 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 the features 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 operator to do this, but this process is often called "Masking" bits. Summary You have learnt about storing multiple Boolean variables ("flags") in a byte, why you should do that, and how to apply that to a real programming situation. More Ideas... What's that? Your new super high-tech futuristic cars have up to 16 available features? No problem! You can apply all the same techniques using 16 bit (= 2 byte) numbers, often called integers. And so on... you could store 32 flags in a 32 bit (= 4 byte) integer (often called a long integer). If you want another example of how to apply these techniques, I will tell you about why I got the idea for this tutorial. I am working on several card games by contract and my main task is to write a general, reusable ActiveX DLL which can be used in all the card games. One of the features is to provide varied animations on the cards, such as translation, spinning, resizing etc. Sometimes you will want to apply more than one animation at once. To call the DLL to perform 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 line of code. So apart from the run time advantages of using flags, this also makes it easier for programmers using the DLL. Tutorial by Simon Price 31/07/01 Simon@VBgames.co.uk http://www.VBgames.co.uk ```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)