Check if the 20th bit of a 32 bit integer is on or off?

AND it with x00001000 and check if its equal to x00001000

if((num & x00001000)==x00001000)

Note that the digits represented here are in hex.

`
     0      0      0      0      1      0      0      0
                                 ^                   
                                 |

 x0000   0000   0000   0000   0001   0000   0000   0000 = 32 bits

  ^                              ^                    ^
  |                              |                    |

  0th bit                        20th bit             32nd bit
`

Count bits set in an integer

Question: Write a function that determines the number of bits set to 1 in the binary representation of an integer. Method 1 - Simple method (Right shift integer until it becomes 0) Going by the brute force approach, it would be easy to come up with the following solution. If the least significant bit of a number is 1, it will return a result of 1 when we AND it with 1. [Read More]

Using pointers to break into bytes

Eg.1 main() { int i = 257; int *iPtr = &i; printf("%d %d”, *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 1 1 Explanation: The integer value 257 is stored in the memory as, 00000001 00000001, so the individual bytes are taken by casting it to char * and get printed. Eg. 2 main() { int i = 258; int *iPtr = &i; printf("%d %d”, *((char*)iPtr), *((char*)iPtr+1) ); } Answer: 2 1 Explanation: [Read More]

To swap the two nibbles in a byte

#include 

unsigned char swap_nibbles(unsigned char c)
{
  unsigned char temp1, temp2;
  temp1 = c & 0x0F;
  temp2 = c & 0xF0;
  temp1=temp1 « 4;
  temp2=temp2 » 4;

  return(temp2|temp1); //adding the bits
}

int main(void)
{
  char ch=0x34;
  printf("\nThe exchanged value is %x”,swap_nibbles(ch));
  return 0;
}

Code to check if an integer is a power of 2 or not in a single line

Problem Check if integer is power of 2 or not FOLLOW UP - Preferable solution to get it in 1 line. Solution Method1 All power of two numbers have only one bit set. So count the no. of set bits and if you get 1 then number is a power of 2. We have discussed how to count number of bits set here. Method 2 Note that the bit pattern of a power of two is of the form 10…0 and that of a number just one less is 011…1. [Read More]

How to add two numbers without using the plus operator?

Actually, SUM = A XOR B CARRY = A AND B Recursive: int add(int a, int b){ if (!a) return b; else return add((a & b) « 1, a ^ b); } Iterative **unsigned long add(unsigned long integer1, unsigned long integer2) { unsigned long xor, and, temp;  and = integer1 & integer2; /* Obtain the carry bits */ xor = integer1 ^ integer2; /* resulting bits */  while(and ! [Read More]

To determine whether machine is Little-Endian and Big-Endian?

Problem Write a program to find whether a machine is big endian or little endian. Solution What Little-Endian and Big-Endian? How can I determine whether a machine’s byte order is big-endian or little endian? How can we convert from one to another? First of all, Do you know what Little-Endian and Big-Endian mean? Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. [Read More]

Swap two number in place without temporary variables.

Problem Write a function to swap two number in place without temporary variables. Solution Method1 - The XOR or Exclusive trick In C this should work: a ^= b ^= a ^= b; to simplify : a=a^b; b=a^b; a=a^b; OR a^=b; b^=a; a^=b; Following are operations in XOR 0^0=0 0^1=1 1^0=1 1^1=0 Hence, we have: a=a^b: ‘a’ will save all the bits that a differs from b: if the bit that ‘a’ and ‘b’ differ, it gets 1, otherwise 0. [Read More]