473,785 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reversing a byte

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 5229
jaysome said:
[...] I highly suspect that the OP will ever use an implementation
that defines "CHAR_BIT > 8".
If he sticks with C, it's almost inevitable in the long run.

Nor would I suspect that most other C programmers will.


Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else who
has written C for the same chip, and so have lots of other people writing C
for other similar chips, too. And such chips, common a few years ago, are
becoming yet more common all the time.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 23 '06 #21
Richard Heathfield wrote:
jaysome said:

[...] I highly suspect that the OP will ever use an implementation
that defines "CHAR_BIT > 8".

If he sticks with C, it's almost inevitable in the long run.
Nor would I suspect that most other C programmers will.

Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else who
has written C for the same chip, and so have lots of other people writing C
for other similar chips, too. And such chips, common a few years ago, are
becoming yet more common all the time.


Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers. After all, you are a revered
mavin here in c.l.c., and you most likely attract others of your type :)

To be sure--a table impementation is fine as long as you understand the
ramifications to portability. And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.

--
jay
Mar 23 '06 #22

jaysome wrote:
Richard Heathfield wrote:
jaysome said:

[...] I highly suspect that the OP will ever use an implementation
that defines "CHAR_BIT > 8".

If he sticks with C, it's almost inevitable in the long run.
Nor would I suspect that most other C programmers will.

Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else who
has written C for the same chip, and so have lots of other people writing C
for other similar chips, too. And such chips, common a few years ago, are
becoming yet more common all the time.


Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers. After all, you are a revered
mavin here in c.l.c., and you most likely attract others of your type :)

To be sure--a table impementation is fine as long as you understand the
ramifications to portability.


That's fair enough.
And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.


Can I borrow your crystal ball, please. I'm going to the races. ;-)

How can you be so sure that the next Big Thing in processors won't
have, say, 16 for CHAR_BIT? How long did it take from Intel 4004 to
80386? [non-ASCII native moan: After all, it's high time `char` really
becoming able to represent characters from more than a handful of
languages, without making us jump through hoops.]

Also, it's not necessarilly /today's/ programmers, rather the ones that
will maintain their code, or try to port it to future architectures.

--
BR, Vladimir

--
BR, Vladimir

Mar 23 '06 #23
Vladimir S. Oka wrote:
jaysome wrote:
Richard Heathfield wrote:

jaysome said:

[...] I highly suspect that the OP will ever use an implementation
that defines "CHAR_BIT > 8".
If he sticks with C, it's almost inevitable in the long run.


Nor would I suspect that most other C programmers will.
Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else who
has written C for the same chip, and so have lots of other people writing C
for other similar chips, too. And such chips, common a few years ago, are
becoming yet more common all the time.


Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers. After all, you are a revered
mavin here in c.l.c., and you most likely attract others of your type :)

To be sure--a table impementation is fine as long as you understand the
ramificatio ns to portability.

That's fair enough.

And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.

Can I borrow your crystal ball, please. I'm going to the races. ;-)

How can you be so sure that the next Big Thing in processors won't
have, say, 16 for CHAR_BIT?


When, and if, that happens, Microsoft, or some Open Source developers,
will put out an announcement stating that your existing C programs may
be in jeapordy if you have, in arguably a pedantic manner, assumed that
"CHAR_BIT == 8".

(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.

--
jay
Mar 23 '06 #24
jaysome wrote:
Vladimir S. Oka wrote:
jaysome wrote:
Richard Heathfield wrote:
jaysome said:

>[...] I highly suspect that the OP will ever use an implementation
>that defines "CHAR_BIT > 8".

If he sticks with C, it's almost inevitable in the long run.

>Nor would I suspect that most other C programmers will.

Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time,
<snip>

Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers. After all, you are a revered
mavin here in c.l.c., and you most likely attract others of your type :)

To be sure--a table impementation is fine as long as you understand the
ramificatio ns to portability.
That's fair enough.
And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.


Can I borrow your crystal ball, please. I'm going to the races. ;-)

How can you be so sure that the next Big Thing in processors won't
have, say, 16 for CHAR_BIT?


When, and if, that happens, Microsoft, or some Open Source developers,
will put out an announcement stating that your existing C programs may
be in jeapordy if you have, in arguably a pedantic manner, assumed that
"CHAR_BIT == 8".


You mean like for Y2K problem: s*itloads of money and time will be
spent fixing the avoidable mess?
(I just searched all 448 source files of one project I work on. Only one
references CHAR_BIT (yippee for me!).
Relevant only if your code /relies/ on CHAR_BIT size.
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.
I'd really like that crystal ball of yours...
And I would be eager to hear about how such lack of
usage of CHAR_BIT affects portability.)
Say your code relies on the fact that CHAR_BIT is 8 (or any other
value), and you use a table of 256 values for reversing a `char`
(sounds familiar?). Now, try to re-compile for the architecture with
CHAR_BIT being 9. See the point? I guess even your neat utility that
generates the said table relies on CHAR_BIT being 8, so re-compiling,
and re-running that doesn't help either. Now, where in all those 448
(or 1448, or 11448) source files was that pesky table?!
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.


They may be "just fine", I'm worried about someone (possibly me) having
to maintain and port their code, not to mention the possibility of
Ariane, Therac-25 and similar.

--
BR, Vladimir

Mar 23 '06 #25
jaysome wrote:
Vladimir S. Oka wrote:
How can you be so sure that the next Big Thing in processors won't
have, say, 16 for CHAR_BIT?


When, and if, that happens, Microsoft, or some Open Source developers,
will put out an announcement stating that your existing C programs may
be in jeapordy if you have, in arguably a pedantic manner, assumed that
"CHAR_BIT == 8".

(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.

Mar 23 '06 #26
"CBFalconer " wrote:

<snip table driven solution>
Works like a charm, NOT, when CHAR_BIT > 8. i.e. document hidden
assumptions.


Can we back up here?

The OP wanted to get a job, right? He was asked a stupid question, since
there are at least three ways to measure efficiency - footprint, speed,
programmer's time and clarity etc. The OP has two choices, he can say "WTF
do you mean" or he can give the answer this doofus actually wants, a table
look up. Is there even the tiniest doubt that that was the desired answer
to successfully continue the interview? Do you really think this guy
(hopefully from "Human Resources") cares or even knows about peculiar
representations of characters?
Mar 23 '06 #27

osmium wrote:
"CBFalconer " wrote:

<snip table driven solution>
Works like a charm, NOT, when CHAR_BIT > 8. i.e. document hidden
assumptions.


Can we back up here?

The OP wanted to get a job, right? He was asked a stupid question, since
there are at least three ways to measure efficiency - footprint, speed,
programmer's time and clarity etc. The OP has two choices, he can say "WTF
do you mean" or he can give the answer this doofus actually wants, a table
look up. Is there even the tiniest doubt that that was the desired answer
to successfully continue the interview? Do you really think this guy
(hopefully from "Human Resources") cares or even knows about peculiar
representations of characters?


If I was interviewed on C skills by an HR drone, I'd say "thank you"
and leave there and then. If I thought the engineer interviewing me
didn't "care or even know" about such things, I'd run as fast as I
could. If neither was the case, I'd expect to be grilled on just such
an point.

--
BR, Vladimir

Mar 23 '06 #28
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;
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;
}

--
pete
Mar 23 '06 #29
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;?
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?

Mar 23 '06 #30

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

Similar topics

4
5974
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.
11
8094
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...
8
4763
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
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
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...
1
10095
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,...
1
7502
isladogs
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...
0
5383
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...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.