java - Straighten-Out 2's Complement -
if have byte[] of length n, containing 2's complement representation of n-byte integer, how byte[] of same length "straight" representation of number? possible, given how there more negative positive integers representable 2's complements?
by "straight" representation mean continuously map integer range range of bytes, without jumps, holes, etc.
e.g.
for 1-byte integer -128, two's complement representation 1000 0000 1-byte integer -1, two's complement representation 1111 1111 1-byte integer 0, two's complement representation 0000 0000 1-byte integer 1, two's complement representation 0000 0001 1-byte integer 127, two's complement representation 0111 1111
what i'm looking convert above values this:
for 1-byte integer -128, "straight" representation 0000 0000 1-byte integer -1, "straight" representation 0111 1111 1-byte integer 0, "straight" representation 1000 0000 1-byte integer 1, "straight" representation 1000 0001 1-byte integer 127, "straight" representation 1111 1111
is there proper name i've dubbed "straight" representation here?
i'd prefer java in solutions, other easily/intuitively understandable language or pseudocode ok too. (is there special wary of in conversion, since java doesn't have unsigned bytes?)
java integer bit-manipulation twos-complement
No comments:
Post a Comment