473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binary mask What is it?

We've been told to create a privatestatic data member called mask, and a method called InitMask that will initialise a table of masks.

This is on a task relating to bit manipulation and binary numbers.

Could somebody please explain to me what is meant by a mask, and by initialising does it mean filling with data, or simply creating some empty table?
Oct 21 '08 #1
7 20840
Banfa
9,065 Expert Mod 8TB
A bit mask is a value (which may be stored in a variable) that enables you to isolate a specific set of bits within an integer type.

Normally the masked will have the bits you are interested in set to 1 and all the other bits set to 0. The mask then allows you to isolate the value of the bits, clear all the bits or set all the bits or set a new value to the bits.

Masks (particularly multi-bit ones) often have an associated shift value which is the amount the bits need shifting left so that the least significant masked bit is shifted to the least significant bit in the type.

For example using a 16 bit short data type suppose you wanted to be able to mask bits 3, 4 and 5 (LSB is number 0). You mask and shift would look something like

#define MASK 0x0038
#define SHIFT 3

Masks are often asigned in hexadecimal because it is easier to work with bits in the data type in that base as opposed to decimal. Historically octal has also been used for bit masks.

If I have a variable, var, that contains data that the mask is relevant to then I can isolate the bits like this

var & MASK

I can isolate all the other bits like this

var & ~MASK

I can clear the bits like this

var &= ~MASK;

I can clear all the other bits like this

var &= MASK;

I can set all the bits like this

var |= MASK;

I can set all the other bits like this

var |= ~MASK;

I can extract the decimal value of the bits like this

(var & MASK) >> SHIFT

I can assign a new value to the bits like this

var &= ~MASK;
var |= (newValue << SHIFT) & MASK;

Initialisation is the process of assigning a value to a variable (or structure or array) as it is being created.
Oct 21 '08 #2
whodgson
542 512MB
I came across a brief reference to a mask recently
The main function comprised 2 arrays i) a double type x [ 8] 22.2,33.3 etc and 2) a bool type y[8] true, false,false etc
Both were copied into 2 separate vectors with copy((v,x,8) and copy(b,y,8)
v was declared as vec v; and b was declared as bits b;

Expand|Select|Wrap|Line Numbers
  1. typedef vector <double> vec;
  2. typedef vector<bool> bits;
  3. //There was a function:
  4. vec projection(vec& v,bits& b)
  5.    (
  6.       int v_size = v.size();
  7.       assert(b.size()>= v_size);
  8.       vec w;
  9.      for(int i=0;i<v_size;i++)
  10.         if(b[i])w.push_back(v[i]);
  11.      return w;
  12.   }
When v was printed all 8 vector elements appeared eg 22.2 33.3 44.4 etc
When w was printed only the double elements which aligned with the bool true elements were printed.
The purpose of the above function was said to be ..."to use the bit vector b as a mask to remove selected elements of the vector v. The resulting vector w is called the projection of v onto the subspace determined by b"
Ref:Fundamentals of Computing with C++ John R Hubbard Ph.D pp235
Oct 22 '08 #3
Thanks for the help, I think I get what masks actually are now. The next step is figuring out how to initialise this mask array without all the errors. Incidentally, would an array of ints be the best way to store the mask?

The mask variable should be a pointer to a table of masks, I think basically I need to have a list of all unsigned ints where only one bit is a 1, ie, 1,2,4,8,16 etc....for all 32 bits.

If I stored these as ints, I already have a utility function called Bin() that takes an int and converts it to a Binary object, which then enables the overloaded << to display it as an int. Would this make sense?
Oct 28 '08 #4
Banfa
9,065 Expert Mod 8TB
If you have been told to create a table like that then I guess you should however a table

Expand|Select|Wrap|Line Numbers
  1. const int masks[32] = {0x00000001,
  2.                        0x00000002,
  3.                        0x00000004,
  4. ...
  5.                        0x20000000,
  6.                        0x40000000,
  7.                        0x80000000};
  8.  
Seems fairly pointless to me since any value extracted from the table masks[index] can be easily calculated by 1 << index. Unless you happen to be working on a platform that has plenty of spare memory but a very low power processor.

There is little point converting an int to a binary object because an int is already a binary object, did you mean binary string?

What you suggest may make sense in the context of your class but makes less sense in the context of the real world (this is not entirely unheard of).
Oct 28 '08 #5
I've already had the argument with the uni organiser regarding the whole 'real world scenario' thing. After just 3 weeks experience of a programming language, after the hand-holding nature of Java, to be thrown into something so abstract complicates matters. Being given a real world problem would help us learn the language because we are able to think and interpret things in english first.

Anyway, back on topic. The reason for the converting to Binary "type" is because we have the class Binary with an overridden << operator. We had to overload it so that when fed a Binary object it prints out the Binary representation instead of the int value. However, he doesn't want normal int values to be output in this way.
Oct 28 '08 #6
Banfa
9,065 Expert Mod 8TB
OK that makes sense, sotty I used a piece of C dialect when I said string I did mean output the bits to std out which is what you say your Binary class does (presumably it could/does have some operators too).

Anyway I think all your questions are answered so if you are still unclear on anything please say so.
Oct 28 '08 #7
Thank you.

I still have a problem with the InitMask() implementation, but that's in another thread of mine started yesterday. I just can't seem to get it to compile :(
Oct 28 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: jose luis fernandez diaz | last post by:
Hi, The program below prints a unsigned short in binary base: #include <iostream> int main() { unsigned short mask =0x1; unsigned short us =0x0071;
23
by: Davey | last post by:
How do I display an integer in binary format in C? e.g. 4 displayed as "100"
6
by: Arjen | last post by:
Hi, I can read in a file with the binaryreader. But how can I get all the bits ("1" and "0") inside my textbox? (I now get the number 68) Thanks!
6
by: LaVic | last post by:
Hello Everyone, I am trying to add two binary numbers in the form of two arrays. The code that i have been using compiles, but it does not give me the results i would expect. The problem that i...
7
by: noridotjabi | last post by:
I'm working on a simple hex editing type program and I was wondering if there was any way to display the actual 1's and 0's of binary to the screen. For example if I had some character, 'a' for...
10
by: Craig | last post by:
Hi there, I'm trying to switch binary numbers around so that the MSB becomes the LSB etc. Is there an easy way of doing this as I can't seem to find anything. If you could help that would be...
26
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
7
desklamp
by: desklamp | last post by:
I'm a total Access newbie, please bear with me! Using Win2K/Access 2003. I'm trying to create a table in which I can store IP addresses and other information. According to Microsoft, there is no...
12
by: waterdriven | last post by:
Hello; I am a newbie. A homework assignment was assigned and I am having trouble getting started. The assignment reads: Write a program to print out the binary value of a 16 bit number.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.