Skip to main content

Come C tricks

Swapping the values of two variables with out using a third!!
x = x ^ y;
y = x ^ y;
x = x ^ y;
All hail the XOR operation

int c;
c &= (c-1);
check if c == 0

This allows you to check if C is a power of 2 (i.e. 0,1,2,4,8...)

Reading sentences (including spaces) from stdin:

scanf("%s",str); can't read spaces in input string.
To do that, you have to: scanf(" %[^\n]s",str);

Note the space before % within the quotes.