java - Leading zero in BigInteger multiplication -


i use simple function multiply big integers. 1 more leading 0 byte included output. why happened , how can prevent it?

ps: , b less mod

private byte[] multiply(final byte[] a, final byte[] b, final biginteger mod) {     biginteger m1 = new biginteger(1, a);     biginteger m2 = new biginteger(1, b);      biginteger out = m1.multiply(m2).mod(mod);      res = out.tobytearray(); } 

the 0 byte added if (positive) value has first byte 128 255.

this byte needed resulting byte has @ least 1 sign bit (https://docs.oracle.com/javase/7/docs/api/java/math/biginteger.html#tobytearray%28%29)

biginteger.valueof(5l).tobytearray() 

returns byte array 1 byte (5).

biginteger.valueof(128l).tobytearray() 

returns byte array 2 bytes (0 , 128 in unsigned representation). distinguish result from

biginteger.valueof(-128l).tobytearray() 

which returns byte array 2 bytes (255, 128 in unsigned representation)


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -