Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reverse bit order

Reply
Thread Tools

reverse bit order

 
 
mike7411@gmail.com
Guest
Posts: n/a
 
      10-09-2006
Is there any easy way to reverse the order of the bits in a byte in
C++?

(i.e. 00000001 becomes 10000000)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Is there any easy way to reverse the order of the bits in a byte in
> C++?
>
> (i.e. 00000001 becomes 10000000)


http://www.google.com/search?hl=en&q=reverse+bits+byte

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Martin Steen
Guest
Posts: n/a
 
      10-10-2006
(E-Mail Removed) wrote:
> Is there any easy way to reverse the order of the bits in a byte in
> C++?
>
> (i.e. 00000001 becomes 10000000)
>


No. There is only the hard way
You have to shift the bits one by one.
The best idea is to calculate an array
of 256 values that contain the reversed bits.
Then you can "look up" in that array. Here
is the code to make the array (named "BitField"):

int BitField[256];
for (int i = 0; i < 256; i++)
{
int k = i;
BitField[i] = 0;
for (int b = 0; b < 8; b++)
{
BitField[i] <<= 1;
BitField[i] |= k & 1;
k >>= 1;
}
}

If you want safer code, use a vector instead of an array:
vector<int> BitField(256);

Best regards, Martin

P.S. normally, I don't do other people's homework - SCNR this time


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-10-2006
Mike posted:

> Is there any easy way to reverse the order of the bits in a byte in
> C++?
>
> (i.e. 00000001 becomes 10000000)



Here's some code I wrote last month. It's written in C, but you'll get the
idea:

http://groups.google.ie/group/comp.l...473fb7f?hl=en&

--

Frederick Gotham
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-10-2006
Martin Steen wrote:
>
> int BitField[256];
> for (int i = 0; i < 256; i++)
> {
> int k = i;
> BitField[i] = 0;
> for (int b = 0; b < 8; b++)
> {
> BitField[i] <<= 1;
> BitField[i] |= k & 1;
> k >>= 1;
> }
> }
>
> If you want safer code, use a vector instead of an array:
> vector<int> BitField(256);
>


How is that safer? Either way looks equally correct. If you're worried
about typos, it's far better to use a manifest constant instead of that
hardcoded value of 256.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-10-2006
Martin Steen wrote:
> (E-Mail Removed) wrote:
>> Is there any easy way to reverse the order of the bits in a byte in
>> C++?
>>
>> (i.e. 00000001 becomes 10000000)
>>

>
> No. There is only the hard way


If there are only 8 bits in a byte (and nobody said it was the actual
requirement), then a table of 255 values would be the easiest way.

if (mybyte)
mybyte = mytable[mybyte];

Now, generation of the table can be done once, outside of your program.

> [..code not involving a table redacted..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-10-2006
(E-Mail Removed) wrote:
> Is there any easy way to reverse the order of the bits in a byte in
> C++?
>
> (i.e. 00000001 becomes 10000000)
>


The one true way is to recognize that reversing any sequence of bits
involves only splitting it into two parts and returning a value whose
upper half is the reverse of the original lower half and whose lower
half is the reverse of the original upper half. Like this:

#include <limits>
#include <iomanip>
#include <iostream>
using std::numeric_limits;
using std::cout; using std::hex; using std::showbase;
using std::internal; using std::setw;

template <unsigned n> struct reverser_imp
{
static inline unsigned reverse(unsigned val, unsigned mask)
{
mask >>= (n/2);
return reverser_imp<n/2>::reverse((val >> (n/2)) & mask, mask)
| (reverser_imp<n/2>::reverse(val & mask, mask) << (n/2));
}
};

template <> struct reverser_imp<1>
{
static inline unsigned reverse(unsigned val, unsigned)
{
return val;
}
};

inline unsigned reverse(unsigned val)
{
return reverser_imp<numeric_limits<unsigned char>::digits>::
reverse(val, numeric_limits<unsigned char>::max());
}

void show_reversed(unsigned val)
{
cout.fill('0');
cout << hex << showbase << internal;
cout << setw(4) << val << ": "
<< setw(4) << reverse(val) << '\n';
}


int main()
{
show_reversed(0x0f);
show_reversed(0x80);
show_reversed(0x40);
show_reversed(0xC0);
return 0;
}


--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-10-2006
Pete Becker posted:

> inline unsigned reverse(unsigned val)
> {
> return reverser_imp<numeric_limits<unsigned char>::digits>::
> reverse(val, numeric_limits<unsigned char>::max());
> }



In my opinion, it's major overkill to use either of:

numeric_limits<char unsigned>::digits
numeric_limits<char unsigned>::max

The former can be retrieved from:

CHAR_BIT

, while the latter can be retrieved from:

(char unsigned)-1

or even:

UCHAR_MAX

--

Frederick Gotham
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-10-2006
Frederick Gotham wrote:
> Pete Becker posted:
>
>> inline unsigned reverse(unsigned val)
>> {
>> return reverser_imp<numeric_limits<unsigned char>::digits>::
>> reverse(val, numeric_limits<unsigned char>::max());
>> }

>
>
> In my opinion, it's major overkill to use either of:
>
> numeric_limits<char unsigned>::digits
> numeric_limits<char unsigned>::max
>
> The former can be retrieved from:
>
> CHAR_BIT
>
> , while the latter can be retrieved from:
>
> (char unsigned)-1
>
> or even:
>
> UCHAR_MAX
>


Haven't you gotten the word? Macros are evil. This is the 21st century.
Quaint C approaches should never be used. Templates, templates,
templates. Always.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      10-10-2006
Pete Becker posted:

> Haven't you gotten the word? Macros are evil. This is the 21st century.
> Quaint C approaches should never be used. Templates, templates,
> templates. Always.


Exceptions, exceptions, exception -- and not the kind you throw!

C++ has many "warts" (if you wish to call them that) which perpetuate from
its origins in C. We have accepted these warts, documented them, and moved
forward.

When you want to give something a name in C++ (be it a function, an object,
a class), then you don't need to pick a very unique name, because all you
need do is enclose it in a namespace:

namespace MyNamespace { int cout; }

Macros muck this up completely. However, there is a finite list of macros
which the Standard defines, such as:

INT_MAX
UCHAR_MAX
CHAR_BIT

As always, the Standard can take liberties wherever it pleases, and it
chooses to define these macros. If you genuinely perfer the numeric_limits
method, then go ahead. However, I myself find it awfully tedious and
longwinded, and I prefer good ol' CHAR_BIT.

While one should hesitate to define macros (for the obvious reasons),
there's no need to hesitate to use the ones that are already there, and
which will _always_ be there. Never will we be able to write:

int CHAR_BIT = 5;

Also, you might notice that "max" and "min" don't evaluate to a compile-
time constant, which make INT_MAX and the like all the more attractive.

--

Frederick Gotham
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SLV reverse bit order Luis Cupido VHDL 8 11-02-2012 04:05 PM
Reverse order of bit in repeating seqence of byte string imageguy Python 9 01-03-2009 10:08 PM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments