<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: bitwise code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:27:00 GMT</pubDate>
    <description>DZone Snippets: bitwise code</description>
    <item>
      <title>Bit field class</title>
      <link>http://snippets.dzone.com/posts/show/5495</link>
      <description>// A bit field is an important data structure in computer science, major uses include memory allocators and Bloom filters.&lt;br /&gt;// The code is in the form of a header file and an implementation file.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;bitfield.h&gt;&lt;br /&gt;// This class is a code snippet, it can be freely used and distributed.&lt;br /&gt;// Author: irfan[dot]hamid[at]gmail[dot]com&lt;br /&gt;//&lt;br /&gt;// Bit fields are a widely used mechanism in computing applications.&lt;br /&gt;// Typical applications include memory allocators and Bloom filters. This class&lt;br /&gt;// provides a generic bit field implementation for an arbitrary number of bits.&lt;br /&gt;//&lt;br /&gt;// Implementation notes:&lt;br /&gt;// The use of C++ allows the clean separation of the data structure from the&lt;br /&gt;// interface. The bit field is implemented as a vector of unsigned int that is&lt;br /&gt;// allocated at object creation.&lt;br /&gt;//&lt;br /&gt;// field: The vector that represents the bit field itself&lt;br /&gt;// bit_count: The total number of bits in the bit field&lt;br /&gt;&lt;br /&gt;#ifndef __BITFIELD_H__&lt;br /&gt;#define __BITFIELD_H__&lt;br /&gt;#include &lt;math.h&gt;&lt;br /&gt;#define SZ_UINT sizeof(unsigned int)&lt;br /&gt;&lt;br /&gt;class Bitfield&lt;br /&gt;{&lt;br /&gt;protected:&lt;br /&gt;	unsigned int *field;&lt;br /&gt;	unsigned int bit_count;&lt;br /&gt;&lt;br /&gt;public:&lt;br /&gt;	Bitfield (int bc);&lt;br /&gt;	~Bitfield ();&lt;br /&gt;&lt;br /&gt;	int set (int bit);&lt;br /&gt;	int get (int bit);&lt;br /&gt;	int reset (int bit);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;#endif&lt;br /&gt;&lt;/bitfield.h&gt;&lt;br /&gt;&lt;br /&gt;&lt;bitfield.cpp&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include "bitfield.h"&lt;br /&gt;&lt;br /&gt;// The constructor takes an int param which gives the number of bits in this&lt;br /&gt;// bit field.&lt;br /&gt;Bitfield::Bitfield (int bc)&lt;br /&gt;{&lt;br /&gt;	bit_count = bc;&lt;br /&gt;&lt;br /&gt;	// E.g, ceil(257/32*8) = 65, 64 ints fully used, last one partially used&lt;br /&gt;	field = (unsigned int*) malloc(((int) ceil(bc/SZ_UINT*8)));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Bitfield::~Bitfield ()&lt;br /&gt;{&lt;br /&gt;	if (field)&lt;br /&gt;		free(field);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// This function sets the corresponding bit in the field equal to 1.&lt;br /&gt;//&lt;br /&gt;// Returns: 0 on success, -1 on error.&lt;br /&gt;int Bitfield::set (int bit)&lt;br /&gt;{&lt;br /&gt;	// Sanity check&lt;br /&gt;	if (bit &gt;= bit_count || !field)&lt;br /&gt;		return -1;&lt;br /&gt;&lt;br /&gt;	// The correct index into the vector will be given by bit/SZ_UNIT. The&lt;br /&gt;	// index into the correct vector element is given by bit%SZ_UNIT, to&lt;br /&gt;	// achieve this, simply left shift 0x01 the appropriate number of times.&lt;br /&gt;	field[bit/SZ_UINT] |= (0x00000001 &lt;&lt; (bit%(SZ_UINT*8)-1));&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// This function sets the corresponding bit in the field equal to 0.&lt;br /&gt;//&lt;br /&gt;// Returns: 0 on success, -1 on error.&lt;br /&gt;int Bitfield::reset (int bit)&lt;br /&gt;{&lt;br /&gt;	if (bit &gt;= bit_count || !field)&lt;br /&gt;		return -1;&lt;br /&gt;	field[bit/SZ_UINT] &amp;= ~(0x00000001 &lt;&lt; (bit%(SZ_UINT*8)-1));&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// This function returns the value of the corresponding bit.&lt;br /&gt;//&lt;br /&gt;// Returns: The value of the bit, if the field is initialized and bit index is&lt;br /&gt;// within bounds, -1 otherwise.&lt;br /&gt;int Bitfield::get (int bit)&lt;br /&gt;{&lt;br /&gt;	if (bit &gt;= bit_count || !field)&lt;br /&gt;		return -1;&lt;br /&gt;	return (field[bit/SZ_UINT] &amp; (0x00000001 &lt;&lt; (bit%(SZ_UINT*8)-1)) ? 1 : 0);&lt;br /&gt;}&lt;br /&gt;&lt;/bitfield.cpp&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 May 2008 11:48:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5495</guid>
      <author>phaana (Irfan Hamid)</author>
    </item>
    <item>
      <title>Bitwise Operation: Quickly Convert Binary Bit to Integer and vice versa using Javascript Shell</title>
      <link>http://snippets.dzone.com/posts/show/4740</link>
      <description>using JavaScript shell found at www.squarefree.com/shell/ , it's very easy to convert binary bit to integer and vice versa&lt;br /&gt;&lt;br /&gt;convert integer to binary bit string&lt;br /&gt;&lt;code&gt;&lt;br /&gt;num = 1024&lt;br /&gt;num.toString(2)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;convert binary bit string to integer&lt;br /&gt;&lt;code&gt;&lt;br /&gt;parseInt('111', 2)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 Nov 2007 05:56:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4740</guid>
      <author>agilekai (kai)</author>
    </item>
    <item>
      <title>Count number of set bits in an integer</title>
      <link>http://snippets.dzone.com/posts/show/4233</link>
      <description>&lt;code&gt;&lt;br /&gt;count = 0&lt;br /&gt;count += byte &amp; 1 and byte &gt;&gt;= 1 until byte == 0&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Jul 2007 01:12:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4233</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
