Here some little bit oriented tricks.
bool endianess(){
int test_num = 1;
char *ptr = (char *) &test_num; // this will return a byte
return (*ptr)
}
- How to count the number of bits set to 1 in the internal representation of a number?
int num_ones_in_binary_representation(int num){
int num_ones = 0;
while (num){
if (num & 1)
num_ones++;
num = num >> 1;
}
return num_ones;
}
A more elegant solution:
int num_ones_in_binary_representation(int num){
int num_ones = 0;
while (num){
num = num & (num - 1); // consider the binary representation of num - 1 and subtract them
num_ones++
}
}
- Is your stack growing up or down?
Call two functions and take the address of their arguments, or consider the address of two consecutive variables within a function.
No comments:
Post a Comment