Questions on OOP in MQL5 - page 93

 
Igor Makanu:

another question... Is there any way to get a 12 bit int via union ?... never mind how ))))

SZY: 9-15 bit int ideally, sign to allocate not a problem, i.e. it is possible unsigned int (in last builds of MQL learnedunsigned int, it is possible instead of uint to use )

I don't understand it ... help!)))

I figured out that I can allocate some needed bits this way:

//+------------------------------------------------------------------+
uint ConvertUintToXBits(const uint value, const int pos, const int cnt_bits)
{
   uint mask = (0xFFFFFFFF >> (sizeof(uint) - cnt_bits)) << pos;  // получили битовую маску
   uint result = value & mask;                                    // выделили биты числа
   return(result >> pos);                                         // сдвинули чтобы привести к 32-битам
}
//+------------------------------------------------------------------+

where the most significant bit would be now , to determine the sign of the number... don't know, i.e. ideally it should be a function

int ConvertUintToXBits(const uint value, const int pos, const int cnt_bits)

ideally it should be a template to use such structures at all:

struct Int12
{
private:
   const int bits;
public:
   Int12(const uint IntValue): bits(12) {ConvertUintToXBits.......}
   int value;
};
 
Igor Makanu:

So again I don't get it...help! help!)))

I figured out that I can allocate some of the bits I need:

where the highest bit would be now to determine the sign of the number... don't know, i.e. ideally it should be a function

ideally it should be a template to use such structures at all:

See. The mask is the significant part of the number, i.e., for int, you have to shift a bit further, since the high bit you have the sign means. <<pos is a mess. As in your case, you will take high bits of int by this mask, while you need low bits))).

For signed integers, you must separately take significant part, separately take high bit (sign), then shift high bit to needed position and use bitwise OR to combine them. Example for int-> 4 bits

int mean4=0xFFFFFFFF>>29

int sign16=1<<31

int sign4=1<3

Now int->int4 watch your hands:

1. we have int val

2. int sign=(val&sign16)>>28.

Here we have taken the sign of our number and shifted it to the right position (the most significant bit in int4)

3. mean3val=val&mean4

Here we have got the three least significant bits (meaningful part) through a mask, the rest is clipped.

4. int out=sign|mean3val

And here we have combined the sign with the significant part. Now we have 4 low-order bits, in which bit 4 is the sign, and bits 1 to 3 are the significant part, all bits above 4 have a value of 0. The range of values is -8...7 And yes, if 8 is fed in, there will be no 7 and it will be 0, as everything will just cut off.

 

It's easier to shift by the right number of bits and use 1 for the mask. At least it is more obvious.

Sign definition: (a>>31)&1 - if 1, then minus, if 0, then plus.

 

I can make significant bits out of int with a "head-on" solution, but I can't make a universal solution... I can't! (((

It is highly probable that I have not explained how and where I want to go

there are 4 bytes int / uint, and there is my dataset, which I want to unpack in the optimizer

let's assume I succeeded in packing it this way:

0-8 9-16 17-24 25-32
int_11 int_9 int_12

what types I need int_XXX, I have not yet estimated

but I want to have usability in the form of this code

input int param1 = 0xFFFFFFF;
int MyParam_1 = ConVertUintToIntXXX((uint)param1, 0, 11);
int MyParam_2 = ConVertUintToIntXXX((uint)param1, 12, 9);
int MyParam_3 = ConVertUintToIntXXX((uint)param1, 21,12);
 

Has no one found my error yet? ))))

Tip: uint all masks should be)))

 
Igor Makanu:

I can make significant bits out of int with a "head-on" solution, but I can't make a universal solution... I can't! (((

It is highly probable that I have not explained how and where I want to go

there are 4 bytes int / uint, and there is my dataset, which I want to unpack in the optimizer

let's assume I succeeded in packing it this way:

0-8 9-16 17-24 25-32
int_11 int_9 int_12

what types I need int_XXX, I have not yet estimated

but I want to have a usability in the form of the following code

int add(int o,int l,int v,int r){
   int m=(int)MathPow(2,l)-1;
   r=~r;   
   r=r|(m<<o);
   r=~r;
   r=r|(v<<o);
   return(r);
}

int extract(int o,int l,int r){
   int m=(int)MathPow(2,l)-1;
   return((r>>o)&m);
}

o - indent (bits), l - length (number of bits), value to add, r - where to add

But I haven't tested it.

 
Dmitry Fedoseev:

Because you are not overriding a virtual function, you are using overloading.


There's clearly something wrong here

 
Alexandr Andreev:

There's obviously something wrong here.

Make the method a template and access it

class A {
template<typename T>
void f( T ) {}
};
void OnStart()
{
        A а;
        а.f(0); //нормально
}
 
A100:

Make the method a template and access it

That's a laifhack!!!

ps: +1 way to write something that will just stop working with the update when they tweak that loafhack

 
Alexandr Andreev:

That's a loophole!!!

ps: +1 way to write something that will just stop working with the update when they tweak this loafhack

class A {
public:
void f( int ) {}
};
class B : public A {
public:
template<typename T>
void f( T ) { ffffffffffff(1); пишу что хочу %<>!!!<>%  } //нормально
};
The battle over nanoseconds looks very strange in the face of such gaps