wtorek, 29 maja 2018

Bit operations. C++ Builder




__fastcall TForm1(TComponent* Owner);
bool __fastcall is_First(int value);
int first[50];
void __fastcall init_First();
UnicodeString bits[50];
UnicodeString negations[50];
UnicodeString lefts[50];
int neg_value[50];
int left_value[50];
UnicodeString __fastcall write_Bits(char value);
void __fastcall init_Bits();
int __fastcall write_Ints(wchar_t *value);
void __fastcall init_Ints();




  __fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
 ++sum;
if(sum==2)
return true;
else
return false;
}
void __fastcall TForm1::init_First()
{
int i,j;
i=0;
j=1;
do
{
 if(is_First(j))
 {
 first[i]=j;
 ++i;
 }
 ++j;

}while(i<50);
}
UnicodeString __fastcall TForm1::write_Bits(char value)
{
int i,j;
UnicodeString y="1";
UnicodeString n="0";
UnicodeString b_value="";
int bits_value[9]={1,2,4,8,16,32,64,128,256};
for(i=8;i>=0;i--)
{
j=(bits_value[i]&value);


if(j!=0)
b_value+=y;
else
b_value+=n;
}
return b_value;
}
void __fastcall TForm1::init_Bits()
{
int i;
for(i=0;i<50;i++)
{
bits[i]=write_Bits(char(first[i]));
negations[i]=write_Bits(char(~first[i]));
        lefts[i]=write_Bits(char(first[i]<<1));
}
}

int __fastcall TForm1::write_Ints(wchar_t *value)
{
   int int_value,i;
wchar_t y='1';
wchar_t n='0';
int_value=0;
   int bits_value[9]={256,128,64,32,16,8,4,2,1};
   for(i=8;i>=0;i--)
   {
if(value[i]==y)
int_value+=bits_value[i];
   }
   return int_value;
}
void __fastcall TForm1::init_Ints()
{
int i;
for(i=0;i<50;i++)
{
neg_value[i]=write_Ints(negations[i].w_str());
        left_value[i]=write_Ints(lefts[i].w_str());
}
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i;
 init_First();
 init_Bits();
 init_Ints();
 for(i=0;i<50;i++)
 {
ListBox1->Items->Add(IntToStr(i+1)+")  "+IntToStr(first[i]));
ListBox2->Items->Add(bits[i]);
ListBox3->Items->Add(negations[i]+"     "+IntToStr(neg_value[i]));
ListBox4->Items->Add(lefts[i]+"     "+IntToStr(left_value[i]));
 }
}

Brak komentarzy:

Prześlij komentarz