MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / binary_multiply

Function binary_multiply

maths/binary_multiplication.py:23–58  ·  view source on GitHub ↗

Multiply 'a' and 'b' using bitwise multiplication. Parameters: a (int): The first number. b (int): The second number. Returns: int: a * b Examples: >>> binary_multiply(2, 3) 6 >>> binary_multiply(5, 0) 0 >>> binary_multiply(3, 4) 12 >>> bin

(a: int, b: int)

Source from the content-addressed store, hash-verified

21
22
23def binary_multiply(a: int, b: int) -> int:
24 """
25 Multiply 'a' and 'b' using bitwise multiplication.
26
27 Parameters:
28 a (int): The first number.
29 b (int): The second number.
30
31 Returns:
32 int: a * b
33
34 Examples:
35 >>> binary_multiply(2, 3)
36 6
37 >>> binary_multiply(5, 0)
38 0
39 >>> binary_multiply(3, 4)
40 12
41 >>> binary_multiply(10, 5)
42 50
43 >>> binary_multiply(0, 5)
44 0
45 >>> binary_multiply(2, 1)
46 2
47 >>> binary_multiply(1, 10)
48 10
49 """
50 res = 0
51 while b > 0:
52 if b & 1:
53 res += a
54
55 a += a
56 b >>= 1
57
58 return res
59
60
61def binary_mod_multiply(a: int, b: int, modulus: int) -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected