Thursday, August 18, 2016

Swap bits for given character

Swap first 4 bits of a number to next 4 bits
Example :
1011 0011 to 0011 1011

Get first 4 bits : (Number & 0XF0), shift 4 positions to right (number & 0Xf0) >> 4

Get the last 4 bits : number & 0XOF, shift 4 position to left, number & 0XOF) << 4

Return sum (OR) of above is the swapped number

unsigned char c ;   // input number
c = (c & 0XF0) >> 4 | (c &0X0F) << 4)

No comments:

Post a Comment