Warning: INSERT command denied to user 'dbo292345962'@'74.208.16.27' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:399:\"INSERT command denied to user 'dbo292345962'@'74.208.16.27' for table 'captcha_sessions'\nquery: INSERT into captcha_sessions (uid, sid, ip_address, timestamp, form_id, solution, status, attempts) VALUES (0, '713b32d07977dc4c12e454fa5b7da274', '54.234.228.64', 1413904056, 'comment_form', '4f1747427fb6ca7c19921f4312734541', 0, 0)\";s:5:\"%file\";s:62:\"/homepages/25/d199835659/htdocs/ID/modules/captcha/captcha.inc\";s:5:\"%line\";i:99;}� in /homepages/25/d199835659/htdocs/ID/includes/database.mysql.inc on line 135
Bitwise and Flags | Inferno Development

Bitwise and Flags

Some functions want to save parameters and have only a single parameter that takes in binary bits and the parameter can determine all sorts of flags from this. The MessageBox function takes numerous flags that can be OR'ed together.

You might be wondering, how does the MessageBox function know which styles I've just passed to it?

A char is 1 byte, or 8 bits.
So in a byte, we can have only 8 flags.

Let's assume there is a function that can take up to 8 flags(values OR'd together).

The constants would look like this.

#define FLAG1 0x01
#define FLAG2 0x02
#define FLAG3 0x04
#define FLAG4 0x08
#define FLAG5 0x10
#define FLAG6 0x20
#define FLAG7 0x40
#define FLAG8 0x80

And these flags represented in binary:

FLAG1: 0000 0001
FLAG2: 0000 0010
FLAG3: 0000 0100
FLAG4: 0000 1000
FLAG5: 0001 0000
FLAG6: 0010 0000
FLAG7: 0100 0000
FLAG8: 1000 0000

You can clearly see I've set it up so that each flag activates 1 bit.

To pass these flags to a function, we will use the OR operator. Because the OR operator can activate bits in the result.

Here is an example of how OR works.
Note: the OR operator is the pipe symbol: |

0000 0110 |                   1111 1111 |                   0101 1010 |
0101 0000                     0000 0000                     1100 1100
-------------               --------------                --------------
0101 0110                     1111 1111                     1101 1110

So if we wanted to pass FLAG4 and FLAG7 to our function(using |), we come out with

0000 1000 |
0100 0000
---------------
0100 1000

I hope you can see that OR'ing these flags together would set the respective bit for each flag. Whereas AND'ing them together would produce 0.

Now, inside of our function, we will test for each flag using the AND operator.
Note: the AND operator is the ampersand: &

Here is an example of how AND works.

0000 0110 &                   1111 1111 &                   1100 0011 &
0101 0110                     0000 0000                     0101 0001
----------------           -----------------             ----------------
0000 0110                     0000 0000                     0100 0001

Remember we wanted to pass FLAG4 | FLAG7(0100 1000).

Here's how our function looks:

#include <iostream>
 
void WhichFlags(char flags) //char == 1 byte
{
                if(flags & FLAG4) {
                        //the user has passed FLAG4
                }
                if(flags & FLAG7) {
                        //the user has passed FLAG7
                }
}
 
int main()
{
        WhichFlags(FLAG4 | FLAG7);
        int num = 24;
        if(num & 1){
          cout << "num is even";
        } else {
          cout << "num is odd";
        }
        return 0;
}

I included a test for testing integer num and seeing whether it's even or odd, to give you an example of using bit testing.

Inside of WhichFlags(), we test for flags like this:

if(flags & FLAG4)

This evaluates like so

if((0100 1000 & 0000 1000) != 0)

You can see the result would be 0000 1000, and not zero, so it evaluates to true.

Let's say we haven't passed any flags...

if((0000 0000 & 0000 1000) != 0)

Would evaluate to 0000 0000, false.

or if we passed a different flag, lets say FLAG5

if((0001 0000 & 0000 1000) != 0)

This would evaluate to false, because FLAG4 hasn't been passed.
But later on when we check for FLAG5, this will become true.

Here's some completed code you can use/study.

#include <stdio.h>
 
#define FLAG1 0x01
#define FLAG2 0x02
#define FLAG3 0x04
#define FLAG4 0x08
#define FLAG5 0x10
#define FLAG6 0x20
#define FLAG7 0x40
#define FLAG8 0x80
 
 
void WhichFlags(char flags)
{
    if(flags != 0) {
        printf("[\n");
        if(flags & FLAG1) printf("FLAG1\n");
        if(flags & FLAG2) printf("FLAG2\n");
        if(flags & FLAG3) printf("FLAG3\n");
        if(flags & FLAG4) printf("FLAG4\n");
        if(flags & FLAG5) printf("FLAG5\n");
        if(flags & FLAG6) printf("FLAG6\n");
        if(flags & FLAG7) printf("FLAG7\n");
        if(flags & FLAG8) printf("FLAG8\n");
        printf("]\n\n");
    }
}
 
int main(int argc, char *argv[])
{
        WhichFlags(FLAG1 | FLAG3 | FLAG5);
        WhichFlags(0);
        WhichFlags(FLAG8);
        return 0;
}

I hope this cleared up some issues, and someone out there learned something.

Here is a good analogy by ID Software Engineer, aLiNuSh, that should clear some things up if your still having trouble understanding.

Quote:

For beginners, it's easy to think of flags as a series of ON/OFF switches. So if you have 8 flags just imagine there are 8 switches, one after another, all turned off. Now if you replace ON with 1 and OFF with 0 you would get this

0000 0000 -> 8 switches that are all OFF

Now if you want to turn the first switch on then you get...

0000 0001

Then you figure you want to turn the last switch on, and you get...

1000 0001

In your head this stuff can be done pretty easily but to do it on a computer you need to do some heavy bitwise operations and that's where the shifts and or's and and's come in.

Baran Ornarli's picture

Great article rubix, flags

Great article rubix, flags were something that use to be intimidating many decades ago.

Bob's picture

That was very informative,

That was very informative, thanks for teaching us flags!

Bob's picture

Very informative article, I

Very informative article, I see how flags work now.

Alexander Neiser's picture

You know I kind of like this

You know I kind of like this idea of bits and flags, it might free up memory, because you can use this instead of integers or bool.

Jorick's picture

Actually, in some cases, if

Actually, in some cases, if you were to modify your code to use integers and other flags instead of bits, doesn't the compiler convert it to bit operations anyway?

James Passaro's picture

I thought the odd even check

I thought the odd even check using bits and binary was really cool.

James Chopra's picture

Impressive tutorial, I'll

Impressive tutorial, I'll give this a shot.

Nicola Shostak's picture

I remember using flags in

I remember using flags in MessageBox function in Win32 API, I never understood at the time how it works.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.