Java tutorial
//package com.java2s; import java.math.BigInteger; public class Main { public static byte[] generateNaf(BigInteger k) { BigInteger _3k = k.shiftLeft(1).add(k); int digits = _3k.bitLength() - 1; byte[] naf = new byte[digits]; for (int i = 1; i <= digits; ++i) { boolean _3kBit = _3k.testBit(i); boolean kBit = k.testBit(i); naf[i - 1] = (byte) (_3kBit == kBit ? 0 : kBit ? -1 : 1); } return naf; } }