Monday, July 2, 2012

C++ Notes

Bitwise shift operators
The bitwise left shift (<<) (A << B) operator shifts A by B bits to the left. Just add B number of zeros to the left of A in binary format.

E.g.
6 = 000110.
6 << 1 = 001100 = 12
6 << 2 = 011000 =  24

The bitwise right shift (>>) (A >> B) operator shifts A by B bits to the right. Just add B number of zeros to the right of A in binary format. If there is no room to shift to the right, truncate the bits on the right.

E.g.
6 = 000110.
6 >> 1 = 000011 = 3
6 >> 2 = 000001 = 1 (last 1 is truncated)
6 >> 3 = 000000 = 0 (both 1's are truncated)
6 >> 4 = 000000 = 0 (both 1's are truncated)

Pointer initialized to 0
In C++ a pointer initialized to 0 is equivalent to NULL.
So
int * ptr = 0;
and
int * ptr = NULL;
are identical.
But initializing to any number other than zero is illegal.

Uninitialized pointers contain garbage values. Only Static pointers are initialized to NULL automatically by the compiler.

Function declaration place
Functions should always be declared/defined before it's call (from top to bottom). If it's not defined, at least it should be declared before it's call.
You can declare it inside main() as well, but it should be before the call to it, is made.
If it's defined before main, declaring it after the definition or inside main is redundant.

No comments:

Post a Comment