Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
Mar 22 '06
45 5233
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
santosh wrote: pete wrote: Ajay wrote: Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed. unsigned char bit_rev(unsigne d char byte) { unsigned hi_mask, lo_mask;
hi_mask = ((unsigned char)-1 >> 1) + 1;
This is probably a stupid question, but why not simply do hi_mask = 128;?
Because a char /may/ contain more than 8 bits.
The statement
hi_mask = ((unsigned char)-1 >> 1) + 1;
will result in hi_mask having a char value that only has it's high order
bit on, no matter what size (in bits) a char is.
OTOH,
hi_mask = 128;
is only guaranteed to set /a/ bit (not necessarily the high order bit)
to one.
[snip]
- --
Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEIuJLagV FX4UWr64RAiPqAJ 46Gyhsr/6x/nXtNBsc8BGiJmow cACg2EUF
eKgfiLiLY8ze4Dr sXA3pT5Q=
=k/wz
-----END PGP SIGNATURE-----
CBFalconer wrote: Charles Mills wrote: Charles Mills wrote: Ajay wrote:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
Use a look up table (untested generated code below):
static unsigned char reverse_byte(un signed char b) { static const unsigned char b_tbl[] = { ---8<---- sniped totally wrong lookup table ---8<---- }; return b_tbl[b]; }
probably want something like this: static const unsigned char b_tbl[] = { 0x0, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, ..., 0xf, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff };
you can fill in the blanks.
Works like a charm, NOT, when CHAR_BIT > 8. i.e. document hidden assumptions. In order of execution:
unsigned char* b_tbl = malloc(1 + UCHAR_MAX); ... /* code to initialize table, after checking it is non-NULL. */ ... /* code that uses the table */
Here is another non protable (but interesting) version based on code
from the book Hackers Delight:
#if CHAR_BIT != 8
# error ---> Code below only works on platforms with CHAR_BIT == 8
#endif
static unsigned char
reverse_byte(un signed char x)
{
x = (x & 0x55) << 1 | (x >> 1) & 0x55;
x = (x & 0x33) << 2 | (x >> 2) & 0x33;
x = (x & 0x0F) << 4 | (x >> 4) & 0x0F;
return x;
}
Nice divide and conquer approach
0x55 => 01010101
0x33 => 00110011
0x0F => 00001111
-Charlie
Lew Pitcher wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
santosh wrote: pete wrote: Ajay wrote: Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed. unsigned char bit_rev(unsigne d char byte) { unsigned hi_mask, lo_mask;
hi_mask = ((unsigned char)-1 >> 1) + 1;
This is probably a stupid question, but why not simply do hi_mask = 128;?
Because a char /may/ contain more than 8 bits.
The statement hi_mask = ((unsigned char)-1 >> 1) + 1; will result in hi_mask having a char value that only has it's high order bit on, no matter what size (in bits) a char is.
OTOH, hi_mask = 128; is only guaranteed to set /a/ bit (not necessarily the high order bit) to one.
[snip]
Thanks.
santosh wrote: pete wrote: Ajay wrote: Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed. unsigned char bit_rev(unsigne d char byte) { unsigned hi_mask, lo_mask;
hi_mask = ((unsigned char)-1 >> 1) + 1;
This is probably a stupid question, but why not simply do hi_mask = 128;?
Because there might be more than 8 bits in a byte. I used to work on
systems with 16 bit bytes regularly. lo_mask = 1; do { if (!(byte & hi_mask) != !(byte & lo_mask)) { byte ^= hi_mask | lo_mask; } hi_mask >>= 1; lo_mask <<= 1; } while (hi_mask > lo_mask); return byte; }
wow, that took me quite a while to figure out! Just out of curiosity, is this the "most efficient" method to reverse bits that you know of?
That depends. On some systems it might be highly inefficient, on other
highly efficient.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
pete wrote: Ajay wrote: Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
unsigned char bit_rev(unsigne d char byte) { unsigned hi_mask, lo_mask;
hi_mask = ((unsigned char)-1 >> 1) + 1;
[snip rest of function definition]
Does this work for one's complement or any other representation of
negative numbers besides two's complement?
Maybe
hi_mask = (UCHAR_MAX + 1) >> 1;
is more portable?
--
If you're posting through Google read <http://cfaj.freeshell. org/google>
santosh wrote: jaysome wrote:
.... snip ... (I just searched all 448 source files of one project I work on. Only one references CHAR_BIT (yippee for me!). I wouldn't be surprised if many of the regulars here would search their source code and find 0 instances of use of CHAR_BIT. And I would be eager to hear about how such lack of usage of CHAR_BIT affects portability.)
My guess--kind of reiterated--is that those who know better will get it right, and those that don't know better will, well, be just fine.
Nevertheless, all other factor being equal, if there's a fully portable way to do something, then that is to be preferred over making assumptions and hard-coding values. That's the point CBFalconer was making to Charles Mills.
Thank you. And, lacking that, document the assumption. E.G:
assert(8 == CHAR_BIT);
--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Pedro Graca wrote: pete wrote: Ajay wrote: Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed. unsigned char bit_rev(unsigne d char byte) { unsigned hi_mask, lo_mask;
hi_mask = ((unsigned char)-1 >> 1) + 1;
[snip rest of function definition]
Does this work for one's complement or any other representation of negative numbers besides two's complement?
Yes.
Maybe
hi_mask = (UCHAR_MAX + 1) >> 1;
is more portable?
Not more portable, but slightly better style, perhaps.
(UCHAR_MAX == (unsigned char)-1) is always true,
regardless of the representation of negative integers.
N869
6.3.1.3 Signed and unsigned integers
[#1] When a value with integer type is converted to another
integer type other than _Bool, if the value can be
represented by the new type, it is unchanged.
[#2] Otherwise, if the new type is unsigned, the value is
converted by repeatedly adding or subtracting one more than
the maximum value that can be represented in the new type
until the value is in the range of the new type.
((unsigned char)-1 == -1 + UCHAR_MAX + 1)
((unsigned char)-1 == UCHAR_MAX )
--
pete
pete wrote: Pedro Graca wrote: Maybe
hi_mask = (UCHAR_MAX + 1) >> 1;
is more portable?
Not more portable, but slightly better style, perhaps.
Actually, (UCHAR_MAX + 1) is not portable.
If UCHAR_MAX is equal to UINT_MAX, then
(hi_mask = (UCHAR_MAX + 1) >> 1)
is equal to zero.
--
pete
pete wrote: Actually, (UCHAR_MAX + 1) is not portable.
If UCHAR_MAX is equal to UINT_MAX, then (hi_mask = (UCHAR_MAX + 1) >> 1) is equal to zero.
What about
hi_mask = (UCHAR_MAX >> 1) + 1;
--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Pedro Graca wrote: pete wrote: Actually, (UCHAR_MAX + 1) is not portable.
If UCHAR_MAX is equal to UINT_MAX, then (hi_mask = (UCHAR_MAX + 1) >> 1) is equal to zero.
What about
hi_mask = (UCHAR_MAX >> 1) + 1;
That's what I thought you had written when I replied
"Not more portable, but slightly better style, perhaps."
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin |
last post by:
Hello,
I need to some help in reversing an 2-dimensional array. I am working
with gif images and I am trying to make the mirror image. I was
hoping that someone could help give me a headstart in how I can
accomplish this. Also, I don't know the the size of the array before
hand as the image can be any size. I already have the my read and
write gif functions working, but I just need to know how to reverse
the contents.
|
by: Tim Marshall |
last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users
to pick colours for various control properties in certain apps.
Is there a way to take the colour code that is displayed in a
backcolor/forecolor/etc property and calculate the "reverse colour"? In
other words, If a user picks 255 (red) for a control backcolor, I'd like
to be able to calculate the opposite or negative of that colour and
assign the control's...
|
by: arnuld |
last post by:
i have created a solutions myself. it compiles without any trouble
and runs but it prints some strange characters. i am not able to find
where is the trouble.
--------------------------------- PROGRAMME
--------------------------------
/* K&R2 section 1.9
exercise 1.19
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |