Two byte floating point number.

Yes please.
You my also have to change the byte order.

Try this

Function Float32ToFloat16(d as single) As Int16
  dim mb1 as new MemoryBlock(4)
  dim mb2 as new MemoryBlock(2)
  dim result as Int16
  mb1.LittleEndian= false
  mb2.LittleEndian= false
  
  mb1.SingleValue(0)= d
  
  // sign bit
  mb2.byte(0) = mb1.byte(0) AND &h80
  
  // exponent
  dim exp as integer
  exp = mb1.Int16Value(0) AND &h7f80     // bits 2-9 of 16
  exp = bitwise.ShiftRight(exp, 7)
  if exp<> 0 then
    exp= exp - 127  // bias for single
    exp = exp mod 16
    exp= exp + 7   //bias for half
    exp = bitwise.ShiftLeft(exp, 3)   // bits 2-5 of 8
    mb2.byte(0) = mb2.byte(0) OR exp
  end
  
  // mantissa
  dim man as integer
  man = mb1.Int32Value(0) AND &h7FFFFF
  man = ShiftRight(man, 12)    // only need first 10 bits
  mb2.Int16Value(0) = mb2.Int16Value(0) OR man
  
  result = mb2.Int16Value(0)
  return result
  
End Function

Nope… not right.

Yeah, I can’t make out the format. I even moved the sign bit so it’s 4-1-11 like in your original post, but I don’t get the right values. Sorry.

Of course, the values you provided are suspect. Shouldn’t there only be one representation of each number?

No it depends on how you calculate the exponent and the mantissa. (as you see on 1 and 100)
The result the other way should alway match.
Thats whay a function that calculate the real value would make things easier to check :slight_smile:

Thanks for your effort anyway.
Apriciate it !

The sign is bit 11 not 15
The exponent is bit 12 to 15

E3 E2 E1 E0 S M10 M9 M8 M7 M6 M5 M4 M3 M2 M1 M0

OK. I figured out the rules. The exponent has no bias - it cannot be negative. And the mantissa includes an explicit leading 1 bit, whereas a Single has an implicit 1 bit. So here are 2 methods:

Function Float32ToFloat16(d as single) As Int16
  dim mb1 as new MemoryBlock(4)
  dim mb2 as new MemoryBlock(2)
  dim result as Int16
  mb1.LittleEndian= false
  mb2.LittleEndian= false
  
  mb1.SingleValue(0)= d
  
  // sign bit
  mb2.byte(0) = mb1.byte(0) AND &h80
  mb2.byte(0) = bitwise.ShiftRight(mb2.byte(0), 4)
  
  // exponent
  dim exp as integer
  exp = mb1.Int16Value(0) AND &h7f80     // bits 2-9 of 16
  exp = bitwise.ShiftRight(exp, 7)
  
  // mantissa
  dim man as integer
  man = mb1.Int32Value(0) AND &h7FFFFF
  man = ShiftRight(man, 13)    // only need first 10 bits
  
  if exp<> 0 then
    exp= exp - 127  // bias for single
    if exp>= 0 then exp= exp+ 1   // add back the implicit 1 bit
    man= man OR &h400   // add back implicit 1
  end
  
  if exp< 0 then
    man = ShiftRight(man, abs(exp+1))
    exp= 0
  end
  
  exp = exp mod 16
  exp = bitwise.ShiftLeft(exp, 4)   // bits 1-4 of 8
  mb2.byte(0) = mb2.byte(0) OR exp
  
  mb2.Int16Value(0) = mb2.Int16Value(0) OR man
  
  result = mb2.Int16Value(0)
  return result
  
End Function
Function Float16ToFloat32(n as int16) As single
  dim mb1 as new MemoryBlock(4)
  dim mb2 as new MemoryBlock(2)
  dim result as Single
  mb1.LittleEndian= false
  mb2.LittleEndian= false
  
  mb2.Int16Value(0) = n
  
  // sign bit
  mb1.byte(0) = mb2.byte(0) AND &h08
  mb1.byte(0) = Bitwise.ShiftLeft(mb1.byte(0), 4)
  
  // exponent
  dim exp as integer
  exp = mb2.byte(0) AND &hF0
  exp = bitwise.ShiftRight(exp, 4)
  
  // mantissa
  dim man as integer
  man = mb2.Int16Value(0) AND &h7FF
  
  if exp= 0 and man<> 0 then
    // number between 0 and 1
    dim x as integer
    for i as integer = 10 downto 0
      x= 2^i
      x= man AND x
      if x = 0 then
        exp = exp -1
      else
        exit for
      end
    next
    exp= exp- 1
    man = man AND &h3FF
    man = bitwise.ShiftLeft(man, abs(exp+1))
  end
  
  if exp<> 0 then
    if exp> 0 then exp = exp - 1     // skip explicit 1 bit
    exp = exp + 127  // bias for single
    exp = bitwise.ShiftLeft(exp, 7)   // bits 2-9 of 16
    mb1.Int16Value(0) = mb1.Int16Value(0) OR exp
  end
  
  man= man AND &h3FF    // strip explicit 1
  man = Bitwise.ShiftLeft(man, 13) 
  mb1.Int32Value(0) = mb1.Int32Value(0) OR man
  
  result = mb1.SingleValue(0)
  return result
  
End Function

Great work Tim!!
Thanks!

It was an interesting puzzle. Some people do crosswords. I twiddle bits. :slight_smile:

LOL…

Let me know if you run out of puzzles.
CRC algorithms I can’t figure out…