Re: Bitfields and sprintbit()

From: Jaco van Iterson (J.C.vanIterson@ET.TUDelft.NL)
Date: 12/08/95

If you're curious how I implemented the sprintbit code you can download
it at
the files are called: src_c3p8+bitfields.tar.gz and change_bitfields.tar.gz

Of course I didn't use if statements for each field, I acces the bits the
way they are stored in memory, by bit number instead of by name.

I hope the bitfield code is portable, I checked 2 systems myself
(Apollo DN3000 and SGI IRIX 5.3) and received no bug reports yet so
it probebly is.

The code you ask for is pretty small so I will include it here.
 * the function is invoked with:
 * sprintfield((char*)&rm->room_flags, room_bits, buf2);
 * ( you need to put (char*)& in front of the bitfield structure name )
char *sprintfield(char *field, char *names[], char *result)
  int nr;
  unsigned char bitmask;
  extern unsigned char bit0;

  *result = '\0';
  for (nr = 0; *names[nr] != '\n'; nr++) {
    bitmask = (bit0==1 ? bit0<<(nr%8) : bit0>>(nr%8));
    if (field[nr/8] & bitmask) {
      strcat(result, names[nr]);
      strcat(result, " ");
  if (!*result)
    strcat(result, "none");
  return result;



> This is to the person who said that they imp'd a working bitfield system,
> or anyone else who wishes to respond.  ;-)
> How have you dealt with the sprintbit() function in a bitfield system?
> At first thought, it seems that you would need an 'if' statement for
> each field, which could lead to an unwieldy sprintbit() function...
> As far as I can tell, there is no way to access the members of the structure 
> except by name.  
> Anyone have any ideas?
> Pink

This archive was generated by hypermail 2b30 : 12/07/00 PST