A bit mask is a pattern of binary bits (1s and 0s) used in Boolean arithmetic, often to store a series of switches in a single memory location. In this (very simple) example, you might have a 4-bit electronic switch inside your computer (that can hold unsigned decimal numbers 0-15) that indicate whether the light is on in 4 different rooms in your house.
Say the rooms are "Lounge", "Kitchen", "Bathroom" and "Bedroom". Each of the rooms is controlled by a single switch, that reports whether the light is on or off, sending that information back to one part of the 4-bit electronic switch in your computer.
Let's say, for the purpose of the example, that "Lounge" is in the top position in the memory location (8 in decimal notation), "Kitchen" the second (4 in decimal notation), "Bathroom" the third (2 in decimal notation), and "Bedroom" in last position (1 in decimal notation).
If you were to read that memory location (the 4-bit switch), you would get a number between 0 and 15. Not easy to distinguish, for example, if you left the bathroom light on when you came back to the computer.
In your program, as you know that the bathroom light is on the 3rd of the 4 switches, you can say something like:
if lights = 2 or lights = 3 or lights = 7 or lights = 11 or lights = 15 then...
This would work but is messy - and would only be any use with simple switches (much more than 4 elements would make the code unworkable).
This is where the bit mask comes in.
In order to identify whether the bathroom light is on, using Boolean arithmetic, you only need to know if the specific switch is on.
If you use the Boolean "AND" operator, if both bits you are comparing are 1 (on, in the lighting example), the output is a 1. For all other cases, "AND" returns zero. So, in order to find out if the bathroom light is on, your bit mask need only contain a 1 in the position of the bathroom light on the switch. (In the example above, 0010 binary, or 2 decimal).
Your program code is then as simple as:
if (lights AND 2) > 0 then...
and this works whether you have two switches or many.
2006-12-05 05:57:07
·
answer #1
·
answered by Taliesyn 2
·
0⤊
1⤋
it is used to mask out bits that you dont care about. example, you have bits that represent the status of a device, but the device only uses the first 2 bits, the rest are dont care's or undifined.
if the bits are XXXXXX00 the status is off (X = undefined)
if XXXXXX01 the status is standby
if XXXXXX10 the status is active
if XXXXXX11 the status is an error
your bitmask is defined as 0x03 (00000011)
so, when you write your code to read the status of the device:
if ((status & bitmask) == 0x00)
//device is off
else if ((status & bitmask) == 0x01)
//device is in standby
else if ((status & bitmask) == 0x10)
//device is active
else if ((status & bitmask) == 0x11)
//device has an error
since the other bits are undefined, you could read anything back in them, so you want to mask them out by "anding" the bitmask with the status, where in this example, they will be 0's, which leave only the first 2 bits to worry about.
2006-12-06 10:06:31
·
answer #2
·
answered by justme 7
·
0⤊
0⤋
Facemask like the one Hannibal Lecter wore in Silence of the lambs
2006-12-05 13:38:42
·
answer #3
·
answered by bluegreenash 2
·
0⤊
1⤋
Its abit like a bit for a horse, while making love?
2006-12-05 13:35:31
·
answer #4
·
answered by Dr.Nina 1
·
0⤊
1⤋
Do you mean "subnet mask"?
2006-12-05 13:40:30
·
answer #5
·
answered by Stephen L 7
·
0⤊
1⤋
Can you expand a bit on this, do you mean "netmask"
2006-12-05 13:28:12
·
answer #6
·
answered by Anonymous
·
0⤊
1⤋
Do you mean bitmap as in the picture format????
2006-12-05 13:42:23
·
answer #7
·
answered by PAUL D 2
·
0⤊
1⤋