Discrete Mathematics University of Kentucky CS 275 Spring ... - MGNet
Discrete Mathematics University of Kentucky CS 275 Spring ... - MGNet
Discrete Mathematics University of Kentucky CS 275 Spring ... - MGNet
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Algorithm: Addition <strong>of</strong> integers<br />
procedure add(a, b: integers)<br />
(a n-1 a n-2 …a 1 a 0 ) 2 := base_2_expansion(a)<br />
(b n-1 b n-2 …b 1 b 0 ) 2 := base_2_expansion(b)<br />
c := 0<br />
for j := 0 to n-1<br />
d := 7(a j +b j +c)/28<br />
s j := a j +b j +c – 2d<br />
c := d<br />
s n := c<br />
{the binary expansion <strong>of</strong> the sum is (s k-1 s k-2 …s 1 s 0 ) 2 }<br />
Questions:<br />
• What is the complexity <strong>of</strong> this algorithm?<br />
• Is this the fastest way to compute the sum?<br />
57<br />
Algorithm: Mutiplication <strong>of</strong> integers<br />
procedure multiply(a, b: integers)<br />
(a n-1 a n-2 …a 1 a 0 ) 2 := base_2_expansion(a)<br />
(b n-1 b n-2 …b 1 b 0 ) 2 := base_2_expansion(b)<br />
for j := 0 to n-1<br />
if b j = 1 then c j := a shifted j places else c j := 0<br />
{c 0 ,c 1 ,…,c n-1 are the partial products}<br />
p := 0<br />
for j := 0 to n-1<br />
p := p + c j<br />
{p is the value <strong>of</strong> ab}<br />
Examples:<br />
• (10) 2 $(11) 2 = (110) 2 . Note that there are more bits than the original integers.<br />
• (11) 2 $(11) 2 = (1001) 2 . Twice as many binary digits!<br />
58