Setting a bit
Use the bitwise OR operator (|
) to set n
th bit of number
to 1
.
// Can be whatever unsigned integer type you want, but
// it's important to use the same type everywhere to avoid
// performance issues caused by mixing integer types.
typedef unsigned long Uint;
// In C++, this can be template.
// In C11, you can make it generic with _Generic, or with macros prior to C11.
inline Uint bit_set(Uint number, Uint n) {
return number | ((Uint)1 << n);
}
Note that it's undefined behavior to shift by more than the width of a Uint
. The same applies to all remaining examples.
Clearing a bit
Use the bitwise AND operator (&
) to set the n
th bit of number
to 0
.
inline Uint bit_clear(Uint number, Uint n) {
return number & ~((Uint)1 << n);
}
You must invert the bit string with the bitwise NOT operator (~
), then AND it.
Toggling a bit
Use the bitwise XOR operator (^
) to toggle the n
th bit of number
.
inline Uint bit_toggle(Uint number, Uint n) {
return number ^ ((Uint)1 << n);
}
Checking a bit
You didn't ask for this, but I might as well add it.
To check a bit, shift number
n
to the right, then bitwise AND it:
// bool requires #include <stdbool.h> prior to C23
inline bool bit_check(Uint number, Uint n) {
return (number >> n) & (Uint)1;
}
Changing the nth bit to x
There are alternatives with worse codegen, but the best way is to clear the bit like in bit_clear
, then set the bit to value, similar to bit_set
.
inline Uint bit_set_to(Uint number, Uint n, bool x) {
return (number & ~((Uint)1 << n)) | ((Uint)x << n);
}
All solutions have been tested to provide optimal codegen with GCC and clang. See https://godbolt.org/z/Wfzh8xsjW.
Read it backwards (as driven by Clockwise/Spiral Rule):
int*
- pointer to int
int const *
- pointer to const int
int * const
- const pointer to int
int const * const
- const pointer to const int
Now the first const
can be on either side of the type so:
const int *
== int const *
const int * const
== int const * const
If you want to go really crazy you can do things like this:
int **
- pointer to pointer to int
int ** const
- a const pointer to a pointer to an int
int * const *
- a pointer to a const pointer to an int
int const **
- a pointer to a pointer to a const int
int * const * const
- a const pointer to a const pointer to an int
- ...
If you're ever uncertain, you can use a tool like cdecl+ to convert declarations to prose automatically.
To make sure we are clear on the meaning of const
:
int a = 5, b = 10, c = 15;
const int* foo; // pointer to constant int.
foo = &a; // assignment to where foo points to.
/* dummy statement*/
*foo = 6; // the value of a canĀ“t get changed through the pointer.
foo = &b; // the pointer foo can be changed.
int *const bar = &c; // constant pointer to int
// note, you actually need to set the pointer
// here because you can't change it later ;)
*bar = 16; // the value of c can be changed through the pointer.
/* dummy statement*/
bar = &a; // not possible because bar is a constant pointer.
foo
is a variable pointer to a constant integer. This lets you change what you point to but not the value that you point to. Most often this is seen with C-style strings where you have a pointer to a const char
. You may change which string you point to but you can't change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn't be changed.
bar
is a constant or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const
pointer unless you need to allow NULL
pointers.
Best Answer
uint_least8_t
is the smallest type that has at least 8 bits.uint_fast8_t
is the fastest type that has at least 8 bits.You can see the differences by imagining exotic architectures. Imagine a 20-bit architecture. Its
unsigned int
has 20 bits (one register), and itsunsigned char
has 10 bits. Sosizeof(int) == 2
, but usingchar
types requires extra instructions to cut the registers in half. Then:uint8_t
: is undefined (no 8 bit type).uint_least8_t
: isunsigned char
, the smallest type that is at least 8 bits.uint_fast8_t
: isunsigned int
, because in my imaginary architecture, a half-register variable is slower than a full-register one.