#if !defined  HAVE_BIN2NAF_H__
#define       HAVE_BIN2NAF_H__

#include "fxttypes.h"

inline void bin2naf(ulong x, ulong &np, ulong &nm)
// Compute (nonadjacent form, NAF) signed binary representation of x:
// the unique representation of x as
//   x=\sum_{k}{d_k*2^k} where d_j \in {-1,0,+1}
//   and no two adjacent digits d_j, d_{j+1} are both nonzero.
// np has bits j set where d_j==+1
// nm has bits j set where d_j==-1
// Thereby:  x = np - nm
{
    ulong xh = x >> 1;  // x/2
    ulong x3 = x + xh;  // 3*x/2
    ulong c = xh ^ x3;
    np = x3 & c;
    nm = xh & c;
}
// -------------------------


inline ulong naf2bin(ulong np, ulong nm)
// Inverse of bin2naf()
{
    return ( np - nm );
}
// -------------------------

#endif  // !defined HAVE_BIN2NAF_H__
