0

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.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
newsb
  • 11
  • I just simply don't know how to incorporate the integer with the binary like how to check number of the integer and then go to that position in the char and && it with 1 and if it is greater than 0 then that means there is a signal bit in that location I pretty much don't know how to put it in code – newsb Feb 27 '15 at 16:42
  • 2
    I suggest you do some browsing about bit operators, there is plenty of material out there, such as http://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm – Weather Vane Feb 27 '15 at 16:45
  • 1
    If you want to manipulate signal masks you should know that there exists functions for that: `sigemptyset`, `sigaddset`, `sigdelset`, `sigfillset` and `sigismember`. – Jean-Baptiste Yunès Feb 27 '15 at 16:49
  • @Jean-BaptisteYunès: Extremely useful and no fun at all. – Scott Hunter Feb 27 '15 at 17:37

1 Answers1

2

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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    Don't worry about it! Every value that you manipulate in a program is transformed into binary the code and the machine. If you write 10, then it is a sequence of bits representing 10 in binary (...1010), if you write 1<<3 (8) then it is also a sequence of bits representing 8 in binary (....1000). – Jean-Baptiste Yunès Feb 27 '15 at 16:48