#include "b40.h"

b40::b40() {
	zero();
};

void b40::zero() {
	for (uint8_t i=0; i<5; ++i) a40[i]=0;
};
void b40::one() {
	for (uint8_t i=0; i<5; ++i) a40[i]=0xFF;
};
void b40::set(uint8_t pos, bool val) {
	if (val) a40[pos>>3] |= 1<<(pos & 0x07); 
	else a40[pos>>3] &= ~(1<<(pos & 0x07));
	
};
void b40::set(uint8_t pos) {
	a40[pos>>3] |= 1<<(pos & 0x07); 
};
void b40::reset(uint8_t pos) {
	a40[pos>>3] &= ~(1<<(pos & 0x07));
};
bool b40::val(uint8_t pos) {
	return a40[pos>>3] & (1<<(pos & 0x07)) ? 1 : 0 ;
};
// --------------------------------------------------------------------------------
void b40::copyFrom(b40 x) {
	for (uint8_t i=0; i<5; ++i) a40[i] = x.a40[i];
};
bool b40::isZero() {
	return 0== (a40[0] || a40[1] || a40[2] || a40[3] || a40[4]);
};
uint8_t b40::bits() {
	uint8_t retval=0;
	for (uint8_t i=0; i<5; ++i)
		for (uint8_t j=0; j<8; ++j)
			if (a40[i] & 1<<j) retval++;
	return retval;
};
void b40::print() {
	for (uint8_t i=0; i<5; ++i) {
		for (uint8_t j=0; j<8; ++j)
			Serial.print(a40[i] & 1<<j ? "1":"0");
		Serial.print(" ");
	};
	
};
void b40::ANDwith(b40 x) {
	for (uint8_t i=0; i<5; ++i) a40[i] &= x.a40[i];
};
