Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

swapping nibble in a character

Posted on 2000-04-10
4
1,709 Views
Last Modified: 2012-05-04
I would like to know how to swap the nibbles in a byte using bitwise operators.

Regards,
arut
0
Comment
Question by:arut
  • 2
4 Comments
 
LVL 1

Expert Comment

by:guyss
ID: 2702986
i'm sorry, i'm not familiar with the word nibbles in this context.
please explain exactly what you mean
and give an example if you can.
0
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
ID: 2703029
unsigend char x=245,swaped;


swaped = ((x & 0xf0) >> 4) | (x & 0x0f) << 4);

0
 

Author Comment

by:arut
ID: 2703214
Dear rbr,

Could you please explain the anding with x part.

How does one get such expresssions easily?

When I tried it, I just couldn't proceed.Can you please suggest some rules or tricks to find solutions for problems using bit wise operators.

It would be very helpful.

Regards,
arut
0
 
LVL 10

Expert Comment

by:rbr
ID: 2703242
Ok what is the representation of the high nibble:
Binary 11110000 = 0xf0

Low nibble
Binary 00001111 = 0x0f

x & 0xf0 cuts the high nibble out of the x (e.g. x=11010011 x & 0xf0 = 11010000)

x & 0xof cuts the low nibble out of the x (e.g. x=11010011 x & 0x0f = 00000011)

>> 4 shifts 4 bit to the right

11010000 >> 4 = 00001101 (you can divide through 16 will be the same results)

<< 4 shifts 4 bit to the left

00000011 << 4 = 00110000 (you can multiply with 16 will be the same results)

These 2 results will be ored (or added will be the same)
00001101 | 00110000 = 00111101 final result

without bit operations

swaped = x / 16 + (x%16) * 16;

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question