2 Replies - 2023 Views - Last Post: 12 February 2012 - 06:57 PM

#1 phaana   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 08

Flexible bit field class

Posted 14 May 2008 - 03:23 PM

Description: This class implements a bit field with an arbitrary number of bits and a standard set/reset/get interface.
<bitfield.h>
/******************************************************************************
 *
 * Bit fields are a very widely used mechanism in computing applications.
 * Typical applications include memory allocators and Bloom filters. This class
 * provides a generic bit field implementation for an arbitrary number of bits.
 *
 * Implementation notes:
 * The use of C++ allows the clean separation of the data structure from the
 * interface. The bit field is implemented as a vector of unsigned int that is
 * allocated at object creation.
 *
 * field: The vector that represents the bit field itself
 * bit_count: The total number of bits in the bit field
 *
 * ***************************************************************************/

#ifndef __BITFIELD_H__
#include <math.h>
#define SZ_UINT sizeof(unsigned int)

class Bitfield
{
protected:
	unsigned int *field;
	unsigned int bit_count;

public:
	Bitfield (int bc);
	~Bitfield ();

	int set (int bit);
	int get (int bit);
	int reset (int bit);
};

#endif /* __BITFIELD_H__ */
</bitfield.h>

<bitfield.cpp>
#include <stdlib.h>
#include "bitfield.h"

/******************************************************************************
 *
 * The constructor takes an int param which gives the number of bits in this
 * bit field.
 *
 * ***************************************************************************/
Bitfield::Bitfield (int bc)
{
	bit_count = bc;

	/* E.g, ceil(257/32*8) = 65, 64 ints fully used, last one partially used */
	field = (unsigned int*) malloc(((int) ceil(bc/SZ_UINT*8)));
}

Bitfield::~Bitfield ()
{
	if (field)
		free(field);
}

/******************************************************************************
 *
 * This function sets the corresponding bit in the field equal to 1.
 *
 * Returns: 0 on success, -1 on error.
 *
 * ***************************************************************************/
int Bitfield::set (int bit)
{
	/* Sanity check */
	if (bit >= bit_count || !field)
		return -1;

	/* The correct index into the vector will be given by bit/SZ_UNIT. The
     * index into the correct vector element is given by bit%SZ_UNIT, to
     * achieve this, simply left shift 0x01 the appropriate number of times. */
	field[bit/SZ_UINT] |= (0x00000001 << (bit%(SZ_UINT*8)-1));
	return 0;
}

/******************************************************************************
 *
 * This function sets the corresponding bit in the field equal to 0.
 *
 * Returns: 0 on success, -1 on error.
 * 
 * ***************************************************************************/
int Bitfield::reset (int bit)
{
	if (bit >= bit_count || !field)
		return -1;
	field[bit/SZ_UINT] &= ~(0x00000001 << (bit%(SZ_UINT*8)-1));
	return 0;
}

/******************************************************************************
 *
 * This function returns the value of the corresponding bit.
 *
 * Returns: The value of the bit, if the field is initialized and bit index is
 * within bounds, -1 otherwise.
 *
 * ***************************************************************************/
int Bitfield::get (int bit)
{
	if (bit >= bit_count || !field)
		return -1;
	return (field[bit/SZ_UINT] & (0x00000001 << (bit%(SZ_UINT*8)-1)) ? 1 : 0);
}
</bitfield.cpp>


Is This A Good Question/Topic? 0
  • +

Replies To: Flexible bit field class

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Flexible bit field class

Posted 11 February 2012 - 03:10 AM

For cpp consider std::bitfield
Was This Post Helpful? 0
  • +
  • -

#3 phaana   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 14-May 08

Re: Flexible bit field class

Posted 12 February 2012 - 06:57 PM

True enough. I was just practicing some bit manipulation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1