Nmzzz
Nmzzz

Reputation: 2472

Confusion with sigdelset() function

sigdelset() function is used to turn off a single bit. Here is its implementation:

    int
    sigdelset(sigset_t *set, int signo)  //signo is the signal number
    {
    *set &= ~(1 << (signo - 1)); // turn bit off 
    return(0);
    }

I can't understand this code. I think that a signal number looks like 0010(in binary). But it seen not right.

Upvotes: 2

Views: 184

Answers (1)

user529758
user529758

Reputation:

You're confusing how the human brain imagines binary numbers and how we denote them in C.

First, there's no binary notation in standard C (however certain compilers accept 0b... as an extension).

Second, 1 << (signo - 1) is a binary left shift operator. Signo is probably the ordinal number of the bit to be turned off, starting from 1. Then we subtract 1 from it, thus we obtain a number starting from 0 (logically the first bit). Then we left shift it to obtain 2 to the power (signo - 1), which is a number of which the binary representation looks like this:

00000010000000... etc.
      ^
      +-  bit #signo - 1

Then the function uses the ~ (2's complement) operator which will result in (along with the bitwise and assignment operator, &=) turning the bit off.

Upvotes: 2

Related Questions