2

Two binary numbers can be represented in the usual "regular, redundant" representation (i.e. introduce another digit, say 2, to obtain a non-unique representation such that any two consecutive 2's have a zero in between), so that addition becomes carry-free. I have heard that the complexity is O(k), where k is the length of the shorter of the two numbers. But what is the algorithm itself? It doesn't seem to appear on the web anywhere. I know you can add 1 to such a representation in constant time so that the result maintains regularity. But I don't know how to generalize this.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
user616847
  • 23
  • 2
  • Padding every other digits to absorb the carries .. this is equivalent to XOR. – kefeizhou Feb 15 '11 at 05:18
  • Could you please be a little more explicit? By "padding", what do you mean? And why every other digit? – user616847 Feb 15 '11 at 06:35
  • http://en.wikipedia.org/wiki/Redundant_binary_representation . If by complexity you mean worst-case, count me as highly suspicious that there's a way to achieve O(1) for repeated increments. – a dabbler Feb 15 '11 at 12:37
  • The wikipedia doesn't tell me anything about the actual algorithm though. I would be grateful if you would be a little more specific about it. The web did not help at all. :( – user616847 Feb 15 '11 at 17:44
  • Follow the pointers from Wikipedia. It looks as though carry-freedom is used to limit the depth of hardware adders; I can find no reference to the claim of O(k). – a dabbler Feb 15 '11 at 22:11
  • @a dabbler: the trick is that while this is not possible for all representations of a given number, you can make your algorithms construct output numbers so that both average amortized times and worst case times are good when they are operated on. – wnoise Mar 04 '11 at 09:23
  • The "algorithm" is shown in the circuit diagram of the article. It relies on parallelism of the adder circuits to be O(1). – xan Mar 19 '11 at 23:12

1 Answers1

0

I see this is an old post, and the poster does not have much recent activity here but thought I'd put forward the answer anyway.

In order to represent this circuit as a traditional equation, let's set forth some notation. Each 'bit' in RBR notation actually consists of two bits, so to refer to these right and left bits, I will use [0] and [1] respectively. To refer to a certain 'bit' position I will use braces {0},{1},{2},...{n}.

Addition of two or three single bits can result in a two-bit sum (the MSB is traditionally called the carry bit). These can also be referenced by [0] and [1], the latter being the carry bit. For example:
(0+1+1)[0]=0, (0+1+1)[1]=1, (0+0+1)[0]=1, (0+0+1)[1]=0

Now without much further, the general algorithm for adding numbers z = x + y is given by:
z{n}[0] = ((x{n-1}[1] + x{n-1}[0] + y{n-1}[1])[0] + (y{n-1}[0]) + (x{n-2}[1] + x{n-2}[0] + y{n-2}[1])[1])[1]

z{n}[1] = ((x{n}[1] + x{n}[0] + y{n}[1])[0] + (y{n}[0]) + (x{n-1}[1] + x{n-1}[0] + y{n-1}[1])[1])[0]

You will note that there is some carrying going on here, but the algorithm achieves O(n) because the carrying is limited to two orders. Also note the special considerations for z{0} and z{1}, which are defined in the circuit diagram in the aforementioned link.

Zoe
  • 27,060
  • 21
  • 118
  • 148
nicholas
  • 2,969
  • 20
  • 39