• 07-27-2007, 05:53 AM
357mag
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:

0
1

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:

1
1

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:

1
1

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, 06:42 AM
nspils
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.

To read more, here is the Wikipedia entry for Boolean algebra:
http://en.wikipedia.org/wiki/Boolean...ra_%28logic%29
• 07-27-2007, 07:10 AM
357mag
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:

0111 1011
1000 0000

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:

0111 1011
0100 0000

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, 07:18 AM
nspils
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, 07:59 AM
357mag
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, 09:17 AM
nspils
I misunderstood your intention. I thought you were trying to understand the evaluation process.
• 07-27-2007, 11:00 AM
Phaelax