Article View: es.comp.os.linux.programacion
Article #10786Re: Inversion de bits
From: J.A. Gutierrez
Date: Wed, 06 Feb 2002 18:08
Date: Wed, 06 Feb 2002 18:08
73 lines
2396 bytes
2396 bytes
Jordi Canton <canton@eq.upc.es> wrote:
: Alguien conoce alguna manera elegante y eficiente de invertir el orden
: de todos los bits de un byte en C?
elegante, yo diria que no.
Eficiente, con una tabla.
: b0 b1 b2 b3 b4 b5 b6 b7
: y quiero obtener un byte con los bits de esta otra forma:
: b7 b6 b5 b4 b3 b2 b1 b0
---------------------------------------------------------------------------
#include <stdio.h>
#define invert_byte(a) ( \
((unsigned char)(a&0x80) >> 7) |\
((unsigned char)(a&0x40) >> 5) |\
((unsigned char)(a&0x20) >> 3) |\
((unsigned char)(a&0x10) >> 1) |\
((unsigned char)(a&0x08) << 1) |\
((unsigned char)(a&0x04) << 3) |\
((unsigned char)(a&0x02) << 5) |\
((unsigned char)(a&0x01) << 7) \
)
char *out_c(unsigned char c);
static char s[9];
main()
{
unsigned char c=0;
do
{
printf ( "%0.3d\t", c);
printf ( "%s\t", out_c(c));
printf ( "%s\t", out_c(invert_byte(c)));
printf ( "%0.3d\n", invert_byte(c));
c++;
}
while ( c>0 );
return(0);
}
char *out_c(unsigned char c)
{
char *p=s+7;
do
{
*p-- = ( "01"[(c&0x01)] );
c = c >> 1;
}
while ( p>=s );
return(s);
}
---------------------------------------------------------------------------
--
PGP and other useless info at \
http://www.cps.unizar.es/~spd/ \
finger://daphne.cps.unizar.es/spd \ Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/ \ (Virgilio)
Technical Details
Message-ID:
<a3rrec$vtt$1@news.unizar.es>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.rediris.es!news.unizar.es!not-for-mailReferences:
<3C61171A.5F9E4911@eq.upc.es>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII-7-bit
X-Google-Thread: f86c8,bd33173853cfd14f
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2002-02-06 09:38:45 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news.rediris.es!news.unizar.es!not-for-mail
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Inversion de bits
Date: 6 Feb 2002 18:08:12 GMT
Organization: Rank
Lines: 73
Distribution: es
Message-ID: <a3rrec$vtt$1@news.unizar.es>
References: <3C61171A.5F9E4911@eq.upc.es>
NNTP-Posting-Host: daphne.cps.unizar.es
X-Trace: news.unizar.es 1013018892 32701 155.210.152.20 (6 Feb 2002 18:08:12 GMT)
X-Complaints-To: usenet@news.unizar.es
NNTP-Posting-Date: 6 Feb 2002 18:08:12 GMT
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (IRIX/6.2 (IP22))