fork download
  1. # By Microsoft Copilot
  2.  
  3. import struct
  4.  
  5. def double_to_binary(num):
  6. # Pack the double into 8 bytes and convert to binary
  7. packed = struct.pack('>d', num) # '>d' for big-endian double
  8. binary_representation = ''.join(f'{byte:08b}' for byte in packed)
  9. return binary_representation
  10.  
  11. # Example usage
  12.  
  13. number = +1.0
  14. binary_form = double_to_binary(number)
  15. print(f'Double: {number}')
  16. print(f'Binary: {binary_form}')
  17.  
  18. number = +1.1
  19. binary_form = double_to_binary(number)
  20. print(f'Double: {number}')
  21. print(f'Binary: {binary_form}')
  22.  
Success #stdin #stdout 0.11s 14156KB
stdin
Standard input is empty
stdout
Double: 1.0
Binary: 0011111111110000000000000000000000000000000000000000000000000000
Double: 1.1
Binary: 0011111111110001100110011001100110011001100110011001100110011010