22
\$\begingroup\$

Had my software final exams recently, one of the last questions had me thinking for a while after the exam had finished.

Background

IEEE754 numbers are according to the below layout Layout for a 32 bit floating point number

The exponent is stored as n + 127 so for an exponent of 20 the exponent in binary would be: 127 + 20 = 147 then 10010011

The Challange

Write a program that when given text representing a binary floating point number to the IEEE754 standard, returns the exponent given as a decimal value.

Example

Input: 00111101110011001100110011001101 (0.1)

Isolated exponent bits: 01111011

Convert to binary: 0 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = 123

Remove offset: 123 - 127 = -4

Output -4


Input: 10111111110111101011100001010010 (-1.64)

Isolated exponent bits: 01111111

Convert to binary: 0 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127

Remove offset: 127 - 127 = 0

Output 0

Rules

  • Input must be text/string/equivalent indexable datatype, eg. strings not integer or numerical
  • Output can be any type, as long as it is decimal
  • Aim for shortest solutions

Adapted from the 2020 Software Design and Development HSC Paper: 30) d) ii

Design an algorithm that takes in a string of 32 characters representing a floating point number, and displays the exponent as its signed decimal value.

Any feedback is appreciated as this is my first question

\$\endgroup\$
17
  • 1
    \$\begingroup\$ Can we take the input as a list of 1s and 0s? \$\endgroup\$ – Adám Nov 5 '20 at 9:50
  • 1
    \$\begingroup\$ All challenges here must have an objective scoring criterion, for which we usually recommend code-golf unless you have something else in mind. \$\endgroup\$ – Bubbler Nov 5 '20 at 9:50
  • 1
    \$\begingroup\$ Also, if you have another challenge idea, please use the sandbox to gather feedback from other users and fix potential problems. It is recommended to leave a challenge there for at least 3 days before posting it to main. \$\endgroup\$ – Bubbler Nov 5 '20 at 10:03
  • 1
    \$\begingroup\$ Btw, welcome to the site! Nice first challenge. In the future, consider using the Sandbox. Had it been there, I might have suggested taking a variable length input representing a binary float with 16/32/64/128/265 bits. \$\endgroup\$ – Adám Nov 5 '20 at 10:05
  • 6
    \$\begingroup\$ @Adám I would of submitted it there but was unable to as i have less than 5 reputation \$\endgroup\$ – Maximilian Rose Nov 5 '20 at 10:25

36 Answers 36

1
2
1
\$\begingroup\$

Japt, 8 bytes

Note the unprintable (charcode 127) at the end.

ů8 Í-#

Try it

\$\endgroup\$
1
\$\begingroup\$

Whitespace, 121 bytes

[S S S N
_Push_0][S S S T    T   T   N
_Push_7][S N
S _Duplicate_7][T   N
T   S _Read_STDIN_as_character][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][S N
S _Duplicate][T N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S S S T    T   S S S S N
Push_48][T  S S T   _Subtract][S T  S S T   S N
_Copy_0-based_2nd][S S S T  S N
_Push_2][T  S S N
_Multiply][T    S S S _Add][S N
T   _Swap_top_two][S S S T  N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   T   S N
_If_negative_Jump_to_Label_PRINT_RESULT][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT_RESULT][S N
N
_Discard_top][S S S T   T   T   T   T   T   T   N
_Push_127][T    S S T   _Subtract][T    N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer r = 0
Integer n = 7
Read STDIN as character
Start LOOP:
  Integer c = STDIN as character
  c = c - 48
  r = r * 2
  r = c + r
  n = n - 1
  If(n < 0):
    Jump to label PRINT_RESULT
  Go to next iteration of LOOP

PRINT_RESULT:
  r = r - 127
  Print r as integer to STDOUT
\$\endgroup\$
1
\$\begingroup\$

Husk, 8 bytes

-127ḋ↑8t

Try it online!

Straightforward implementation following the steps of the question. Accepts list of digits as argument.

-127ḋ↑8t
       t        # tail: remove first element
     ↑8         # get first 8 remaining elements
    ḋ           # interpret as binary digits
-127            # subtract 127
\$\endgroup\$
1
\$\begingroup\$

Julia 0.7, 25 bytes

n->parse("0b"*n[2:9])-127

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 40 bytes

Convert.ToInt32(s.Substring(1,8),2)-127;

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Zsh, 20 bytes

<<<$[0b${n:1:8}-127]

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.