For a single byte in twos complement notation, FF is -1, FE is -2, FD is -3, etc. The values from 80 (-128) to FF (-1) are negative numbers. To get the positive values, do an "exclusive or" (xor / '^' operator) with FF, and then add one.
FD xor FF = 02. Add one and you get '03'. Therefore 'FD' = -3.
To get the number of repeats, get the "absolute value" of the signed byte, and then add one. [So if you're doing bitwise operations, you'll add one twice.]
To repeat 20 times, you need -19 decimal as a single-byte signed value. 19 decimal = 13 hex, xor FF = EC, + 1 = ED.
(P.S. To see if a number is negative, do 'and 80'. Zero result is positive; '80' result is negative. Or if you have signed bytes, as in Java or C, just check for '< 0' ;-)
JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28