Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A quick cheatsheet on using a bit map to store multiple values

I always get confused when I am about to use a bit map to store multiple flags. For example, if there are 10 possible properties for an object (all Yes or No), I use an unsigned int and the first 10 bits (from LSB) based on the properties. Now how to set and unset a particular bit and also how to check if a bit is set or not?

If I want to unset the 5th bit, I use: bitand (flag, 2^5 - 1)

But I am not clear on what to use to check if 5th bit is set or not.

like image 761
Arvind Avatar asked Jun 21 '09 13:06

Arvind


People also ask

What is bitmap indexing used for?

Bitmap indexes are widely used in data warehousing applications, which have large amounts of data and ad hoc queries but a low level of concurrent transactions. For such applications, bitmap indexing provides: Reduced response time for large classes of ad hoc queries.

What is bitmap in database?

A bitmap index is a special kind of database index that uses bitmaps. Bitmap indexes have traditionally been considered to work well for low-cardinality columns, which have a modest number of distinct values, either absolutely, or relative to the number of records that contain the data.

What is bitmap indexing in data mining?

Bitmap indexes store the bitmaps in a compressed way. If the number of distinct key values is small, bitmap indexes compress better and the space saving benefit compared to a B-tree index becomes even better. Bitmap indexes are most effective for queries that contain multiple conditions in the WHERE clause.

How are bitmap indexes stored in Oracle?

Oracle bitmap indexes are very different from standard b-tree indexes. In bitmap structures, a two-dimensional array is created with one column for every row in the table being indexed. Each column represents a distinct value within the bitmapped index.


1 Answers

check if the nth bit is set:

(flags & (1 << n)) != 0

set the nth bit:

flags |= (1 << n)

clear the nth bit:

flags &= ~(1 << n)

toggle the nth bit:

flags ^= (1 << n)
like image 91
Ferruccio Avatar answered Sep 22 '22 23:09

Ferruccio