Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > print bytes tha...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 24995 of 27343
Post > Topic >>

print bytes that have reversed bits.

by ericmatteson2003november@[EMAIL PROTECTED] Mar 17, 2008 at 03:33 PM

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.
 




 7 Posts in Topic:
print bytes that have reversed bits.
ericmatteson2003november@  2008-03-17 15:33:59 
Re: print bytes that have reversed bits.
user923005 <dcorbit@[E  2008-03-17 16:29:56 
Re: print bytes that have reversed bits.
Ben Bacarisse <ben.use  2008-03-17 23:53:36 
Re: print bytes that have reversed bits.
Richard Heathfield <rj  2008-03-18 05:46:23 
Re: print bytes that have reversed bits.
ericmatteson2003november@  2008-03-24 18:34:29 
Re: print bytes that have reversed bits.
Peter Nilsson <airia@[  2008-03-24 19:28:52 
Re: print bytes that have reversed bits.
pete <pfiland@[EMAIL P  2008-04-24 01:33:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 21:21:01 CDT 2008.