NO

Author Topic: Bit Manipulations (Standard C code)  (Read 5510 times)

tbohon

  • Guest
Bit Manipulations (Standard C code)
« on: October 22, 2007, 06:13:06 PM »
When I taught college level programming classes I had an assignment that I gave out every term to my C Programming students.  I don't remember the wording of the assignment but it required all of the functions below to solve it - and this was the solution I would pass out after they had turned their assignments in for grading.  It was written in Mix C but should be easily adapted to just about any compiler.

Enjoy!

Tom

Code: [Select]
#include <stdio>

int len;
unsigned max_value;

main()
{
   unsigned uarg1;
   int iarg1, iarg2;

   printf("\nCalculating word length ...");
   wordlength();
   printf("\nWord length of this machine is %d bits\n",len);
   printf("Maximum value which can be stored ");
   printf("in one word is %u\n",max_value);

   printf("\n\nRotating values ...");
   uarg1 = 124;
   iarg1 = 3;
   rightrot(uarg1,iarg1);
   uarg1 = 336;
   iarg1 = 4;
   rightrot(uarg1,iarg1);
   uarg1 = 747;
   iarg1 = 1;
   rightrot(uarg1,iarg1);

   printf("\n\n\nInverting bits ... ");
   uarg1 = 1024;
   iarg1 = 5;
   iarg2 = 3;
   invert(uarg1,iarg1,iarg2);
   uarg1 = 577;
   iarg1 = 7;
   iarg2 = 5;
   invert(uarg1,iarg1,iarg2);
   uarg1 = 322;
   iarg1 = 6;
   iarg2 = 4;
   invert(uarg1,iarg1,iarg2);

   printf("\n\n\nNORMAL PROGRAM TERMINATION\n\n");
}       /* end of main() */

/* ----------------------------------------------------------------- */

wordlength()           /* calculates word length & max value */
{
   unsigned int value;
   int j;

   value = ~0;

   while (((value >>= 1) ^ 0) != 0)
        ++len;
   ++len;

   max_value = 1;
   for (j=(len-1); j>=0; --j)
        max_value = max_value * 2;
   max_value = max_value - 1;

   return(len,max_value);
}       /* end of wordlength() */

/* ----------------------------------------------------------------- */

rightrot(n,b)
unsigned n;
int b;
{
   unsigned work;
   int i, j;

   printf("\n\nRotating %u right by %d bits\n",n,b);

   binary(n);

   for (i=1; i<=b; i++)
   {
        work = n & 1;
        work <<= (len - 1);
        n >>= 1;
        n |= work;
   }

   printf("\nResult:  %u\n",n);
   binary(n);
   return(0);
}       /* end of rightrot() */

/* ---------------------------------------------------------------- */

binary(v)         /* displays v as a binary number */
unsigned v;
{
   unsigned mask;
   int i;
   char stak[128];

   mask = 001;
   for (i=0; i<len; i++)
   {
        stak[i] = '0';
        if ((v & mask) == 1) stak[i] = '1';
        v >>= 1;
   }

   printf("Binary representation is:  ");
   for (i=len-1; i>=0; --i)
        putchar(stak[i]);
   return(0);
}       /* end of binary() */

/* ---------------------------------------------------------------- */

invert(x,p,n)          /* inverts n bits of x beginnint at p */
unsigned x;
int n,p;
{
   unsigned work;
   int i,j,nr;

   printf("\n\nBeginning value = %u\n",x);
   binary(x);
   printf("\nInverting %d bits starting at %d\n",n,p);
   for (i=0; i<p; i++)
   {
        work = x & 1;
        work <<= (len - 1);
        x >>= 1;
        x |= work;
   }

   for (i = 1; i<=n; i++)
   {
        j = x & 1;
        if (j == 0)  j = 1;
        else j = 0;
        j <<= (len - 1);
        x >>= 1;
        x |= j;
   }

   for (i=1; i<=len-(n+p); i++)
   {
        work = x & 1;
        work <<= (len-1);
        x >>= 1;
        x |= work;
   }

   printf("The new value is %u\n",x);
   binary(x);
   return(0);
}       /* end of invert() */

Synfire

  • Guest
