Using mask in bit manipulations...

This is a discussion on Using mask in bit manipulations... within the C Programming forums, part of the General Programming Boards category; Hi, In bit manipulations using bitwise operators, what is a mask and why is it needed? thnx...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Using mask in bit manipulations...

    Hi,

    In bit manipulations using bitwise operators, what is a mask and why is it needed?

    thnx

  2. #2
    Unregistered
    Guest
    say you have a program that if the user whats a particular option on

    you could use a boolean variable for this or you ould use "bit flags"

    where ase each bit of say an int represents a flag

    bit 0 is one thing on or off
    bit 1 is something else

    if you wanted to test this values you would "Mask" out the other bts in the int like so

    [Code]

    #include <stdio.h>
    #define OPTION 0x01

    int main(void)
    {
    char ch = 0;
    int flags = 0;

    printf("Hit a Key To Toggle the Bit Flag On Hit 'o' to turn it off\n");
    if((ch = getchar()) != 'o')
    flags |= OPTION; // set the first bit or our option flag on
    else
    flags &= ^OPTION; // set the Option flag off

    if(flags & OPTION)
    printf("The Option is Enabled!");

    return 0;
    }

  3. #3
    Unregistered
    Guest
    forgot to end my code tag.

    Code:
    #include <stdio.h>
    #define OPTION 0x01
    
    int main(void)
    {
        char ch = 0;
        int flags = 0;
    
        printf("Hit a Key To Toggle the Bit Flag On Hit 'o' to turn it off\n");
        if((ch = getchar()) != 'o')
            flags |= OPTION; // set the first bit or our option flag on
        else
            flags &= ^OPTION;  // set the Option flag off
    
        if(flags & OPTION)
           printf("The Option is Enabled!");
    
        return 0;
    }

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    keep getting loged out...

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    So if i am thinking correct, the mask can be anything to use in different situations, but it's used usually with AND right?

    thnx

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    yup.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >So if i am thinking correct, the mask can be anything to use in
    >different situations, but it's used usually with AND right?

    The reason why an AND is used is that

    0 AND 0 = 0
    0 AND 1 = 0
    1 AND 0 = 0
    1 AND 1 = 1

    So if you have the byte

    1101:0110

    And you want to test if bit 3 is set (i.e. is equal to 1), then you can define the mask

    0000:1000

    And then AND the mask with the byte, which results in

    0000:0000

    So bit 3 is not set.

    Code:
    byte = 0xD6;
    mask = 0x08;
    
    if ((byte & mask) > 0)
        /* bit 3 is set */

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    it's useful for saving space. 1 char = 8 bits.
    i think OR can be used to set bits. set a mask, like
    Code:
    0010 0001, OR
    0100 0011 and you get
    0110 0011.
    i've only seen xor used in encripting. i remember something called eqv(in basic i think), but i've forgotten what it was. can someone remind me?

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    Use XOR if you want to switch bits (1->0, 0->1)

    0 XOR 0 = 0
    0 XOR 1 = 1
    1 XOR 0 = 1
    1 XOR 1 = 0

    0100:1101 XOR 0000:1111 would produce 0100:0010
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21