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