In practice, the difference is in the location where the preprocessor searches for the included file.
#include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.
#include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the
#include <filename> form. This method is normally used to include programmer-defined header files.
A more complete description is available in the GCC documentation on search paths.
Setting a bit
Use the bitwise OR operator (
|) to set a bit.
number |= 1UL << n;
That will set the
nth bit of
n should be zero, if you want to set the
1st bit and so on upto
n-1, if you want to set the
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
nth 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
nth bit of
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
nth bit of
number into the variable
Changing the nth bit to x
nth bit to either
0 can be achieved with the following on a 2's complement C++ implementation:
number ^= (-x ^ number) & (1UL << n);
n will be set if
1, and cleared 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);
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.
number = (number & ~(1UL << n)) | (x << n);
(number & ~(1UL << n)) will clear the
nth bit and
(x << n) will set the
nth bit to
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.
A callback is an event or delegate in those languages - a way to get your code run by somebody else's code in it's context. Hence, the term "callback":
A canonical example is a sort routine with a user defined comparison function (the callback). Given a sort routine such as:
(The specifics of how the sort is performed isn't important - just focus on the fact that any sorting algorithm needs to compare 2 values and determine which is higher.)
So, now we can define some comparison functions:
And reuse the same sort function with them:
Things get a bit more complicated if you're using member functions, as you have to manage the
thispointer - but the concept is the same. As with most things, it's easier to explain them in C first. ;)