Re: Bit Manipulations (Standard C code)
« Reply #1 on: October 23, 2007, 08:11:07 AM »
Fixed it to work with PellesC for ya.

Code: [Select]
#include <stdio.h>

int wordlength( void );
int rightrot( unsigned, int );
int binary( unsigned );
int invert( unsigned, int, int );

int len;
unsigned max_value;

void main( void )
{
   unsigned uarg1;
   int iarg1, iarg2;

   printf("\nCalculating word length ...");
   wordlength();
   printf("\nWord length of this machine is %d bits\n",len);
   printf("Maximum value which can be stored ");
   printf("in one word is %u\n",max_value);

   printf("\n\nRotating values ...");
   uarg1 = 124;
   iarg1 = 3;
   rightrot(uarg1,iarg1);
   uarg1 = 336;
   iarg1 = 4;
   rightrot(uarg1,iarg1);
   uarg1 = 747;
   iarg1 = 1;
   rightrot(uarg1,iarg1);

   printf("\n\n\nInverting bits ... ");
   uarg1 = 1024;
   iarg1 = 5;
   iarg2 = 3;
   invert(uarg1,iarg1,iarg2);
   uarg1 = 577;
   iarg1 = 7;
   iarg2 = 5;
   invert(uarg1,iarg1,iarg2);
   uarg1 = 322;
   iarg1 = 6;
   iarg2 = 4;
   invert(uarg1,iarg1,iarg2);

   printf("\n\n\nNORMAL PROGRAM TERMINATION\n\n");
}       /* end of main() */

/* ----------------------------------------------------------------- */

int wordlength( void )           /* calculates word length & max value */
{
   unsigned int value;
   int j;

   value = ~0;

   while (((value >>= 1) ^ 0) != 0)
        ++len;
   ++len;

   max_value = 1;
   for (j=(len-1); j>=0; --j)
        max_value = max_value * 2;
   max_value = max_value - 1;

   return(len,max_value);
}       /* end of wordlength() */

/* ----------------------------------------------------------------- */

int rightrot( unsigned n, int b )
{
   unsigned work;
   int i;

   printf("\n\nRotating %u right by %d bits\n",n,b);

   binary(n);

   for (i=1; i<=b; i++)
   {
        work = n & 1;
        work <<= (len - 1);
        n >>= 1;
        n |= work;
   }

   printf("\nResult:  %u\n",n);
   binary(n);
   return(0);
}       /* end of rightrot() */

/* ---------------------------------------------------------------- */

int binary( unsigned v )         /* displays v as a binary number */
{
   unsigned mask;
   int i;
   char stak[128];

   mask = 001;
   for (i=0; i<len; i++)
   {
        stak[i] = '0';
        if ((v & mask) == 1) stak[i] = '1';
        v >>= 1;
   }

   printf("Binary representation is:  ");
   for (i=len-1; i>=0; --i)
        putchar(stak[i]);
   return(0);
}       /* end of binary() */

/* ---------------------------------------------------------------- */

int invert( unsigned x, int n, int p )          /* inverts n bits of x beginning at p */
{
   unsigned work;
   int i,j;

   printf("\n\nBeginning value = %u\n",x);
   binary(x);
   printf("\nInverting %d bits starting at %d\n",n,p);
   for (i=0; i<p; i++)
   {
        work = x & 1;
        work <<= (len - 1);
        x >>= 1;
        x |= work;
   }

   for (i = 1; i<=n; i++)
   {
        j = x & 1;
        if (j == 0)  j = 1;
        else j = 0;
        j <<= (len - 1);
        x >>= 1;
        x |= j;
   }

   for (i=1; i<=len-(n+p); i++)
   {
        work = x & 1;
        work <<= (len-1);
        x >>= 1;
        x |= work;
   }

   printf("The new value is %u\n",x);
   binary(x);
   return(0);
}       /* end of invert() */


tbohon

  • Guest
Re: Bit Manipulations (Standard C code)
« Reply #2 on: October 24, 2007, 06:13:11 PM »
Cool!  I was going to do that myself but am recovering from eye surgery (not supposed to be on the computer even today, don't tell anyone ...  ;) ) so haven't had a chance.

Tom