An efficient way to determine the minimum field size needed to store deviations in user input

Sorry for the clumsy title; I could not find a way to express what I am trying to do.

I get input from a user from several 32 bit integers. For example, a user may enter the following values ​​(shown in hexadecimal format for ease of explanation):

0x00001234
0x00005678
0x0000abcd

In this particular case, the first 2 bytes of each input are constant, and the last 2 bytes are variable. To be effective I could be stored 0x0000as a single constant and create a vector of values uint16_tfor the storage of the input variable ( 0x1234, 0x5678, 0xabcd).

Now let's say that the user enters the following:

0x00000234
0x56780000
0x00001000

uint32_t, , .


:

uint32_t myVal = 0;
myVal |= input1;
myVal |= input2;
// ...

"toggled" (.. 1) myVal. .

, . ?


Update:

.

, , , ( , , , ).

, A B . 128-, B 32- .

128- 32- , . .

B, 128- , A, 128- ( ).

+3
5

... std::numeric_limits<uint16_t>::max()? , uint32_t.


:

, . 32- 128- . , 128- , . :

uint32_t get_value( uint32_t v1, uint32_t v2, uint32_t v3, uint32_t v4)
{
  uint32_t temp = v1; 
  if ( temp - v2 != temp ) throw exception;
  temp += v2; if ( temp - v3 != temp ) throw exception;
  temp += v3; if ( temp - v4 != temp ) throw exception;
  temp = v4;
  return temp;
}

++ , , .

+1

128- , , 32 , bool reject_all = false. , (128-32 = 96) , reject_all = true, . reject_all, , .

+1

[0, (2^32)-1] uint32_t. , 2 , - , , , . , .

0

, , , , . , ( O (n), n - ).

, :

unsigned long long bitmask = 0uL;
std::size_t count = val.size();
for (std::size_t i = 0; i < count; ++i)
  bitmask |= val[i];

, /, /, , 32 . SSE, OpenMP.

, , 1 1 32, .

, . (, CUDA Nvidia OpenCL, Mac , OpenCL, OpenMP).

0

uint32_t ORVal = 0;
uint32_t ANDVal = 0xFFFFFFFF;

ORVal  |= input1;
ANDVal &= input1;
ORVal  |= input2;
ANDVal &= input2;
ORVal  |= input3;
ANDVal &= input3; // etc.

// At end of input...
mask = ORVal ^ ANDVal; 
// bit positions set to 0 were constant, bit positions set to 1 changed

ORVal 1, 1 0, ALL 0 . ANDVal 0, 0 1, ALL 1 .

1, ORVal ANDVal 1. 0, ORVal ANDVal 0. 0 1, ORVal 1 ANDVal 0, XOR , .

0
source

Source: https://habr.com/ru/post/1774012/


All Articles