Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between bitmask and bitmap in C

Tags:

c

What is the conceptual difference between them? I know bitmap is sort of bitfields in the structs..

struct{
    int bit1: 1;
    int bit2: 1;
    int bit3: 1;
  };

so in tht case is bitmask something we define for an enum?

like image 648
rain Avatar asked Mar 15 '12 21:03

rain


2 Answers

A bitmask is an integer type that is used to "mask" certain bits when performing bitwise operations. For example, the bitmask 0xFFFFFFFF might be used to mask a 32-bit unsigned value because you want to operate on all bits at once, whereas 0x00000001 would only operate on the very last bit. You often see bitmasks defined as the 'flipped' version and then flipped using ~.

A bitmap, on the other hand, is a set of variables each mapped to an individual bit. There are many ways of achieving this, your struct is one (common) example of a bitmap.

You might put various masks in an enum to give yourself easier access to them, but it's not strictly necessary to do so.

like image 176
Chris Browne Avatar answered Oct 14 '22 23:10

Chris Browne


Bitmap is more of data itself which comprise of bits. One of the example could be say that there are 8 friends in a group and group performs various activities together. Group participation in each activity now can be represented by "bitmap" which comprise of bits (each for one friend). e.g.

Skii - 10110000 <<<<friend 5,6 and 8 will go to Skii

Movie - 10011000 <<< friend 4,5 and 8 will go to movie

College- 11111111 <<<all friends will go to college

Bitmask is more skeleton for bitmap. bitmask will be used to set and get bit values in bitmap.

friend1 - 00000001<<<< bitmask for friend 1

friend2 - 00000010 <<<bitmask for friend 2

friend5 - 00010000

(Note: I found it awkward to address friend0 :) , purist may consider everything as n-1)

Now using bitmap and bitmask we can determine

  • is friend1 going for Skii?

Skii & friend1 <<<< this would be zero

  • is friend 5 going to Movie?

Movie & friend5 <<< yes

like image 36
Mubeen Muqadam Avatar answered Oct 14 '22 23:10

Mubeen Muqadam