This program is for swapping odd bits with its next odd bits and even bits with its next even bits LOGIC:If the two bits are different, just toggle them odd bit condition : i %2 ==0even bit condition : i… Read More ›
C Language
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions
Number power of 2
This program is to find out whether the given number is power of 2. Any number is power of 2 if (num & num -1) =0 Implementation: Output:
Check given number is power of 4
This program is to check whether a given number is power of 4 Condition for a number to be power of 4: If the number of bit set is 1 The number of unset bit before the first set is… Read More ›
Rotation of a number
This Program is to perform Right and Left rotate Operation What is rotation of a number by a bit? Rotation of a number by a particular bit can be: Left Rotation Right Rotation Left Rotation:This means shifting all the bits… Read More ›
Change the endianess of a number
This program is to change the endianess of a number along with swapping the nibble of a byte Logic:1) data & 0x000000FF — This picks the first byte from a given number2) data & 0x0F) — This picks the first… Read More ›
Reverse of a number
This program is to reverse a number Logic: Loop through all the bits of an integer.If a bit at ith position is set in the i/p no.then set the bit at (NO_OF_BITS – 1) – i in o/p.Where NO_OF_BITS is… Read More ›
Clear till ith bit from MSB
This program is to clear all the bit starting from MSB till the ith bit Example: number = 224 : 1110 0001ith bit = 3Result= 1 : 0000 0001 Logic: For clearing say till 2nd bit, we need AND till… Read More ›
Clear till ith bit from LSB
This program is to clear all the bit starting from LSB till the ith bit Example: number = 7 : 0000 0111ith bit = 2Result= 4 : 0000 0100 Logic: For clearing say till 2nd bit, we need AND till… Read More ›
To find square of a number
This program is to find the square of a number. Logic: for even number: 4 * (n/2)^2 for odd number: 4 * (n/2)^2 + 4 * (n/2) + 1 (n/2) = (n >> 1) Output:
To set and clear a particular bit
This program is to set and clear a particular bit in a given number. The logic used will be: To set a particular bit: ORing the bit with 1 To clear the particular bit: ANDing the bit with 0. All… Read More ›