Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by azzick ( 16 years ago )
#include "Word.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <limits.h>
int BitsToBytes(unsigned short Bits)
{
return ldiv(Bits+sizeof(WordBase)*8-1, sizeof(WordBase)*8).quot;
}
unsigned short Min(unsigned short a, unsigned short b)
{
if (a<b)
return a;
return b;
}
Word::Word(unsigned short WordSize)
{
Size = WordSize;
Data = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
}
Word::Word(const Word& W)
{
Size = W.Size;
Data = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
memcpy(Data, W.Data, BitsToBytes(Size));
}
Word::~Word()
{
free(Data);
Data = 0;
}
int Word::Set(const WordBase* NewData)
{
memcpy(Data, NewData, BitsToBytes(Size));
return 0;
}
/*
int Word::Set(bool *NewData)
{
return 0;
}
*/
int Word::SetSize(unsigned short NewSize)
{
//Size = NewSize;
WordBase* buf;
buf = (WordBase*)calloc(BitsToBytes(NewSize), sizeof(WordBase));
memcpy(buf, Data, Min(BitsToBytes(NewSize), BitsToBytes(Size)));
free(Data);
Data = buf;
Size = NewSize;
return 0;
}
unsigned short Word::GetSize()
{
return Size;
}
//Внимание, использование констант
int Word::SetCell(unsigned short Pos, bool Cell)
{
if (Size == 0)
return -1;
Pos = Pos % Size;
unsigned short Byte = Pos/(sizeof(WordBase)*8);
unsigned short Bit = Pos % (sizeof(WordBase)*8);
if (Cell)
Data[Byte] = Data[Byte] | (One << Bit);
else
Data[Byte] = Data[Byte] & ~(One << Pos);
return 0;
}
int Word::FastSetCell(bool Cell)
{
if (Size > 0)
{
if (Cell)
Data[0] = Data[0] | One;
else
Data[0] = Data[0] & ~One;
return 0;
}
return -1;
}
//Внимание, использование констант
bool Word::GetCell(unsigned short Pos)
{
Pos = Pos % Size;
unsigned short Byte = Pos/(sizeof(WordBase)*8);
unsigned short Bit = Pos % (sizeof(WordBase)*8);
if (((Data[Byte] >> Bit) & One) == One)
return true;
return false;
}
bool Word::FastGetCell()
{
if (Size > 0)
return (bool)(Data[0] % 2);
return 0;
}
void Word::CopyFrom(const Word& W)
{
free(Data);
Data = 0;
Data = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
memcpy(Data, W.Data, Min(BitsToBytes(Size), BitsToBytes(W.Size)));
}
void Word::ExtraMemClean()
{
Data[BitsToBytes(Size)-1] = ((WordBase)( Data[BitsToBytes(Size)-1] << (Size % (sizeof(WordBase)*8)) )) >> (Size % (sizeof(WordBase)*8));
}
//Внимание, использование констант
void Word::Print()
{
WordBase* buf;
buf = (WordBase*)malloc(sizeof(WordBase));
for (int i=BitsToBytes(Size)-1; i>=0; i--)
{
*buf = Data[i];
for (unsigned int j=0; j<sizeof(WordBase)*8; j++)
{
if (*buf >> (sizeof(WordBase)*8-1) == One)
std::cout << '1';
else
std::cout << '0';
*buf = *buf << 1;
}
}
std::cout << '\n';
}
Word& Word::operator=(const Word& W)
{
if (this == &W)
return *this;
free(Data);
Data = 0;
Size = W.Size;
Data = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
memcpy(Data, W.Data, BitsToBytes(Size));
return *this;
}
//циклический сдвиг слова влево на указанное число бит
Word& Word::operator <<(unsigned short Offset)
{
/*
if (Offset <0)
return *this >> -Offset;
Offset = Offset % Size;
Word *Result = new Word(*this);
unsigned short Byte = Offset/(sizeof(WordBase)*8);
unsigned short Bit = Offset % (sizeof(WordBase)*8);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = (Data[i] << Bit) | (Data[(i-1 +BitsToBytes(Size)) % (BitsToBytes(Size))] >> (sizeof(WordBase)*8-Bit));
WordBase* buf = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
for (int i=0; i<BitsToBytes(Size); i++)
buf[i] = Result->Data[(i-Byte+BitsToBytes(Size)) % BitsToBytes(Size)];
free(Result->Data);
Result->Data = buf;
return *Result;
*/
Offset = Offset % Size;
Word *Result = new Word(Size);
for (int i=0; i<Size; i++)
Result->SetCell(i, GetCell((i-Offset+Size) % Size));
return *Result;
}
//циклический сдвиг слова вправо на указанное число бит
Word& Word::operator >>(int Offset)
{
/*
if (Offset <0)
return *this << -Offset;
Offset = Offset % Size;
Word *Result = new Word(*this);
unsigned short Byte = Offset/(sizeof(WordBase)*8);
unsigned short Bit = Offset % (sizeof(WordBase)*8);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = (Data[i] >> Bit) | (Data[(i+1) % (BitsToBytes(Size))] << (sizeof(WordBase)*8-Bit));
WordBase* buf = (WordBase*)calloc(BitsToBytes(Size), sizeof(WordBase));
for (int i=0; i<BitsToBytes(Size); i++)
buf[i] = Result->Data[(i+Byte) % BitsToBytes(Size)];
free(Result->Data);
Result->Data = buf;
return *Result;
*/
if (Offset <0)
return *this >> -Offset;
Offset = Offset % Size;
Word *Result = new Word(Size);
for (int i=0; i<Size; i++)
Result->SetCell(i, GetCell((i+Offset) % Size));
return *Result;
}
//поразрядное слово И слово
Word& Word::operator &(const Word &W)
{
Word *Result = new Word(Min(this->Size, W.Size));
for (int i=0; i<BitsToBytes(Result->Size); i++)
{
Result->Data[i] = this->Data[i] & W.Data[i];
}
return *Result;
}
//поразрядное слово ИЛИ слово
Word& Word::operator |(const Word &W)
{
Word *Result = new Word(Min(this->Size, W.Size));
for (int i=0; i<BitsToBytes(Result->Size); i++)
{
Result->Data[i] = this->Data[i] | W.Data[i];
}
return *Result;
}
//поразрядное слово И bool
Word& Word::operator &(bool B)
{
Word *Result;
if (B)
Result = new Word(*this);
else
{
Result = new Word(this->Size);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = 0;
}
return *Result;
}
//поразрядное слово ИЛИ bool
Word& Word::operator |(bool B)
{
Word *Result;
if (B)
{
Result = new Word(this->Size);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = Unsigned_Max;
}
else
{
Result = new Word(*this);
}
return *Result;
}
Word& Word::operator ~()
{
Word *Result = new Word(*this);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = ~Result->Data[i];
return *Result;
}
Word& Word::operator ^(bool B)
{
Word *Result;
if (B)
{
Result = new Word(Size);
for (int i=0; i<BitsToBytes(Size); i++)
Result->Data[i] = ~Data[i];
}
else
Result = new Word(*this);
return *Result;
}
Word& Word::operator ^(const Word &W)
{
Word *Result = new Word(Min(this->Size, W.Size));
for (int i=0; i<BitsToBytes(Result->Size); i++)
{
Result->Data[i] = this->Data[i] ^ W.Data[i];
}
return *Result;
}
Revise this Paste