Path: utzoo!attcan!uunet!snorkelwacker!bloom-beacon!eru!luth!sunic!chalmers!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.lang.c++ Subject: Re: Overloading [] to read and write to a bit array Summary: Example of using [] to access bit array Keywords: bit, array, c++, [] Message-ID: <1029@appli.se> Date: 4 Jul 90 09:25:11 GMT References: <13053@shlump.nac.dec.com> Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 131 In-reply-to: heintze.peewee.enet.dec.com's message of 3 Jul 90 16:35:31 GMT In <13053@shlump.nac.dec.com> Sieg Heintze writes: >Can someone provide an example of overloading the [] operator to read as well >as write to a bit array? The trick is to implement Bit as a memory pointer and a bit offset and define "operator=" and "operator int()" on that class, then have BitArray "operator[]" return a Bit. The following code implements this scheme and works if compiled with G++ 1.37.1 under NCR Tower32 650 Sys V.3. The code have some questions commented-in, please mail if you have answers to them (or post if you think it's of public interest). // Example of Bit handling in memory buffers by Niklas Hallqvist 900704 // This program compiled and worked under G++ 1.37.1 #include #include class Bit { public: Bit(const int&, const int); // Is const int& what I mean? Bit(const Bit&); Bit& operator=(const int); operator int(); private: int& word; int offset; }; Bit::Bit(const int &wrd, const int offs) : word(wrd), offset(offs) { } Bit::Bit(const Bit& value) : word(value.word), offset(value.offset) { } Bit& Bit::operator=(const int value) { word &= ~(1 << offset); word |= ((value & 1) << offset); return *this; } int Bit::operator int() { return (word >> offset) & 1; } class BitArray { public: BitArray(const int); ~BitArray(); Bit operator[](const int); void print_internal_representation(void); protected: const int length; const int size; int* const buffer; }; const int bits_per_int = 8 * sizeof(int); BitArray::BitArray(const int sz) : size(sz), length((size + bits_per_int - 1) / bits_per_int), buffer(new int[length]) { for(int i = 0; i < length; i++) // Should this zeroing be necessary? My new buffer[i] = 0; // doesn't seem to return zeroed memory! } BitArray::~BitArray() { delete buffer; } // Should this read: delete [length] buffer? // When does this Bit get destroyed? We need it to live until we've used it // in the expression where the operator gets called. Bit BitArray::operator[](const int index) { return Bit(buffer[index / bits_per_int], index % bits_per_int); } void BitArray::print_internal_representation(void) { for(int i = 0; i < length; i++) cout << buffer[i] << ' '; cout << '\n'; } int main(void) { const int max = 12; BitArray a(max); int i; a.print_internal_representation(); for(i = 0; i < max; i += 2) { a[i] = 1; a.print_internal_representation(); } for(i = 0; i < max; i ++) cout << i << " " << a[i] << "\n"; return 0; } And here is a script of a session using this program (called bit): $ bit 0 1 5 21 85 341 1365 0 1 1 0 2 1 3 0 4 1 5 0 6 1 7 0 8 1 9 0 10 1 11 0 $ Please mail if you see two signatures below! --- Niklas Hallqvist Phone: +46-(0)31-19 14 85 Applitron Datasystem Fax: +46-(0)31-19 80 89 N. Gubberogatan 30 Email: niklas@appli.se S-416 63 GOTEBORG sunic!chalmers!appli!niklas Sweden