print bytes that have reversed bits.

  • Thread starter ericmatteson2003november
  • Start date
E

ericmatteson2003november

This example program in c reverses order
of bits within a byte and
prints the integer byte result out on screen in
base 10 ascii including suppressing
leading zeroes.
program listing is next */
// this revmake.c was written by Eric Matteson.
// permission is granted to copy this program.
// reverse bits in each number in array 0-255;
// 1 becomes -128
// 2 becomes 64
// 4 becomes 32
#include<stdio.h>
int hmanyout(char* outres,int insign)
{
int intun,base,irem,iprod,frac,isgn;
int revctr,fwctr;
int dst[15];
revctr=10;
isgn=0;
base=10;
intun=insign;
if(insign < 0)
{
intun = 0 - insign;
isgn=1;
}
while(revctr >= 0)
{
frac = intun / base;
iprod = base * frac;
irem = intun - iprod;
if(irem < 0)
{
frac = frac - 1;
iprod = base * frac;
irem = intun - iprod;
}
dst[revctr] = irem;
intun = frac;
revctr = revctr - 1;
}
revctr=0;
irem=0;
while((irem == 0)&&(revctr < 9))
{
revctr=revctr+1;
irem=dst[revctr];
if(irem != 0)revctr=revctr-1;
}
// max revctr == 9 rightmost digit at dst[10]
if(isgn == 1)
{
*(outres)='-';
}
while(revctr < 10)
{
irem=dst[revctr+1]+48;
*(outres + isgn)=(char)irem;
isgn=isgn+1;
revctr=revctr+1;
}
if(insign != (0-1))
{
*(outres + isgn)=',';
isgn = isgn + 1;
}
if(insign == (0-1))
{
*(outres + isgn)='}';
*(outres + (isgn+1))=';';
isgn = isgn + 2;
}
*(outres + isgn)=(char)13;
*(outres + (isgn+1))=(char)10;
return isgn;
}
int revbits(int revin)
{
int revout,revctr,revterm;
revctr=0;
revout=0;
revterm=revin;
revloop: revctr=revctr+1;
revout = revout + revout;
revout = revout + (revterm & 1);
revterm = revterm >> 1;
if(revctr < 8)goto revloop;
return revout;
}
int main(char** uuc,int ui)
{
char tex[80];
int olop,ilop,tctr,twid,treb,xctr;
FILE* revhan;
revhan = stdout;
xctr=0;
olop=0;
while(olop < 32)
{
tctr=0;
while(tctr < 80)
{
tex[tctr]=(char)32;
tctr=tctr+1;
}
twid=4 ;
if(olop == 0)
{
tex[3]='i';
tex[4]='n';
tex[5]='t';
// 6 is blank
tex[7]='t';
tex[8]='r';
tex[9]='e';
tex[10]='v';
tex[11]='[';
tex[12]=']';
tex[13]='=';
tex[14]='{';
// length = 15;
twid=15;
}
fwrite(tex,1,twid,revhan);
ilop=0;
while(ilop < 8)
{
treb=revbits(xctr);
if(treb > 127)treb=treb-256;
twid=hmanyout(tex,treb);
if(ilop == 7)twid = twid + 2;
xctr=xctr+1;
fwrite(tex,1,twid,revhan);
ilop=ilop+1;
}
olop=olop+1;
}
return 0;
}
// this is last line of revmake.c
/* after end of program.
 
U

user923005

 This example program in c reverses order
of bits within a byte and
prints the integer byte result out on screen in
base 10 ascii including suppressing
leading zeroes.
program listing is next */
// this revmake.c was written by Eric Matteson.
// permission is granted to copy this program.
// reverse bits in each number in array  0-255;
// 1 becomes -128
// 2 becomes 64
// 4 becomes 32
  #include<stdio.h>
  int hmanyout(char* outres,int insign)
  {
  int intun,base,irem,iprod,frac,isgn;
  int revctr,fwctr;
  int dst[15];
  revctr=10;
  isgn=0;
  base=10;
  intun=insign;
    if(insign < 0)
    {
    intun = 0 - insign;
    isgn=1;
    }
    while(revctr >= 0)
    {
    frac = intun / base;
    iprod = base * frac;
    irem = intun - iprod;
     if(irem < 0)
     {
     frac = frac - 1;
     iprod = base * frac;
     irem = intun - iprod;
     }
    dst[revctr] = irem;
    intun = frac;
    revctr = revctr - 1;
    }
  revctr=0;
  irem=0;
    while((irem == 0)&&(revctr < 9))
    {
    revctr=revctr+1;
    irem=dst[revctr];
    if(irem != 0)revctr=revctr-1;
    }
// max revctr == 9       rightmost digit at dst[10]
    if(isgn == 1)
    {
    *(outres)='-';
    }
    while(revctr < 10)
    {
    irem=dst[revctr+1]+48;
    *(outres + isgn)=(char)irem;
    isgn=isgn+1;
    revctr=revctr+1;
    }
    if(insign != (0-1))
    {
    *(outres + isgn)=',';
    isgn = isgn + 1;
    }
    if(insign == (0-1))
    {
    *(outres + isgn)='}';
    *(outres + (isgn+1))=';';
    isgn = isgn + 2;
    }
  *(outres + isgn)=(char)13;
  *(outres + (isgn+1))=(char)10;
  return isgn;
  }
  int revbits(int revin)
  {
  int revout,revctr,revterm;
  revctr=0;
  revout=0;
  revterm=revin;
revloop:  revctr=revctr+1;
  revout = revout + revout;
  revout = revout + (revterm & 1);
  revterm = revterm >> 1;
  if(revctr < 8)goto revloop;
  return revout;
  }
  int main(char** uuc,int ui)
  {
  char tex[80];
  int olop,ilop,tctr,twid,treb,xctr;
  FILE* revhan;
  revhan = stdout;
  xctr=0;
  olop=0;
  while(olop < 32)
  {
  tctr=0;
  while(tctr < 80)
  {
  tex[tctr]=(char)32;
  tctr=tctr+1;
  }
  twid=4 ;
  if(olop == 0)
  {
  tex[3]='i';
  tex[4]='n';
  tex[5]='t';
//    6 is blank
  tex[7]='t';
  tex[8]='r';
  tex[9]='e';
  tex[10]='v';
  tex[11]='[';
  tex[12]=']';
  tex[13]='=';
  tex[14]='{';
// length = 15;
  twid=15;
  }
  fwrite(tex,1,twid,revhan);
  ilop=0;
    while(ilop < 8)
    {
    treb=revbits(xctr);
    if(treb > 127)treb=treb-256;
    twid=hmanyout(tex,treb);
    if(ilop == 7)twid = twid + 2;
    xctr=xctr+1;
    fwrite(tex,1,twid,revhan);
    ilop=ilop+1;
    }
  olop=olop+1;
  }
  return 0;
  }
