THE GAME :
Write a C function to swap the bits of a char so
that its bits would become the mirror image of the
char.MSBs become its LSBs etc. E.g. 11001100
binary would become 00110011 binary.
---------------------------------------
Sep 4 '06
77 3227
Aman JIANG wrote:
THE GAME :
Write a C function to swap the bits of a char so
that its bits would become the mirror image of the
char.MSBs become its LSBs etc. E.g. 11001100
binary would become 00110011 binary.
This is exciting! Here's my attempt. I hope it's of use to you. I haven't
really tested it thoroughly, so I hope there are no mistakes.
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int swap_bits(int b) {
int result = 0;
char digits[CHAR_BIT + 1];
char digits_rev[CHAR_BIT + 1] = {0};
int n;
for (n = 0; n != CHAR_BIT; ++n) {
sprintf(digits + n, "%i", !!((1 << n) & b));
}
for (n = 0; n != CHAR_BIT; ++n) {
digits_rev[n] = digits[CHAR_BIT - n - 1];
}
for (n = 0; n != CHAR_BIT; ++n) {
result |= (digits_rev[n] - '0') << n;
}
return result;
}
S.
Aman JIANG posted:
unsigned char RevChar(unsigne d char Byte)
{
typedef unsigned long tpname;
assert(sizeof(t pname) == sizeof(void*));
tpname byte = (tpname)Byte;
return (unsigned char)
(((byte & 128) >7)
| ((byte & 64) >5)
| ((byte & 32) >3)
| ((byte & 16) >1)
| ((byte & 8) << 1)
| ((byte & 4) << 3)
| ((byte & 2) << 5)
| ((byte & 1) << 7));
}
Since when is a byte eight bits?
If I wanted to mirror-image an unsigned char, I'd probably start off with
something like:
#include <limits.h>
char unsigned Mirror(char unsigned const val_byte)
{
unsigned const val = val_byte;
unsigned mirror = 0;
unsigned single_bit_val = 1U << CHAR_BIT-1;
unsigned single_bit_mirr or = 1;
do
{
if(val & single_bit_val) mirror |= single_bit_mirr or;
else mirror &= ~single_bit_mir ror;
}
while(single_bi t_mirror <<= 1,
(single_bit_val >>= 1) != (unsigned)UCHAR _MAX+1);
return mirror;
}
--
Frederick Gotham
Frederick Gotham wrote:
Since when is a byte eight bits?
If I wanted to mirror-image an unsigned char, I'd probably start off with
something like:
Yes, eight bits.
and... was ur code right ?
Aman JIANG wrote:
unsigned char RevChar(unsigne d char Byte)
{
typedef unsigned long tpname;
assert(sizeof(t pname) == sizeof(void*));
tpname byte = (tpname)Byte;
return (unsigned char)
(((byte & 128) >7)
| ((byte & 64) >5)
| ((byte & 32) >3)
| ((byte & 16) >1)
| ((byte & 8) << 1)
| ((byte & 4) << 3)
| ((byte & 2) << 5)
| ((byte & 1) << 7));
}
Yes... Yes... and ALL jeerer here,
can you overtake me on performance ?
Possibly. The C Standard says nothing about the speeds
of operations on different data types, which are inherently
implementation-dependent. On some machines, though, it will
turn out that `unsigned long' arithmetic is slower than the
ordinary `int' arithmetic you would get if you were to
work directly with `byte' instead of with `Byte'.[*]
Here's an easy optimization, though: Get rid of the
completely pointless assert() call. It asserts nothing that
is of any importance to the code, and if it takes any time
that time is pure waste.
[*] On some systems, arithmetic on `byte' will be done
in `unsigned int' rather than in `int'. However, such systems
use a character that's at least sixteen bits wide, so your
code would be wrong anyhow.
if you can, this is my homework.
BUT if you can't, it's just ur homework, okay ?
PS: donnot use TABLE plz.
Why not? Did your teacher forbid it?
--
Eric Sosman es*****@acm-dot-org.invalid
Frederick Gotham wrote:
>
.... snip ...
>
Since when is a byte eight bits?
Since an octal 7400 series buffer cost one dollar.
--
"The French have no word for entrepreneur." - George W. Bush
"Those who enter the country illegally violate the law."
- George W. Bush in Tucson, Ariz., Nov. 28, 2005
"I hear the voices". - G W Bush, 2006-04-18
jacob navia said:
Richard Heathfield a écrit :
>ravichoudhar i said:
>>>it is just complement operation, can be done using ~ operator.
For the example posted, that would work. But it would not give a true mirror image on many inputs. Using ~, 10000000 would become 01111111 rather than 00000001.
Yes but that's this guy problem. If he asks other people to do his/her
homework he can't ask also for correct answers :-)
I agree. I did not provide a correct answer. I merely commented on an
incorrect answer.
--
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)
>Since when is a byte eight bits?
CBFalconer posted:
Since an octal 7400 series buffer cost one dollar.
Aman JIANG posted:
Yes, eight bits.
The quantity of bits which comprises a byte is implementation-defined and can
be determined from the macro CHAR_BIT.
Even if 99.9999% of machines have 8-Bit bytes, that has no bearing whatsover
on the definition of the specification of the C Programming Language.
I would expect your original function to check for 8-Bit bytes:
#include <limits.h>
void Func(void)
{
#if CHAR_BIT != 8
#error "This code is not Standard C code -- bytes must be 8-Bit."
#endif
}
--
Frederick Gotham
Eric Sosman wrote:
Why not? Did your teacher forbid it?
1. I am NOT a Student. OK?
2. TABLE is NOT a true algorithm for this.
Aman JIANG wrote:
unsigned char RevChar(unsigne d char Byte)
{
typedef unsigned long tpname;
assert(sizeof(t pname) == sizeof(void*));
What is the point of this assert?
tpname byte = (tpname)Byte;
What is the point of this typecast?
return (unsigned char)
(((byte & 128) >7)
| ((byte & 64) >5)
| ((byte & 32) >3)
| ((byte & 16) >1)
| ((byte & 8) << 1)
| ((byte & 4) << 3)
| ((byte & 2) << 5)
| ((byte & 1) << 7));
}
Yes... Yes... and ALL jeerer here,
can you overtake me on performance ?
Quite possibly. On many platforms, a lookup table can be much faster
(especially a small one that only needs 255 elements).
if you can, this is my homework.
BUT if you can't, it's just ur homework, okay ?
PS: donnot use TABLE plz.
Why not? What if the most optimal solution uses a table?
--
Clark S. Cox III cl*******@gmail .com
Aman JIANG wrote:
<snip>
PS: donnot use TABLE plz.
That means I can't do it since all the computers here are on tables.
--
Flash Gordon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bryan Krone |
last post by:
I have a stream of data comming off a serial port at 19200. I am wondering
what is the most efficient way to grep through the data in realtime? I have
20 or so different strings I need to find. All of which are ~15 characters
or less. Currently I'm using code that looks like this:
forever loop
{
sysread the serial buffer into $newdata
if( defined $newdata )
|
by: jacob navia |
last post by:
As everybody knows, C uses a zero delimited unbounded
pointer for its representation of strings.
This is extremely inefficient because at each query of the
length of the string, the computer starts an unbounded
memory scan searching for a zero that ends the string.
A more efficient representation is:
struct string {
|
by: Russ Brown |
last post by:
Hello all,
Recently a post on this list made me think a bit about the way in which I
write my queries.
I have always written queries with ordinary joins in this manner:
SELECT * FROM a, b WHERE a.x=b.x;
However I recently saw an laternative syntax:
|
by: Phil Endecott |
last post by:
Dear Experts,
I have a couple of questions about the efficiency of queries involving
views.
Say I have a large table T, and a view V that just adds some extra
columns to T, using for example some date-to-text formatting functions.
The functions are defined as immutable. Now I "select * from V where
pkey=xxxxx". My hope was that the "where" filter would run on the table
T and the functions would only run on the single row that is...
|
by: extrudedaluminiu |
last post by:
Hi,
Is there any group in the manner of the C++ Boost group that works on
the evolution of the C language? Or is there any group that performs an
equivalent function?
Thanks,
-vs
| |
by: burningsunorama |
last post by:
Hi guys!
This is maybe a too 'academic problem', but I would like to hear your
opinions, something like pros and cons for each approach....
... Recently we've had at work a little talk about the way of providing
const modifier for function parameters. From my point of view are, of
course, design requirements always more important and thus one should
always use const keyword with parameters whose values shouldn't get
changed inside a...
|
by: Frederick Gotham |
last post by:
I'm trying to write extremely efficient, fully portable algorithms to
determine if an integer is odd or even. The most basic algorithms would be:
#define IS_ODD(x) ((x) % 2)
#define IS_EVEN(x) (!((x) % 2))
Attempting to make them slightly more efficient, we could change them to:
#define IS_ODD(x) ((x) & 1)
|
by: vamshi |
last post by:
Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
|
by: OldBirdman |
last post by:
Efficiency
I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm not concerned when dealing with user typing, but I am if a procedure is called by a query.
Does the VBA compiler generate "in-line" code for some apparent function calls? For example, y = Abs(x) might be compiled as y = x & mask. The string...
|
by: john |
last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned:
"Maybe your first idea for a two-dimensional vector was something like this:
class Matrix {
valarray< valarray<doublev;
public:
// ...
};
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |