I'm working with a cool program from a book that uses a mask to print an integer's binary equivalent. I've never used a mask before so it's kind of new to me. If my integer is 123(0111 1011) and my mask value is defined initially as 1000 0000(which is 128), then the first time through the compiler is doing this:
Then it prints a 0 since 0 AND 1 gives you 0. Then I right shift my mask value over to the right by one place so my mask value is 0100 0000. Then I go again:
This time it prints a 1 because 1 AND 1 gives you a 1. Then I shift my mask value over to the right one place again so it looks like 0010 0000. Then I go again:
This time it again prints a 1 since 1 AND 1 gives you a 1. This process repeats until done and the loop terminates.
My question is you can see that the compiler is working with only one column at a time. It's working with the column that has the 1 bit in the mask value. What's the reason for this? Is this why they call it a mask? Because when you put 0's in all the other positions of your mask, the compiler ignores or becomes blind to those other neighboring columns?
07-27-2007, 07:42 AM
This is the logical or "Boolean" AND operation. The statement is true only if both operands are true (==1). If one of the operands is false (==0), then the whole statement is false. So your statement evaluates to true (==1) only if the bit in the integer value is true and if the bit in the mask is true.
I know. I'm using the bitwise AND(&) operator. But what I want to make sure of is I've noticed that the compiler is only working with one column at a time. The compiler only "sees" the column where the 1 bit is in my mask. It does not see(it ignores) all the other columns 8 bit integer value. Again, let's look at my example. My integer is 123(0111 1011). My mask is 128(1000 0000). The first time through my loop the compiler does this:
But it looks at the column where the 1 is in my mask. And since ANDing 0 and 1 produces 0, it will print a 0. Then I shift my mask so now my mask is 64(0100 0000). Now we have this:
Now the compiler "sees" only the second column from the left, where the 1 bit is in my mask, and because 1 AND 1 is 1, it prints 1. I keep shifting my mask and keep looping until the process is done.
But my question is I've noticed that the compiler only "sees" the column of bits where the 1 bit is in my mask. All the other columns of bits are hidden or "masked off.".
That must be the way it's working. Because there are zero's in all the other bit positions in my mask, the compiler does not see or pay attention to those other columns when doing the ANDing operation.
07-27-2007, 08:18 AM
Yes, it is a "bit by bit" evaluation and is returning the results of your evaluation only in the bit where the mask has a "true".
What happens if you set two bits of the mask as true?
07-27-2007, 08:59 AM
Why would I want to do that? I only want to print an integers binary equivalent, so all I need is 1 bit to be set to true, and all the others set to 0 or false. If I did as you suggested, I guess the result would depend on what the bit was set in the original value. Because 1 & 1 will give you 1, but 1 & 0 will give you 0, so it depends whether or not there was a 1 or a 0 in that particular bit position in the integer value.
07-27-2007, 10:17 AM
I misunderstood your intention. I thought you were trying to understand the evaluation process.
07-27-2007, 12:00 PM
sorry ahead of time if I've also misunderstood and this doesn't answer your questions.
You have your original number, 123(0111 1011). To put a mask over it is to hide certain bits of it, resulting in a different number. Think of it this way. Place a photo on the table. Now put another piece of paper(the mask) over that photo. Punch some random holes in that paper and you'll see some parts of the photo are visible while the rest is hidden. A bit mask works in a similar way. Exactly what part of your original number(photo) "peeks through" the paper depends on how you apply the mask, either AND, OR, XOR. I don't know, did I make any sense?