Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bitmask?

Tags:

c++

c++11

bitmask

How do i use it in C++ ? when is it useful to use ?
Please give me an example of a problem where bitmask is used , how it actually works . Thanks!

like image 604
Animea Animea Avatar asked Sep 03 '13 12:09

Animea Animea


People also ask

When should you use a bitmask?

Bitmasks are used when you want to encode multiple layers of information in a single number. So (assuming unix file permissions) if you want to store 3 levels of access restriction (read, write, execute) you could check for each level by checking the corresponding bit.

How does a bitmask work?

A “Bitmask” is simply a binary number that represents something. 'Mask' means hiding something, The binary representations of 1,2,3,….,7 actually represent a mask. In a particular subset, the i-th element of the set belongs to it if and only if the i-th bit of the mask is set (value is 1).

How is bitmask calculated?

The way the bit mask is calculated, is by taking each enabled position of the bitmask and apply it as the power over 2 and then adding all of the enabled values together. As a results of the script, the bitmask for 0 to 9 is calculated to 1023, as shown in Figure 1.

How do I use bitwise mask?

In a bit mask, Bitwise AND can be used to make sure particular bits in the result value are set to 0. The trick is to put a 1 in the mask for any bit you do not want changed in the result, and a 0 in the mask for any bit that you want to make sure is a 0 in the result.


1 Answers

Briefly bitmask helps to manipulate position of multiple values. There is a good example here ;

Bitflags are a method of storing multiple values, which are not mutually exclusive, in one variable. You've probably seen them before. Each flag is a bit position which can be set on or off. You then have a bunch of bitmasks #defined for each bit position so you can easily manipulate it:

    #define LOG_ERRORS            1  // 2^0, bit 0     #define LOG_WARNINGS          2  // 2^1, bit 1     #define LOG_NOTICES           4  // 2^2, bit 2     #define LOG_INCOMING          8  // 2^3, bit 3     #define LOG_OUTGOING         16  // 2^4, bit 4     #define LOG_LOOPBACK         32  // and so on...  // Only 6 flags/bits used, so a char is fine unsigned char flags;  // initialising the flags // note that assigning a value will clobber any other flags, so you // should generally only use the = operator when initialising vars. flags = LOG_ERRORS; // sets to 1 i.e. bit 0  //initialising to multiple values with OR (|) flags = LOG_ERRORS | LOG_WARNINGS | LOG_INCOMING; // sets to 1 + 2 + 8 i.e. bits 0, 1 and 3  // setting one flag on, leaving the rest untouched // OR bitmask with the current value flags |= LOG_INCOMING;  // testing for a flag // AND with the bitmask before testing with == if ((flags & LOG_WARNINGS) == LOG_WARNINGS)    ...  // testing for multiple flags // as above, OR the bitmasks if ((flags & (LOG_INCOMING | LOG_OUTGOING))          == (LOG_INCOMING | LOG_OUTGOING))    ...  // removing a flag, leaving the rest untouched // AND with the inverse (NOT) of the bitmask flags &= ~LOG_OUTGOING;  // toggling a flag, leaving the rest untouched flags ^= LOG_LOOPBACK;    ** 

WARNING: DO NOT use the equality operator (i.e. bitflags == bitmask) for testing if a flag is set - that expression will only be true if that flag is set and all others are unset. To test for a single flag you need to use & and == :

**

if (flags == LOG_WARNINGS) //DON'T DO THIS    ... if ((flags & LOG_WARNINGS) == LOG_WARNINGS) // The right way    ... if ((flags & (LOG_INCOMING | LOG_OUTGOING)) // Test for multiple flags set          == (LOG_INCOMING | LOG_OUTGOING))    ... 

You can also search C++ Triks

like image 71
goGud Avatar answered Sep 16 '22 16:09

goGud