How do you use bitwise flags in C++?

12,750

Solution 1

Use std::bitset    

Solution 2

If you want to use bitfields then this is an easy way:

typedef struct MAZENODE
{
    bool backtrack_north:1;
    bool backtrack_south:1;
    bool backtrack_east:1;
    bool backtrack_west:1;
    bool solution_north:1;
    bool solution_south:1;
    bool solution_east:1;
    bool solution_west:1;
    bool maze_north:1;
    bool maze_south:1;
    bool maze_east:1;
    bool maze_west:1;
    bool walls_north:1;
    bool walls_south:1;
    bool walls_east:1;
    bool walls_west:1;
};

Then your code can just test each one for true or false.

Solution 3

Use hex constants/enums and bitwise operations if you care about which particular bits mean what.

Otherwise, use C++ bitfields (but be aware that the ordering of bits in the integer will be compiler-dependent).

Solution 4

Learn your bitwise opertors: &, |, ^, and !.

At the top of a lot of C/C++ files I have seen flags defined in hex to mask each bit.

#define ONE 0x0001

To see if a bit is turned on, you AND it with 1. To turn it on, you OR it with 1. To toggle like a switch, XOR it with 1.

Solution 5

To manipulate sets of bits, you can also use ....

std::bitset<N>

std::bitset<4*4> bits;
bits[ 10 ] = false;
bits.set(10);
bits.flip();
assert( !bits.test(10) );
Share:
12,750
KingNestor
Author by

KingNestor

CS Student at the University of Central Missouri

Updated on June 05, 2022

Comments

  • KingNestor
    KingNestor almost 2 years

    As per this website, I wish to represent a Maze with a 2 dimensional array of 16 bit integers.

    Each 16 bit integer needs to hold the following information:

    Here's one way to do it (this is by no means the only way): a 12x16 maze grid can be represented as an array m[16][12] of 16-bit integers. Each array element would contains all the information for a single corresponding cell in the grid, with the integer bits mapped like this:

    alt text
    (source: mazeworks.com)

    To knock down a wall, set a border, or create a particular path, all we need to do is flip bits in one or two array elements.

    How do I use bitwise flags on 16 bit integers so I can set each one of those bits and check if they are set.

    I'd like to do it in an easily readable way (ie, Border.W, Border.E, Walls.N, etc).

    How is this generally done in C++? Do I use hexidecimal to represent each one (ie, Walls.N = 0x02, Walls.E = 0x04, etc)? Should I use an enum?


    See also How do you set, clear, and toggle a single bit?.

  • Jason
    Jason over 15 years
    unfortunately this is not portable as the order of bits is compiler specific.
  • Jason
    Jason over 15 years
    Also, I'm not sure if bool is a supported type for bitfields, it would be more portable if unsigned were used.
  • KPexEA
    KPexEA over 15 years
    yes bool is supported for bitfields, but you are correct, ordering of them is totally up to the compiler and usually associated with endianess
  • user1411601
    user1411601 over 15 years
    Unless you want to send this binary data structure between machines, or store it to a file and read it back with another program, the order of bits is unimportant. So this gets +1 vote from me.
  • xtofl
    xtofl about 15 years
    Would be even nicer if you could nest the n,s,e,w fields into an internal struct.