Can you please tell me the algorithm for aligning the length of the array by 4.

For example, I have an array of 7 bytes. Therefore, I need to supplement this array with another zero byte so that the length becomes equal to 8.

I myself can’t figure out how to calculate it.

  • which means there is an array of 7 bytes. Therefore, I need to supplement this array with another zero byte ? - Grundy
  • @ badc0de32 I didn’t quite understand what was required. It is necessary that the length of the array was always a multiple of 4m? - V. Dmitriy

1 answer 1

It is necessary to produce the following bit operations over the length

(Length + 3) & !3 

In general, the following formula is used to align m

 (Length + (m - 1)) & !(m - 1) 
  • @Grundy as they understand each other? - V. Dmitriy
  • 2
    @ V.Dmitriy is elementary. "Alignment by 2 ^ n" is a typical task and there is nothing more to say besides its wording. - Vladimir Martyanov
  • @ Vladimir Martiyanov in what cases is this alignment used? - V. Dmitriy
  • one
    @ V.Dmitriy MZPE file loader (that is, almost all executables in Windows) constantly does these things. Only there everything is harder in the end. - Vladimir Martyanov
  • one
    @ V.Dmitriy work with page memory, work with bitmaps ... - Anton Shchyrov