M
mike7411
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
C++?
(i.e. 00000001 becomes 10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
Martin said:int BitField[256];
for (int i = 0; i < 256; i++)
{
int k = i;
BitField = 0;
for (int b = 0; b < 8; b++)
{
BitField <<= 1;
BitField |= k & 1;
k >>= 1;
}
}
If you want safer code, use a vector instead of an array:
vector<int> BitField(256);
Martin said:No. There is only the hard way
[..code not involving a table redacted..]
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
inline unsigned reverse(unsigned val)
{
return reverser_imp<numeric_limits<unsigned char>::digits>::
reverse(val, numeric_limits<unsigned char>::max());
}
Frederick said:Pete Becker posted:
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.
Frederick said:Macros muck this up completely.
Frederick said:Macros muck this up completely.
Yup. They're evil. Doesn't matter how you rationalize it.
[rationalizaton snipped]
It's far better to write long winded, unmaintainable, compiler-busting
templates than to resort to macros. That's the C++ way!
Frederick said:Pete Becker posted:
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:
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.
Frederick said:Pete Becker posted:
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.
Frederick said:Pete Becker posted:
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.
Pete Becker said:Now you've seen the light.
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.
Frederick said: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;
Nate said:what about:
#undef CHAR_BIT
int CHAR_BIT = 5;
or is that Undefined Behavior?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.