Non-adjacent form


The non-adjacent form of a number is a unique signed-digit representation. Like the name suggests, non-zero values cannot be adjacent. For example:
All are valid signed-digit representations of 7, but only the final representation,, is in NAF.

Properties

NAF assures a unique representation of an integer, but the main benefit of it is that the Hamming weight of the value will be minimal. For regular binary representations of values, half of all bits will be non-zero, on average, but with NAF this drops to only one-third of all digits.
Obviously, at most half of the digits are non-zero, which was the reason it was introduced by G.W. Reitweisner for speeding up early multiplication algorithms, much like Booth encoding.
Because every non-zero digit has to be adjacent to two 0s, the NAF representation can be implemented such that it only takes a maximum of m + 1 bits for a value that would normally be represented in binary with m bits.
The properties of NAF make it useful in various algorithms, especially some in cryptography; e.g., for reducing the number of multiplications needed for performing an exponentiation. In the algorithm, exponentiation by squaring, the number of multiplications depends on the number of non-zero bits. If the exponent here is given in NAF form, a digit value 1 implies a multiplication by the base, and a digit value −1 by its reciprocal.
Other ways of encoding integers that avoid consecutive 1s include Booth encoding and Fibonacci coding.

Converting to NAF

There are several algorithms for obtaining the NAF representation of a value given in binary. One such is the following method using repeated division; it works by choosing non-zero coefficients such that the resulting quotient is divisible by 2 and hence the next coefficient a zero.
Input E = 2
Output Z = NAF
i ← 0
while E > 0 do
if E is odd then
zi ← 2 −
EEzi
else
zi ← 0
EE/2
ii + 1
return z