Java – Convert Decimal to 17-bit 2’s Complement Signed Number

java

I am trying to convert decimals, (0.05, -0.28, 25.00, etc), to a 17-bit 2's complement Signed Number. The parameter data I am sending is SysEX Midi Data (to some external Synthesizer). The data parameters are composed of 3 bytes.

For example, the example given is as follows:

  • The parameter data to set the value to -0.02 is (07 7F 70) (Data2,Data1,Data0)

I was wondering if anyone could give me guidance for doing this in Java, here are the specifications I am going off of:

  • The parameter data is a 17-bit number 2’s complement signed number.
  • data2= Mix channel number and most significant bits (bits 14-16) of parameter’s data (with the format 0xxx xyyy, where xxxx is the mix channel
    number (0-15) and yyy are the uppermost bits (bits 14-16) of the parameter data.)

  • data1= Bits 7-13 of parameter’s data

  • data0= Least significant bits (bits 0-6) of parameter’s data

So, here is what I am able to assume so far:

  • The 3 bytes is composed of 17 bits, + the 4 bits for sending #'s 0-15 (The Mix Channel)
  • The 17th bit is the signing bit?
  • The signing bit only needs to be set if It's a negative number, and in which that case needs to be formatted w/ 2's complement
  • The highest bit for each parameters data byte is set to 0

With all this in mind, can someone help me understand, post, or help me write a function to format this 17-bit number from a signed decimal value? Binary Bit Adding / Subtracting In Java, Two's Complements, etc…It would be greatly appreciated it anyone has any example functions, or explanations!

Cheers

Best Answer

The value is scaled, so first you have to convert it into an integer. A 17-bit two's complement number has a range from -2^16 to 2^16 - 1, or -65536 to 65535. Therefore, for -0.02, you multiply it by 65536, which gives you -1310. As Ingo pointed out, Java values are already in two's complement notation, so as long as you mask off the bits greater than bit 16, you are already good to go.

This assumes the display goes from -1.0 to 1.0. If the maximum value on the display is different, you have to divide the -1310 by whatever the maximum value is. 100 gets pretty close to your example result, but it will differ for different parameters.

However, your three bytes are 7-bit bytes, but we just calculated it for 8-bit bytes, so you have to do some more screwy bit shifting to get it into the form it wants:

(0x1fae2 >> 14) & 0x7f = 0x07 # First byte
(0x1fae2 >> 7)  & 0x7f = 0x75 # Second byte
 0x1fae2        & 0x7f = 0x62 # Third byte

I'll leave it as an exercise for the reader to implement the first part in Java. If you pass in 0x1fae2 as param, Ingo's code works for the second part.

Related Topic