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
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.
- 2why 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.
- 2I 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