Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

BitOp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           BitOp.cpp  -  description
00003                              -------------------
00004     begin                : Thu Sep 6 2001
00005     copyright            : (C) 2001 by 
00006     email                : 
00007  ***************************************************************************/
00008 #include <stdio.h>
00009 namespace rwil {
00010   namespace BitOp {
00011 
00012     void SetBit(char *data, int bit, int val)
00013     {
00014       int byte = bit/8;
00015       int loc = 7-bit%8;
00016       data[byte] = data[byte] & (~(1 << loc)) | (val << loc);
00017     }
00018         
00019     void SetBits(char *data, int startbit, int numbits, int val)
00020     {
00021       int count;
00022       for(count=0;count<numbits;count++)
00023         {
00024           //            printf("%d=", val & (1 << count));
00025           SetBit(data, startbit+numbits- 1 - count, val & (1 << count)?1:0);
00026         }
00027       //        printf("\n");
00028     }
00029 
00030     int GetBit(char *data, int bit)
00031     {
00032       return data[bit/8] & (1 << (7-bit%8))?1:0;
00033     }
00034 
00035     int GetBits(char *data, int startbit, int numbits)
00036     {
00037       int count;
00038       int retval = 0;
00039       for(count=0;count<numbits;count++)
00040         {
00041           retval += GetBit(data, startbit + numbits - count - 1) << count;
00042         }
00043       return retval;
00044     }
00045 
00046     void PrintBits(char *data, int bits)
00047     {
00048       int count;
00049       for(count=0;count<bits;count++)
00050         {
00051           if(!(count%8)) printf("-");
00052           printf("%d", data[count/8] & (1 << (7-count%8))?1:0);
00053         }
00054       printf("\n");
00055     }
00056   }
00057 }

Generated on Tue Apr 9 13:53:07 2002 for RWIL - Real World Interface Library by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001