Check if the 20th bit of a 32 bit integer is on or off?
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
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
Posted on January 3, 2010
(Last modified on August 7, 2020)
| 6 minutes
| Kinshuk Chandra
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
Posted on December 7, 2009
(Last modified on August 7, 2020)
| 8 minutes
| Kinshuk Chandra
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
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
#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
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
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?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
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]How to find out if a machine is 32 bit or 64 bit?
Posted on December 5, 2009
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
n = sizeof(void *);
if n==8, than 64 bit
if n==4, than 32 bit.
To determine whether machine is Little-Endian and Big-Endian?
Posted on November 28, 2009
(Last modified on August 7, 2020)
| 3 minutes
| Kinshuk Chandra
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.
Posted on November 27, 2009
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
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]