473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

swap bit in C

I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris
Jul 19 '05 #1
5 17812
Hello,

Chris <ch***@usercom.com> writes:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary What is the easiet way?


Assuming that you want to use 8 bits of whatever bytes you have, you could use

char swapbyte(unsigned char c)
{ unsigned char result=0;
for(int i=0;i<8;++i)
{ result=result<<1;
result|=(c&1);
c=c>>1;
}
return result;
}

Or, another possibility

(c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0 )|((c&16)?8:0)
|((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0)

Have a nice day,
Chris Dams
Jul 19 '05 #2
One solution I found is:
byte byteSwap, resultSwap;

byteSwap = 0;
for( k = 0; k < 8; ++k ) {//process 8 bits

byteSwap *= 2;

if( resultSwap & 2 != 0 )
byteSwap += 1;

resultSwap /= 2;
}

Chris

On Mon, 10 Nov 2003 15:32:34 -0500, Chris <ch***@usercom.com> wrote:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris


Jul 19 '05 #3
"Chris" <ch***@usercom.com> wrote in message
news:7e********************************@4ax.com...
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?


A lookup table.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #4
Chris wrote:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris


If you have a 64 bit machine with 64 bit integer support
and you need to swap lotsa bits in bytes, this algorithm will swap 8
bytes at a time in around 20 instructions.

typedef unsigned long long swap_t;

inline swap_t swapbitsinbytes( swap_t v )
{

const swap_t h_mask_1 = 0xaaaaaaaaaaaaaaaaLL;
const swap_t l_mask_1 = 0x5555555555555555LL;

const swap_t h_mask_2 = 0xccccccccccccccccLL;
const swap_t l_mask_2 = 0x3333333333333333LL;

const swap_t h_mask_4 = 0xf0f0f0f0f0f0f0f0LL;
const swap_t l_mask_4 = 0x0f0f0f0f0f0f0f0fLL;

v = ( ( v & h_mask_1 ) >> 1 ) | ( ( v & l_mask_1 ) << 1 );
v = ( ( v & h_mask_2 ) >> 2 ) | ( ( v & l_mask_2 ) << 2 );
return ( ( v & h_mask_4 ) >> 4 ) | ( ( v & l_mask_4 ) << 4 );

}

#include <iostream>

int main()
{

std::cout << std::hex << swapbitsinbytes( 0x0102040810204080LL );
}

Jul 19 '05 #5
Thank you for all your reply.

Chris
On Mon, 10 Nov 2003 15:32:34 -0500, Chris <ch***@usercom.com> wrote:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris


Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I am still struggling with the rules for name lookup. Please consider: namespace xxx {
14
by: Otto Meijer | last post by:
Hi everyone, for one of my projects I need to figure out the size of the swap file(s) of a certain system. The problem is I need to do this on a host of platforms like: HP_UX, Solaris, Linux,...
9
by: Jongmin Lee | last post by:
Hi Everybody, I have very simple code snippet to explain my problem. Class "Swap" is construncted in "Main" with two initial variables. Later, "Swap" class is going to swap those two...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.