Suppose there is a number 102 . How to find out how much it needs to be increased so that it becomes a multiple of 16 ?

    9 answers 9

    For the specific case of the multiplicity of the number 16, bit arithmetic can be used to take a module:

     int p = 102; int needAdd = -p & 0xf; 

    In general, you need a remainder (in the Euler sense, positive). Unfortunately, the % operator in C # is not very good for negative argument values, so we have to write our own:

     public static int Modulo(int p, int q) { q = Math.Abs(q); var result = p % q; if (result < 0) result += q; return result; } 

    Having the correct remainder, the task is trivial:

     int p = 102; int q = 16; int needAdd = Modulo(-p, q); 

    The code does not depend on the signs of the numbers p , q (if only q not zero).


    For purists: ( needAdd + p ) (- p + p ) 0 (mod q ), and by construction, needAdd ∈ [0, q ).

    • Perhaps the author still needs to increase the number, and not find out the remainder. - Kromster
    • 3
      @KromStern: p += needAdd; ? - VladD
    • Yes, this will be the answer to the question. - Kromster
    • @KromStern: Disagree. I quote the question: How do I know how much it needs to be increased .... - VladD
    • I agree. But the rest is not to blame. - Kromster

    The most banal way: we divide 102 by 16, we get the quotient (6) and the remainder (6). Check if the remainder is zero. If equal, then we found our number. If not, then we take the quotient, add a unit to it. Then multiply by 16 and get 16*7=112 . Now from the result (112), we subtract the initial number (102) and have that the initial number should be increased by 10.

    • 2
      why so long? the remainder is six, and 16 minus six will be 10. - aleksandr barakin
    • @alexanderbarakin, just the first thing that came to mind. Your decision is better, of course - ixSci
    • Explanatory explanation, just for a beginner, and without cleverness with bits and cycles. - Kromster
     am = (a + 0x0FL) & ~0x0FL; // блиТайшСС свСрху число, ΠΊΡ€Π°Ρ‚Π½ΠΎΠ΅ 16 d = am - a; // Ρ€Π°Π·Π½ΠΈΡ†Π° 
    • what is L ? - Sergiks
    • L - suffix to indicate long constants. - Outtruder
    • In which YaP such conversion is accepted? This is absent in php and js. - Sergiks
    • C / C ++. But it is not really necessary here, it is because of internal paranoia I used to insert it, so that the household constant in the expected (for me) way of the broken one expands to a long one. - Outtruder

    Let's look at the bits! :)

    For multiples of 16, the lower 4 bits are zeros:

      0: 0000 0000 16: 0001 0000 32: 0010 0000 

    To increase any number to a multiple of 16, you need to add "1" in the positions where there are now zeroes and another 1:

     11: 0000 1011 - Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‚Ρ€Π΅Ρ‚ΠΈΠΉ Π±ΠΈΡ‚ = "0" Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ 0100 - "1" для Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅Π³ΠΎ Π±ΠΈΡ‚Π° Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ 0001 - ΠΈ Π΅Ρ‰Ρ‘ +1, Ρ‡Ρ‚ΠΎΠ±Ρ‹ всС младшиС Π±ΠΈΡ‚Ρ‹ ΠΎΠ±Π½ΡƒΠ»ΠΈΠ»ΠΈΡΡŒ. ------------- Π½Π΅ Ρ…Π²Π°Ρ‚Π°Π»ΠΎ 0100 (это 4) ΠΈ Π΅Ρ‰Ρ‘ 1 = ΠΈΡ‚ΠΎΠ³ΠΎ +5 22: 0001 0110 - Π½ΡƒΠ»ΠΈ Π² ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΈ Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠΌ Π±ΠΈΡ‚Π°Ρ… Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ 1001 (это 9) Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ 0001 (Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ) --------------- сумма: 1010 (это Π΄Π΅ΡΡΡ‚ΡŒ) 

    Calculate the required increase can also be bitwise operations:

    1) from the initial number only the lower 4 bits are written. The bit operation "And" will leave only those bits that are = 1 in both operands:

      исходноС число: 0101 1100 маска ΠΌΠ». 4 Π±ΠΈΡ‚: 0000 1111 --------------------------- Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ «И»: 0000 1100 

    2) the low-order bits should be inverted with the β€œNOT” operation

     1100 «НЕ» ---- 0011 

    3) add one:

      0011 = 3 +0001 = 1 ---- 0100 = 4 

    This means that you need to add 0000 0100 (4) to the original number 0101 1100 (92) to get 96, which is probably a multiple of 16.

    • 2
      I am surprised that so many responded to such a simple question and so many detailed answers. It’s a pity, I can’t mark several answers as a solution at once - Fangog

    The next multiple of 16 is the following: - Add 16-1 - this is the number whose residue from dividing by 16 is -1. - Divide integrally by 16 - we get the result of dividing the original number by 16 rounded upwards - Multiply by 16 - we get a number not less than the initial one and a multiple of 16

     Math.floor((102+15) / 16) * 16 

    Well, as a function for any numbers:

     function calc(x, m) { return Math.floor((x+m-1) / m) * m; } 

    To find out how much to increase, you must subtract the original number from the resulting number.

      Pseudocode:

       исходноС_число = 102 ΠΊΡ€Π°Ρ‚Π½ΠΎΠ΅_число = 16 частноС = 6 Ссли остаток_ΠΎΡ‚_дСлСния(исходноС_число) == 0 Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ исходноС_число ΠΈΠ½Π°Ρ‡Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ (частноС + 1) * ΠΊΡ€Π°Ρ‚Π½ΠΎΠ΅_число - исходноС_число 

      C code:

       #include <stdio.h> int main() { int input_number = 29; int multiple = 16; int quotient = input_number / multiple; int answer = 0; if (input_number % multiple == 0) { answer = input_number; } else { answer = (quotient + 1) * multiple - input_number; } printf("%u\n", answer); return 0; } 

        I would do this: Divide this number by 16. I would drop the shot, add one, multiply by 16 and take away the number that was given. It looks like this: 102/16 = 6,375. +1 = 7. 7 * 16 = 112. 112-102 = 10. Answer >> 10

          with integer division (positive numbers) of the Π΄Π΅Π»ΠΈΠΌΠΎΠ³ΠΎ by the Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ , the частноС and the остаток .
          искомоС number will be the result of subtracting the остатка from the дСлитСля .

          1. If in the language used there is a функция or an ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ for obtaining the остатка of integer division, then:

           искомоС = Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ - функция ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅, Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ) искомоС = Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ - ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ) 

          2. if, in the language used, there is only a функция or an ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ for obtaining a частного from integer division, then:

           искомоС = Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ - ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ - Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ * функция ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅, Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ) ) искомоС = Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ - ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ - Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ * ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ) ) 

          3. and if there is neither one nor the other, but there is ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ (ordinary) division ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ and функция rounding функция , then:

           искомоС = Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ - ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ - Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ * функция ( Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ) ) 

          4. But if in the language used there is neither one nor the other, then one can get by with subtraction and comparison. algorithm in abstract language:

           Ссли Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ > 0 ΠΈ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ > 0 число = Π΄Π΅Π»ΠΈΠΌΠΎΠ΅ ΠΏΠΎΠΊΠ° число >= 0 число = число - Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ΠΊΠΎΠ½Π΅Ρ† Ρ†ΠΈΠΊΠ»Π° искомоС = -число ΠΊΠΎΠ½Π΅Ρ† условия 
          • This will not work for all languages, since different languages ​​have different ideas about what % for negative numbers. (See, for example, the table here .) - VladD
          • Thanks, added reservation. - aleksandr barakin
           n = 102; m = n%16 ? 16-n%16 : 0; 
          • The minus is not mine, but: your code does not work for negative n , and modulo this is no different from my answer. - VladD
          • @VladD Thank you for the informative comment. - Yuri Negometyanov