Main Page | Class List | File List | Class Members | File Members

bits.c

Go to the documentation of this file.
00001 /***********************************************************************
00002   bits.c
00003   BITWISE OPERATIONS, functions to manipulate bits.  Max Luttrell,
00004   3/17/96
00005 */
00006 
00007 #include "standard.h"
00008 
00009 byte getbit(byte b, int n)
00010 /*
00011   get value of bit in position n from byte b,
00012   where a byte has positions 12345678
00013 */
00014 {
00015   return (b >> (8-n)) & 1;
00016 }
00017 
00018 unsigned int getbitint(unsigned int b, int n)
00019 /*
00020   get value of bit in position n from unsigned int b,
00021   where an unsigned integer has positions 1234567890123456
00022 */
00023 {
00024   return (b >> (16-n)) & 1;
00025 }
00026 
00027 
00028 void setbit(byte *b, int n, byte v)
00029 /*
00030   set value of bit in position n of byte b to be the value v.
00031   a byte has positions 12345678
00032   input: pointer to the byte.  output: none.  side effect: changes
00033   the particular bit in the byte
00034 */
00035 {
00036   if (v != 0)
00037     *b = (*b | (128 >> (n-1))); /*turn the bit on*/
00038   else
00039     *b = (*b & (~(128 >> (n-1)))); /*turn the bit off*/
00040 }
00041 
00042 
00043 void setbitint(unsigned int *b, int n, byte v)
00044 /*
00045   set value of bit in position n of unsigned int b to be the value v.
00046 
00047                              MSB      1   LSB 
00048         a byte has positions 1234567890123456
00049   input: pointer to the unsigned int.  output: none.  side effect: 
00050         changes the particular bit in the byte
00051 */
00052 {
00053   if (v != 0)
00054     *b = (*b | (32768 >> (n-1))); /*turn the bit on*/
00055   else
00056     *b = (*b & (~(32768 >> (n-1)))); /*turn the bit off*/
00057 }
00058 
00059 void printbyte(byte b)
00060 /* prints a byte to std output */
00061 {
00062   int j;
00063   for(j=7;j>=0;j--)
00064     if ((int) b  & (1 << j))
00065       printf("1");
00066     else
00067       printf("0");
00068   printf("\n");
00069 }
00070 
00071 void printint(unsigned int b)
00072 /* prints an unsigned int to std output */
00073 {
00074   int j;
00075   for(j=15;j>=0;j--)
00076     if ((int) b  & (1 << j))
00077       printf("1");
00078     else
00079       printf("0");
00080   printf("\n");
00081 }
00082 
00083 
00084 int random_bit(void)
00085 /*
00086  * returns: either a 1 or a 0.  
00087  *
00088  * there is approximately an equal chance of each.
00089  */
00090 {
00091   static int random_counter = -1;
00092   static int r;
00093  
00094   if (random_counter == -1) {
00095     r = rand();
00096     random_counter = 30;
00097   }
00098   return ((r & (1 << random_counter--))?1:0);
00099 }
00100 
00101 void randomize_byte(byte *b)
00102 /*
00103  * sets the bits inside of byte b to random values
00104  */
00105 {
00106   int i;
00107 
00108   for (i=1;i<=8;i++)
00109     setbit(b,i,random_bit());
00110 }

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1