// this is last line of revmake.c
/* after end of program.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* I saw this from Dan Pop: */
unsigned rbits(unsigned int c)
{
c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
return c;
}

int main(void)
{
unsigned i;
for (i = 0; i <= UCHAR_MAX; i++)
printf("c=%u, reversed c = %u\n", i, rbits(i));
return 0;
}
 
R

Richard Heathfield

(e-mail address removed) said:
This example program in c reverses order
of bits within a byte and
prints the integer byte result out on screen in
base 10 ascii including suppressing
leading zeroes.

I was somewhat surprised to find that this program (after a little light
surgery on BCPL comments) actually compiled, in conforming mode. One of
its more interesting features is that although it appears to meet the
author's claim to display the integer results in ASCII, *regardless of the
native character set for the implementation*, nevertheless the non-integer
results use the native character set. So if you run this on a box that
uses ASCII, you'll notice nothing untoward. But if you run it on a
mainframe...

<snip>
 
E

ericmatteson2003november

(e-mail address removed) said:


I was somewhat surprised to find that this program (after a little light
surgery on BCPL comments) actually compiled, in conforming mode. One of
its more interesting features is that although it appears to meet the
author's claim to display the integer results in ASCII, *regardless of the
native character set for the implementation*, nevertheless the non-integer
results use the native character set. So if you run this on a box that
uses ASCII, you'll notice nothing untoward. But if you run it on a
mainframe...

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Try this other version on a mainframe ...
I have replaced adding to 48 with an array of
current character digits mframe[]
*/
// beginning of revmain.c
// revmain.c is written by Eric Matteson
// reverse bits in each number in array 0-255;
#include<stdio.h>
char mframe[]={"0123456789"};
int hmanyout(char* outres,int insign)
{
int intun,base,irem,iprod,frac,isgn;
int revctr,fwctr;
int dst[15];
revctr=10;
isgn=0;
base=10;
intun=insign;
if(insign < 0)
{
intun = 0 - insign;
isgn=1;
}
while(revctr >= 0)
{
frac = intun / base;
iprod = base * frac;
irem = intun - iprod;
if(irem < 0)
{
frac = frac - 1;
iprod = base * frac;
irem = intun - iprod;
}
dst[revctr] = irem;
intun = frac;
revctr = revctr - 1;
}
revctr=0;
irem=0;
while((irem == 0)&&(revctr < 9))
{
revctr=revctr+1;
irem=dst[revctr];
if(irem != 0)revctr=revctr-1;
}
// max revctr == 9 rightmost digit at dst[10]
if(isgn == 1)
{
*(outres)='-';
}
while(revctr < 10)
{
irem=dst[revctr+1];
// mframe[] is list of character digits
*(outres + isgn)=mframe[irem];
isgn=isgn+1;
revctr=revctr+1;
}
if(insign != (0-1))
{
*(outres + isgn)=',';
isgn = isgn + 1;
}
if(insign == (0-1))
{
*(outres + isgn)='}';
*(outres + (isgn+1))=';';
isgn = isgn + 2;
}
*(outres + isgn)=(char)13;
*(outres + (isgn+1))=(char)10;
return isgn;
}
int revbits(int revin)
{
int revout,revctr,revterm;
revctr=0;
revout=0;
revterm=revin;
revloop: revctr=revctr+1;
revout = revout + revout;
revout = revout + (revterm & 1);
revterm = revterm >> 1;
if(revctr < 8)goto revloop;
return revout;
}
int main(int ui,char** uuc)
{
char tex[80];
int olop,ilop,tctr,twid,treb,xctr;
FILE* revhan;
revhan = stdout;
xctr=0;
olop=0;
while(olop < 32)
{
tctr=0;
while(tctr < 80)
{
tex[tctr]=(char)32;
tctr=tctr+1;
}
twid=4 ;
if(olop == 0)
{
tex[3]='i';
tex[4]='n';
tex[5]='t';
// 6 is blank
tex[7]='t';
tex[8]='r';
tex[9]='e';
tex[10]='v';
tex[11]='[';
tex[12]=']';
tex[13]='=';
tex[14]='{';
// length = 15;
twid=15;
}
fwrite(tex,1,twid,revhan);
ilop=0;
while(ilop < 8)
{
treb=revbits(xctr);
if(treb > 127)treb=treb-256;
twid=hmanyout(tex,treb);
if(ilop == 7)twid = twid + 2;
xctr=xctr+1;
fwrite(tex,1,twid,revhan);
ilop=ilop+1;
}
olop=olop+1;
}
return 0;
}
// end of revmain.c
/* --
 
P

Peter Nilsson

Richard Heathfield said:
...
I was somewhat surprised to find that this program (after
a little light surgery on BCPL comments) actually compiled,
in conforming mode. One of its more interesting features
is that although it appears to meet the author's claim to
display the integer results in ASCII, *regardless of the
native character set for the implementation*, nevertheless
the non-integer results use the native character set. So
if you run this on a box that uses ASCII, you'll notice
nothing untoward. But if you run it on a mainframe...

Try this other version on a mainframe ...
I have replaced adding to 48 with an array of
current character digits mframe[]

Why? Note that '0', '1', '2' ... '9' are required to be
consecutive, i.e. '1' == '0' + 1, '2' == '0' + 2, etc...
up to '9' = '0' + 9;

The point is, why assume or require ASCII?
  */
// beginning of revmain.c
// revmain.c is written by Eric Matteson
// reverse bits in each number in array  0-255;

<snip 120+ line program>

#include <stdio.h>

unsigned char rev_octet(unsigned char x)
{
unsigned y, i;
for (y = i = 0; i < 8; i++, x >>= 1)
y = (y << 1) | (x & 1u);
return y;
}

int main(void)
{
int i;

printf("int trev[]={\n");

for (i = 0; i < 256; i++)
{
int x = rev_octet(i);
if (x >= 0x80) x = x - 256; /* 8-bit 2c */
printf("%d", x);
if (i != 255) putchar(',');
if ((i + 1) % 8 == 0) putchar('\n');
}

printf("}\n");

return 0;
}

That said, I'd actually use the pre-processor in the
destination source file...

#define B8(p) B7(p), B7(p + 1)
#define B7(p) B6(p), B6(p + 2)
#define B6(p) B5(p), B5(p + 4)
#define B5(p) B4(p), B4(p + 8)
#define B4(p) B3(p), B3(p + 16)
#define B3(p) B2(p), B2(p + 32)
#define B2(p) B1(p), B1(p + 64)
#define B1(p) p , p - 128

int trev[] = { B8(0) };
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top