# Setting a bit

Use the bitwise OR operator (`|`

) to set a bit.

```
number |= 1UL << n;
```

That will set the `n`

th bit of `number`

. `n`

should be zero, if you want to set the `1`

st bit and so on upto `n-1`

, if you want to set the `n`

th bit.

Use `1ULL`

if `number`

is wider than `unsigned long`

; promotion of `1UL << n`

doesn't happen until after evaluating `1UL << n`

where it's undefined behaviour to shift by more than the width of a `long`

. The same applies to all the rest of the examples.

# Clearing a bit

Use the bitwise AND operator (`&`

) to clear a bit.

```
number &= ~(1UL << n);
```

That will clear the `n`

th bit of `number`

. You must invert the bit string with the bitwise NOT operator (`~`

), then AND it.

# Toggling a bit

The XOR operator (`^`

) can be used to toggle a bit.

```
number ^= 1UL << n;
```

That will toggle the `n`

th bit of `number`

.

# Checking a bit

You didn't ask for this, but I might as well add it.

To check a bit, shift the number n to the right, then bitwise AND it:

```
bit = (number >> n) & 1U;
```

That will put the value of the `n`

th bit of `number`

into the variable `bit`

.

# Changing the *n*th bit to *x*

Setting the `n`

th bit to either `1`

or `0`

can be achieved with the following on a 2's complement C++ implementation:

```
number ^= (-x ^ number) & (1UL << n);
```

Bit `n`

will be set if `x`

is `1`

, and cleared if `x`

is `0`

. If `x`

has some other value, you get garbage. `x = !!x`

will booleanize it to 0 or 1.

To make this independent of 2's complement negation behaviour (where `-1`

has all bits set, unlike on a 1's complement or sign/magnitude C++ implementation), use unsigned negation.

```
number ^= (-(unsigned long)x ^ number) & (1UL << n);
```

or

```
unsigned long newbit = !!x; // Also booleanize to force 0 or 1
number ^= (-newbit ^ number) & (1UL << n);
```

It's generally a good idea to use unsigned types for portable bit manipulation.

or

```
number = (number & ~(1UL << n)) | (x << n);
```

`(number & ~(1UL << n))`

will clear the `n`

th bit and `(x << n)`

will set the `n`

th bit to `x`

.

It's also generally a good idea to not to copy/paste code in general and so many people use preprocessor macros (like the community wiki answer further down) or some sort of encapsulation.

# Function pointers in C

Let's start with a basic function which we will be *pointing to*:

```
int addInt(int n, int m) {
return n+m;
}
```

First thing, let's define a pointer to a function which receives 2 `int`

s and returns an `int`

:

```
int (*functionPtr)(int,int);
```

Now we can safely point to our function:

```
functionPtr = &addInt;
```

Now that we have a pointer to the function, let's use it:

```
int sum = (*functionPtr)(2, 3); // sum == 5
```

Passing the pointer to another function is basically the same:

```
int add2to3(int (*functionPtr)(int, int)) {
return (*functionPtr)(2, 3);
}
```

We can use function pointers in return values as well (try to keep up, it gets messy):

```
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
```

But it's much nicer to use a `typedef`

:

```
typedef int (*myFuncDef)(int, int);
// note that the typedef name is indeed myFuncDef
myFuncDef functionFactory(int n) {
printf("Got parameter %d", n);
myFuncDef functionPtr = &addInt;
return functionPtr;
}
```

## Best Solution

Either because the function has been around since before

`const`

or because those who designed the API have troubles keeping up.