top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to swap the two nibbles in a byte ?

+7 votes
874 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 Google+ Share Button LinkedIn Share Button Multiple Social 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?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...