Using a mask


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Using a mask

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    37

    Using a mask

    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?

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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

  3. #3
    Join Date
    Mar 2005
    Posts
    37
    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.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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?

  5. #5
    Join Date
    Mar 2005
    Posts
    37
    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.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I misunderstood your intention. I thought you were trying to understand the evaluation process.

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    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?

Similar Threads

  1. Clearing Mask Edit box
    By Kelly in forum .NET
    Replies: 2
    Last Post: 06-03-2002, 04:23 PM
  2. Mask Edit Control
    By bob in forum VB Classic
    Replies: 0
    Last Post: 03-25-2002, 12:55 AM
  3. input mask
    By cb in forum VB Classic
    Replies: 0
    Last Post: 10-25-2001, 11:14 AM
  4. Mask Property for a Textbox
    By Barend Esterhuizen in forum VB Classic
    Replies: 1
    Last Post: 11-30-2000, 08:45 AM
  5. Input Mask Error Messages?
    By Bryan in forum VB Classic
    Replies: 0
    Last Post: 06-30-2000, 06:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center