newsb
newsb

Reputation: 11

Simple bitmasking method in C to check bits

I am currently new to C as well as bitmasking and stuff like that and need some insight on how to make a program with 3 methods that checks to see if a bit is set and then manipulate it, with methods that -

INSET: This function takes two parameters, the current signal mask and a signal number, and returns a 1 if the corresponding signal is in the current set and a 0 if it is not.

ADDSET: This function also takes the current signal mask and a signal number, and adds the corresponding signal to the current signal set (i.e., sets the bit in the corresponding bit position to 1). Note that you must pass the address of the current signal mask to this function because it will modify its content.

DELSET: This functions takes the current signal mask and a signal number as parameters, and deletes the corresponding signal from the current signal set. Note that you must also pass the address of the current signal mask to this function because it will modify its contents.

Pretty much I know that there will have to be if statements set up in the method INSET but honestly all I have written down so far is this:

int INSET(unsigned char signalmask, int SIGNUM){
    if (SIGNUM)
        { ... }
}

void ADDSET(unsigned char *signalmask, int SIGNUM){
}

void DELSET (unsigned char * signalmask, int SIGNUM){
}

Any help on how to simply get it started would be appreciated.

Upvotes: 0

Views: 157

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49813

The problem is determining if bit N is set or not. You can use 1 << n to shift a single bit into position n (assuming the LSB is position 0), and then a bitwise and (&, not &&) to see if that bit is set in the mask.

Upvotes: 2

Related Questions