473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Swapping Of Nibles

9 New Member
how can we swap two nibles in a byte
Aug 20 '07 #1
8 2197
sicarie
4,677 Recognized Expert Moderator Specialist
how can we swap two nibles in a byte
Do you have an algorithm for this? Have you run a search of Google? What did you find?
Aug 20 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Use a circular bit shift.
Aug 20 '07 #3
rakeshnayak
3 New Member
Rakesh


Rakesh


Code removed per Posting Guidelines
Aug 20 '07 #4
JosAH
11,448 Recognized Expert MVP
Use a circular bit shift.
But there isn't any such thing available in C, nor C++! ;-)

kind regards,

Jos
Aug 20 '07 #5
mac11
256 Contributor
I vote for a lookup table with 256 entries (An array where each entry is the nibble swapped value of it's index) - this might speed up execution if that concerns you.

(I once used a similar table to perform bitwise byte reversal)
Aug 20 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
But there isn't any such thing available in C, nor C++! ;-)
True. You write a function to do this using bit operations:
Expand|Select|Wrap|Line Numbers
  1. void SwapNybble(unsigned short& arg)
  2. {
  3.     unsigned char LSB  =  arg & 0x00ff;
  4.     unsigned char MSB = (arg & 0xff00) >> 8;
  5.     arg = 0;
  6.     arg = LSB;
  7.     arg |= (arg << 8) | MSB;
  8. }
  9. int main()
  10. {
  11.      unsigned short data = 0xff00;
  12.      bitset<16> b(data);
  13.      cout << b.to_string() << endl;
  14.      SwapNybble(data);
  15.      b = data;
  16.      cout << b.to_string() << endl;
  17. }
  18.  
Aug 21 '07 #7
chinmaya
9 New Member
Thanks a lot for this code
with regards
chinmaya
True. You write a function to do this using bit operations:
Expand|Select|Wrap|Line Numbers
  1. void SwapNybble(unsigned short& arg)
  2. {
  3.     unsigned char LSB  =  arg & 0x00ff;
  4.     unsigned char MSB = (arg & 0xff00) >> 8;
  5.     arg = 0;
  6.     arg = LSB;
  7.     arg |= (arg << 8) | MSB;
  8. }
  9. int main()
  10. {
  11.      unsigned short data = 0xff00;
  12.      bitset<16> b(data);
  13.      cout << b.to_string() << endl;
  14.      SwapNybble(data);
  15.      b = data;
  16.      cout << b.to_string() << endl;
  17. }
  18.  
Aug 22 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
It occurred to me last night that I should have used an unsigned char rather than an unsigned short. A nybble is 4 bits and my example uses 8 bits.

Oops.
Aug 22 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
16
by: Sethu Madhavan | last post by:
Hi All Here i am facing one technical issue related to Swapping the Object Instance in Dotnet. e.g.. class Customer{ Order _lastOrder;
4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
2
by: Sam Marrocco | last post by:
Does anyone know of a method of swapping collection items? For example: Current collection: ..Item(1)="a" ..Item(2)="b" ..Item(3)="c" After swapping items 2 and 1.... ..Item(1)="b"...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
2
by: Protoman | last post by:
How would I write a user defnable letter swapping algorithm? I've written an Enigma encrypting program, and I need to redo the plugboard function. Right now, the letter swapping is fixed. I need to...
1
by: subramanian100in | last post by:
Will the following code always work ? #include <stdio.h> #include <stdlib.h> # define SIZE 50 void myswap(char **name1, char **name2); int main(void)
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.