Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use bitwise flags in C++?

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?.

like image 288
KingNestor Avatar asked Feb 13 '09 18:02

KingNestor


People also ask

How do Bitwise operators work in C?

Bitwise Operators in C/C++ The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers.

Where do we use bitwise operators in C?

The Bitwise Operator in C is a type of operator that operates on bit arrays, bit strings, and tweaking binary values with individual bits at the bit level. For handling electronics and IoT-related operations, programmers use bitwise operators. It can operate faster at a bit level.

How do you perform a bitwise?

It is represented by a single ampersand sign (&). Two integer expressions are written on each side of the (&) operator. The result of the bitwise AND operation is 1 if both the bits have the value as 1; otherwise, the result is always 0.


3 Answers

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.

like image 190
KPexEA Avatar answered Oct 24 '22 20:10

KPexEA


Use std::bitset    

like image 31
Vinay Avatar answered Oct 24 '22 22:10

Vinay


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).

like image 40
moonshadow Avatar answered Oct 24 '22 20:10

moonshadow