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<intBitFi eld(256);
Best regards, Martin
P.S. normally, I don't do other people's homework - SCNR this time ;)
>
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<intBitFi eld(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.
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_li mits;
using std::cout; using std::hex; using std::showbase;
using std::internal; using std::setw;
>
>
In my opinion, it's major overkill to use either of:
>
numeric_limits< char unsigned>::digi ts
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.
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.
>
Yup. They're evil. Doesn't matter how you rationalize it.
>
[rationalizaton snipped]
So _your_ rationalisation is this:
There's a undamaged wheel in a car-wreck, but we can't make use of it
because it's evil for a car to crash.
It's far better to write long winded, unmaintainable, compiler-busting
templates than to resort to macros. That's the C++ way!
Indeed it is, but not when it comes to the most basic of things, and there
is little more basic than the CHAR_BIT family of macros. Even if you choose
to never us CHAR_BIT and the like, you'll never be able to write:
>It's far better to write long winded, unmaintainable, compiler-busting
>templates than to resort to macros. That's the C++ way!
>
>
Indeed it is,
Now you've seen the light.
but not when it comes to the most basic of things, and there
is little more basic than the CHAR_BIT family of macros. Even if you choose
to never us CHAR_BIT and the like, you'll never be able to write:
>
namespace MyNamespace { int CHAR_BIT(); }
>
well... not in your header files in anyway.
>
That's because macros are evil. Always use templates. Never use macros.
If you can't write it as a template you haven't tried hard enough.
--
-- Pete
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see www.petebecker.com/tr1book.
That's because macros are evil. Always use templates. Never use macros.
If you can't write it as a template you haven't tried hard enough.
>
>
This has nothing to do with "Writing Templates" Vs "Writing Macros", and
everything to do with the Standard Library, which provides certain macros.
>
There's plenty of things you can do with macros which you _can't_ do with
templates.
Hey Fred, so that the rest of us can sleep easy, could you please post
something acknowledging that you knew that each of Pete's postings in
this subthread regarding templates vs. macros has been tongue-in-cheek.
Your responses have appeared to take Pete seriously, and perhaps you
have done so just to be funny, but if so I must confess that your humor
is sometimes over my head.
Comment