top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to swap the two nibbles in a byte ?

+7 votes
1,304 views
How to swap the two nibbles in a byte ?
posted Nov 18, 2013 by Anuj Yadav

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes
 
Best answer

Previous solution will not work for Negative numbers.
Because when we will do right shift then it will pad it with 1 not 0. So answer will not be correct. For avoiding this we should mask. The perfect answer will be :

#define swapNibble(x) ( (x<<4) & (0xF0) | (x>>4) & (0x0F) )

** If anyone wants more explanation plz comment

answer Nov 22, 2013 by Shekhar P
+2 votes
#define swapNibble(x) ( (x<<4) | (x>>4) )
answer Nov 18, 2013 by Vikas Upadhyay
Similar Questions
+2 votes

How can I wwap value two variables without using third variable or +/- operator?

+4 votes

How to swap ith and jth Bits for a 32-Bit Integer?

+2 votes

how many types we can swap values of two variables without using third variable?

...