Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Post top tier code

Name: Anonymous 2014-07-22 13:29

Can be yours or not, I just want to see something good.

Name: Anonymous 2014-07-30 5:15

Delivered, bitptr in C++. I didn't write the bitpack/unpack routines, I just implemented the concept of a bitpointer.
#include <climits> // CHAR_BIT
#include <cstdlib> // div, div_t

class bitref;

class bitptr {
friend class bitref;
public:
bitptr(void *memory = nullptr, int offset = 0);
bitptr& operator+=(int j);
bitptr& operator-=(int j);
bitptr& operator++();
bitptr& operator--();
bitptr operator++(int);
bitptr operator--(int);
bitref operator*() const;
bool operator==(const bitptr& rhs) const;
bitptr& operator=(void *rhs);

private:
unsigned char *p;
int i;
};

class bitref {
public:
bitref(const bitptr& p) : p(p.p), i(p.i) {};
bitref& operator=(bool b) {
if (b)
*p |= 1 << i;
else
*p &= ~(unsigned char)(1 << i);
return *this;
}
bool operator==(bool b) {
return b == (*p & (1 << i));
}
explicit operator bool() const { return *p & (1 << i); }
private:
unsigned char *p;
int i;
};

bitptr::bitptr(void *memory, int offset) {
div_t d;
p = static_cast<unsigned char *>(memory);
d = div(offset, CHAR_BIT);
p += d.quot;
i = d.rem;
}

bitptr& bitptr::operator+=(int j) {
div_t d;
d = div(j + i, CHAR_BIT);
p += d.quot;
i = d.rem;
return *this;
}

bitptr& bitptr::operator-=(int j) {
return *this += -j;
}

bitptr& bitptr::operator++() {
if (++i == CHAR_BIT) ++p, i = 0;
return *this;
}

bitptr& bitptr::operator--() {
if (--i == -1) --p, i = CHAR_BIT - 1;
return *this;
}

bitptr bitptr::operator++(int) {
bitptr val{ *this };
++*this;
return val;
}

bitptr bitptr::operator--(int) {
bitptr val{ *this };
--*this;
return val;
}

bitref bitptr::operator*(void) const {
return bitref(*this);
}

bool bitptr::operator==(const bitptr& q) const {
return p == q.p && i == q.i;
}

bitptr& bitptr::operator=(void *q) {
p = static_cast<unsigned char *>(q);
i = 0;
return *this;
}
// Example
int main(void) {
bitptr p, q;
char c = 0;
p = static_cast<void*>(&c);
q = p;
*p = *q;
q++;
*q = 1;
return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List