[Question]
  [
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](https://i.stack.imgur.com/V5zks.png)](https://i.stack.imgur.com/V5zks.png)
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](https://en.wikipedia.org/wiki/IEEE_754) 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](https://educationstandards.nsw.edu.au/wps/portal/nesa/resource-finder/hsc-exam-papers/2020/software-design-and-development-2020-hsc-exam-pack): 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
  
[Answer]
# [Haskell](https://www.haskell.org/), 33 bytes
```
foldl1(\x y->x*2+y-1).take 8.tail
```
[Try it online!](https://tio.run/##y0gszk7NyfmfZhvzPy0/JyXHUCOmQqFS165Cy0i7UtdQU68kMTtVwQJIZeb8z03MzFOwVSgoyswrUVBRSFOINtAx0DGEQgM4bUAkbRj7/19yWk5ievF/3eSCAgA "Haskell – Try It Online")
Input is a list of integers 0 or 1.
It's slightly shorter to `foldl1` by `x*2+y-1` than to subtract 127 at the end. This works because
```
  foldl1 (\x y->x*2+y-1) [a,b,c,d,e,f,g,h]
= ((((((a*2+b-1)*2+c-1)*2+d-1)*2+e-1)*2+f-1)*2+g-1)*2+h-1
= a*128 + b*64-64 + c*32-32 + d*16-16 + e*8-8 + f*4-4 + g*2-2 + h-1
= 128a + 64b + 32c + 16d + 8e + 4f + 2g + h - 127
= fromBinary[a,b,c,d,e,f,g,h]-127
```
[Answer]
# [Python 3](https://docs.python.org/3/), 26 bytes
```
lambda s:int(s[1:9],2)-127
```
[Try it online!](https://tio.run/##ZU5BCoMwELz7ir1tAhESFaRC@hH1YG2Cpa1Kkh6K@PY0Se2pAzvMsrO7s77dtMyl17Lzj@F5uQ5gm9vsiG1Fc@pZQXNR1N4p6yxIIBkEEORcBESK6q8Esryi7DAnn/gtBPru8SgiIQMezDQbJzXelYl/sHsVdTWGUVKiRJrpxYBjCm4zpDxNuh/tmjiamtXE7Bo3t0N@hs3ssB1XWyOl6nek/gM "Python 3 – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~55~~ \$\cdots\$ ~~40~~ 36 bytes
Saved ~~2~~ 6 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!!!  
Saved ~~2~~ a whopping 13 bytes thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!!
```
#define f(s)strtol(s+1,s[9]=0,2)-127
```
[Try it online!](https://tio.run/##jVHbboMwDH3vV1hMSKENGpdNU8XYy7SvaHlIQ9KidRThSGOr@PUxh0vbdS@NZMucHI7tE@lvpey6u1zpolSgGXpoanPYM1yEHFfLLA145Plh9NQVpYEPUZTMmx1nQEfuRD0Ho9DgKoMUjk4QhHRsstW/CB0OTngDp6fcpDPxKA30wBY2OW3ST2nHVk2lpFH5MKb/wMFGGFEKRlq/jNwp@a5qovXrrJu3aN0sXykebcuL73iS14camO1RlLlq6LcgGctnwOJbHTSbunv3IzA/IQksFj3bg8HTs69IWoO3PSFL/tzDZhXHFxi9mqy@2IajdwbtWIJkNNtcoYrQkynX@lVNFM0cF8F/ATcHF9cl7Y8cBIfJI5WmIhtl21nb/Ui9F1vs/M9f "C (gcc) – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~8~~ 7 bytes
```
¦8£CƵQ-
```
[Try it online!](https://tio.run/##yy9OTMpM/f//0DKLQ4udj20N1P3/38DAEAhABIiFgQ0B "05AB1E – Try It Online")
```
¦8£CƵQ-
¦             remove first element
 8£           take next 8 elements
   C          convert from binary string to an integer
    ƵQ        push 127
      -       subtract
```
Thanks to Kevin for -1 byte
[Answer]
# x86 machine code, 14 bytes
Same machine code works in 32 or 64-bit mode. (Same length for 16-bit mode would be easy with different regs). Callable from C as x86-64 System V `int8_t getexp(const char *b2_str);`
```
    23        ; input: char *str in RDI
    24        ; return: int8_t AL; (calling convention allows garbage in high bytes of RAX)
    25                         getexp_scalar:
    26 00000030 6A09              push 9                 ; sign bit gets shifted out the top.
    27 00000032 59                pop  rcx
    28 00000033 B030              mov  al, '0'
    29                         .loop:
    30 00000035 AE                 scasb                    ; set FLAGS like cmp '0', [rdi]  would, if it was encodeable
    31 00000036 10D2               adc  dl,dl               ; DL = DL<<1 | CF, rotate CF into DL.
    32 00000038 E2FB              loop .loop
    33 0000003A 8D4281            lea  eax, [rdx-127]      ; no shorter than sub dl, 127  or  xchg eax,edx / sub al,127
    34 0000003D C3                ret
```
We just do base 2 ASCII->integer conversion for 9 bits, letting the first bit (sign bit) overflow off the top of DL. The 8-bit return value is only the 8-bit AL register, as a 2's complement binary integer.
`adc dl,dl` to rotate CF into DL is more efficient on modern CPUs than `rcl dl, 1` (rotate through carry, i.e. shift in CF). They're equivalent except for some FLAGS output semantics. I could have used EDX for the same code size; that would simply leave different garbage in the high bytes outside the AL return value in RAX. (And yes, high garbage above narrow return values is [100% allowed by standard x86 calling conventions](https://stackoverflow.com/questions/36706721/is-a-sign-or-zero-extension-required-when-adding-a-32bit-offset-to-a-pointer-for/36760539#36760539).)
[Try it online!](https://tio.run/##hVVtb9s2EP7OX/GgX/pme5a7tZ3dYUhXdAhgYECyDwOKwDiJtEREIhWScuRh/z07Uo4VOxsqwHrhkc89d/fcmbxXTV7vp4Z88/BAtS4NsvdihUC3yi9RWOMDiooc3vjgoA2uvlyy3anQOcM7tAkfNwEX6xWASpcV8n1QHnaLq4u/0NAeuUJJLqdSgTxa5eADGUlOoqC61qaMfnbKBM3uZqJUQfXtxrOR3FLgcLWdr/Azzq8VfGSd6wDN6M4WiqOSYA9gJA9f6W3gBdsFhEoh2HY2gtoWcEV/XGjsDqB6gpfzl2JWW9uODJiRz/EfF3NQAV/XF79fo9a3CkXTRoAJvjmpb4B729VyAr0F07znLChTWKkor9URnWQByHoi62foX9b4hW@fPmX4B799ncDZQEHxa8y/ZdMYUaSMRHxcUgQo6hOdfpotPtw8IhvL@bEucFFCRQa@yyMH8B7AOqAvqjKdVbLHD8nO2WHzEZ2lIMQT6WjTdmGJq@vLScx5@rhYn1Z1cV7W5wU5yz3HJX0@fsbypjplT7akOOUhzjcLvIWj/ua7qTnLzf9EdgigafonrDiFiRi7is87fub@ntp4mnq@syY9NYodmTJUsQFYYdwRbnDp9dvs6HCwDEcH6bAx@Yg9NXVqp5xXeJXrcqqM1GRes1eSsdmispmeNdxG2GpVyxkLlnbcitln7PwsKfvA7tGhrw5aSw4/nAR1n1qKxZWamhtfUTE0dwLmkLz@e@hoX9cSTTNniNmTWO6Gl7jO@I@Gli2Nv82HpLP16LOl4hY7VQTW3TBJNLdvEniWHB9pd4cupEGpYqzXg1Rb6urA77Uoa5tTjQ2PGxfE8FiKOKh2TELeEfaJ3TcebfNNdmyKqK2TjDOD9fXn6VY7H34VYhQOl2g8Lo7zA1zXSTSmpTjkgFE@4uCGt/oeKm8nHMiIms6eYCaA0/45x@jPMOT3McQj3wgQ9zPAMYBUnMW71Fx@74cQno891euwKZ3t2lcx@zvO9mshViv8efkHz2KuncfHaRRS3Bnnfuh81Exn4tRWPBO9xd52qFiqMc/SJikvflpgyvf3PPimP0b58SaWhSI5E8KzSPi/AjNnJQU69Oi7hRgiXkLmeDGfZ3zFW3x79stePPwL "Assembly (nasm, x64, Linux) – Try It Online") 3 versions with a test caller.  
(Including alternate 15-byte scalar version using `lodsb` / `and al, 1` / `lea edx, [rdx*2 + rax]`)
# x86-64 SIMD (MMXext+MOVBE) machine code, 20 bytes
Having base-2 digits in MSB-first printing order is a major inconvenience for little-endian x86, where the lowest SIMD vector element comes from the lowest address in memory. x86 lacks a bit-reverse like ARM's `rbit`, so I used x86-64 features to byte-reverse 8 bytes. Without that, `movq mm0, [e/rdi+1]` (4B) / `pslld mm0,7` (4B) would be 32-bit-mode compatible and work on a Pentium III (`pmovmskb r32, mm` was new with SSE, [aka MMXext](https://en.wikipedia.org/wiki/Extended_MMX), so not original P5 Pentium-MMX).
```
                             ; input: char *RDI;   result int8_t AL
                             ; clobbers: MM0.  Leaves FPU in MMX state (no EMMS)
    12                         getexp_mmx:
    14 00000010 480F38F04701          movbe    rax, [rdi+1]   ; byte-reverse (big-endian) load of the exponent field.  saves 1B vs. mov + bswap
    15 00000016 48C1E007              shl      rax, 7         ; low bit to high in each byte.  same size as pslld mm0, 7.
    16 0000001A 480F6EC0              movq     mm0, rax
    17 0000001E 0FD7C0                pmovmskb eax, mm0       ; pack vector high bits into 1 byte
    18 00000021 2C7F                  sub      al, 127
    19 00000023 C3                    ret
; size = 0x24 - 0x10 = 0x14 = 20 B
```
MMX instructions typically need 1 fewer prefix byte than SSE2 and later; if you were doing this for real you'd probably use XMM0, and avoid the EMMS (exit MMX state) that real code would normally use. (And maybe a `pshufb` for the byte reverse if you have SSSE3, to avoid bouncing through integer regs...) This should still be much faster than a loop with adc and false dependencies, and the slow `loop` instruction itself. **It's handy that the exponent field is exactly 8 bits wide.** Doing this for `double` would need an SSE2 16-byte load (or 2 byte reverses and annoying shuffles to combine into a 16-byte vector), and would have to mask away the sign bit.
lodsb (1) + lodsq (2) + bswap rax (3) is the same total length as `movbe rax, [rsi+1]` (6), although that would make this portable to machines without `movbe` (Atom, Haswell and later).
---
For subnormal (aka denormal) floats including `0.0`, this just returns `-127`, the unbiased the exponent *field*, as requested by the question. And `-128` (overflowed from +128) for Inf / NaN inputs. (I only realized this overflow issue after the fact, posting anyway because all 255 possible outputs are *unique*, and that puts both special exponents at adjacent values so the caller can test for subnormal or inf/nan with `e <= -127`.)
It doesn't return the actual `floor(log2(|x|))` of the represented value like AVX512 `vgetexpps xmm, xmm` does: (`-Inf` for an input of 0, and values below -127 for non-zero subnormals, matching [the pseudocode in the documentation](https://www.felixcloutier.com/x86/vgetexpps#tbl-5-15)).
So even if we could take input as an actual *binary* floating-pointer number in [IEEE754 binary32 format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) (aka `float`) in an XMM register, we couldn't just use that one 6-byte AVX512 instruction to produce an integer-valued `float` result.
---
### not competing AVX2 version, 15 bytes
This takes its arg as 32 bytes of ASCII digits in a YMM register, but they have to be in least-significant-digit first order, hence not competing.
```
     2                         getexp_avx2_ymm_reversed:
     3             ; input: LSD-first ASCII digits in ymm0.  Probably can't justify that and would need to byte-reverse 
     5 00000000 C5FD72F007             vpslld     ymm0, ymm0, 7
     6 00000005 C5FDD7C0               vpmovmskb  eax, ymm0
     7 00000009 C1E817                 shr        eax, 23         ; AL = exponent
     8 0000000C 2C7F                   sub        al, 127
     9 0000000E C3                     ret
```
Fun fact: AVX-512 allows `vpslld ymm0, [rdi], 7`, if you had bytes in the LSD-first order in memory. (7 bytes including 4-byte EVEX, so an extra 2 bytes to take a char\* instead of YMM arg.)
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), 7 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
╞8<å♣(-
```
[Try it online.](https://tio.run/##y00syUjPz0n7///R1HkWNoeXPpq5WEP3/38lAwNDIAARIBYGNlTigioB8g1AGBOAlBiCJSEGYQFKAA)
**Explanation:**
```
╞        # Remove the first character of the (implicit) input-string
 8<      # Slice to only keep the first 8 characters of the string
   å     # Convert it from a binary-string to a base-10 integer
    ♣    # Push constant 128
     (   # Decrease it by 1 to 127
      -  # Subtract it from the integer
         # (after which the entire stack joined together is output implicitly as result)
```
[Answer]
## JavaScript (ES6), 23 bytes
```
f=
b=>(`0b${b}0`>>>24)-127
```
```
<input oninput=o.textContent=f(this.value)><pre id=o>
```
Uses the fact that `>>>` truncates to 32 bits.
[Answer]
# [Ruby](https://www.ruby-lang.org/) `-n`, ~~24~~ ~~23~~ 20 bytes
*-1 byte thanks to [Sisyphus](https://codegolf.stackexchange.com/users/48931/sisyphus)*  
*-3 bytes using `-n` command-line flag, inspired by [this answer](https://codegolf.stackexchange.com/a/213642/83605)*
```
p$_[1,8].to_i(2)-127
```
[Try it online!](https://tio.run/##KypNqvz/v0AlPtpQxyJWryQ/PlPDSFPX0Mj8/38DA0MgABEgFgY25ALLGcIUAQmIWgMQA0T8yy8oyczPK/6vmwcA "Ruby – Try It Online")
[Answer]
# [Kotlin](https://kotlinlang.org), ~~26~~ ~~31~~ 29 bytes
Converted to a valid anonymous function, thanks to [Bubbler](https://codegolf.stackexchange.com/users/78410/bubbler)  
-2 bytes thanks to [user](https://codegolf.stackexchange.com/users/95792/user)
```
{it.slice(1..8).toInt(2)-127}
```
[Try it online!](https://tio.run/##y84vycnM@59WmqeQm5iZp5FYlF5speBYVJRYaRNcUpSZl26nqVDNpQAFZYk5CmkVVgoaEDlNBV07Bc@8EgXb/9WZJXrFOZnJqRqGenoWmnol@UBxDSNNXUMj89r/nAVA5SU5eRppFRpKBgaGQAAiQCwMbKikqclV@x8A "Kotlin – Try It Online")
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 22 bytes
```
echo $[2#${1:1:8}-127]
```
[Try it online!](https://tio.run/##S0oszvj/PzU5I19BJdpIWaXa0MrQyqJW19DIPPb///8GBoZAACJALAxsCAA "Bash – Try It Online")
# [Bash](https://www.gnu.org/software/bash/), ~~37~~ 27 bytes
Input is from STDIN, output is to STDOUT.
```
expr $[2#`cut -b2-9`] - 127
```
[Try it online!](https://tio.run/##S0oszvj/P7WioEhBJdpIOSG5tERBN8lI1zIhVkFXwdDI/P9/AwNDIAARIBYGNgQA "Bash – Try It Online")
## Explanation
```
expr                             # Evaluate the following:
         `cut -b2-9`             #     Slice all characters from the 2nd character
                                 #     to the 9th character in standard input
     $[2#           ]            #     Convert it from base 2
                      - 127      #     And subtract by 127
```
[Answer]
# [Desmos](http://desmos.com/calculator), ~~42+9=51~~ ~~40+9=49~~ 30 bytes
```
f(l)=\sum_{i=0}^7l[9-i]2^i-127
```
Input as a list of 1's and 0's.
[![](https://i.stack.imgur.com/pWCJD.png)](https://i.stack.imgur.com/pWCJD.png)
[Try It On Desmos!](https://www.desmos.com/calculator/urde7webzo)
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 12 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?") ([SBCS](https://github.com/abrudz/SBCS ".dyalog files using a single byte character set"))
```
¯127+2⊥8↑1↓⊢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT////9B6QyNzbaNHXUstHrVNNHzUNvlR16L/aY/aJjzq7XvU1fyod82j3i2H1hsDZR/1TQ0OcgaSIR6ewf/TFAyA0BAKDeC0AZG0IQA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 63 bytes
```
2=M&!`.{8}
1
01
+`10
011
$
-127$*
(1*)-\1
-
^0*(-)?(1*)-?
$1$.2
```
[Try it online!](https://tio.run/##K0otycxL/P/fyNZXTTFBr9qilsuQy8CQSzvB0ABIG3KpcOkaGpmraHFpGGpp6sYYculyxRloaehq2oMF7LlUDFX0jP7/NwAqNjQEESAWBjYEAA "Retina 0.8.2 – Try It Online") Explanation:
```
2=M&!`.{8}
```
Get the second overlapping 8-character substring.
```
1
01
+`10
011
```
Convert to binary (may leave leading `0`s).
```
$
-127$*
(1*)-\1
-
```
Subtract 127 (may leave trailing `-`).
```
^0*(-)?(1*)-?
$1$.2
```
Convert to decimal, removing garbage.
[Answer]
# [J](http://jsoftware.com/), 12 bytes
```
127-~1{8#.\]
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DY3MdesMqy2U9WJi/2tyKXClJmfkK6QBVRgoGEKhAZw2IJI2/A8A "J – Try It Online")
```
        #.        convert to binary
       8  \       all the infixes with length 8
           ]      of the argument
     1{           take the second one (0-indexed) 
127-~             subtract 127
        
```
[Answer]
# C#, 120 115 42 32 bytes
```
s=>Convert.ToByte(s[1..9],2)-127
```
Thanks to @SunnyMoon for realising that the code is not actually 120 bytes (-2)  
Thanks to @Sunnymoon for slaying 3 *sneaky* spaces in the code (-3)  
Thanks to @user for coding a much **much** shorter lambda solution (***-73***)  
Thanks to @thedefault. for using the `..` operator and `Convert.ToByte` (***-10***)
[Try it online!](https://tio.run/##ZYyxCsIwEIb3PMXRqYE2NF1EagsquDkpOIhDiKkEagK9a6FInz2mxc2DO35@vu805hp1GNC6F1wmJPOumO4UIuzhwwCQFFkNo7dPOCvrUr7W8IPFaXB6h9RHPwPrqIEW6oB1c/RuND2Jqz9MZFK8SyG2j6zkuSw3oVp/RAZ9Z8SttxFp06QoZJzlLOlvZcL5Ys5sDl8)
**As it turns out, C# is actually quite useful for golfing! Lambdas are the way to go!**
---
Welcome to code golf, Maximilian Rose!
[Answer]
# [Python 3](https://docs.python.org/3/), 54 bytes
ty to @Bubbler and @Kevin Cruijssen for corrections
```
lambda n: sum([2**i*int(n[8-i])for i in range(8)])-127
```
[Try it online!](https://tio.run/##K6gsycjPM/7/PycxNyklUSHPSqG4NFcj2khLK1MrM69EIy/aQjczVjMtv0ghUyEzT6EoMS89VcNCM1ZT19DI/P9/MwtTyxQA "Python 3 – Try It Online")
[Answer]
# [><>](http://esolangs.org/wiki/Fish), 30 bytes
```
r~8[01:@{2%*+$2*60l4)?.{""-n;
```
(contains an unprintable `DEL` between the quotes)
[Try it Online!](https://tio.run/##S8sszvj/v6jOItrA0Mqh2khVS1vFSMvUIMdE016vWqleSTfP@v///7rFBgaGQAAiQCwMbAgA)
```
r~8[                           grab relevant part of string
    01                         accumulator and exponent initialization
                             main loop
       @{2%                    convert current digit charcter to 0 and 1
           *+$                 accumulate     
      :       2*               increment exponent
                60l4)?.        loop while digits remaining
                       {""-n;  print result sub 127 and terminate
```
Most of the work is constructing a number from binary notation.
[Answer]
# Scala, 27 bytes
Uses Lynn's [clever approach](https://codegolf.stackexchange.com/a/214774/95792)
```
_.slice(1,9)reduce(_*2+_-1)
```
[Try it online](https://scastie.scala-lang.org/pUShRsE7TVSCWYzYpJK8yg)
# Scala, 29 bytes
```
n=>BigInt(n.slice(1,9),2)-127
```
[Try it online!](https://scastie.scala-lang.org/9DgvUdWjRki0fkZ2BXCjhQ)
Very straightforward. Takes a `String`, parses the exponent's bits as an integer in binary, then subtracts 127, returning a `BigInt`.
# Scala, 29 bytes
```
n=>(BigInt(n.tail,2)>>23)-127
```
[Try it online](https://scastie.scala-lang.org/pSvsZAQ3S5aOOVkOhC2kjg)
Unfortunately, because of operator precedence, the outer parentheses are needed.
# Scala, 29 bytes
```
_.slice(1,9)./:(0)(_*2+_)-127
```
[Try it online](https://scastie.scala-lang.org/bAxtSdGJRKqCgeNT3py96Q)
Yet another solution, this time taking a list of digits (`List[Int]`).
[Answer]
# [Forth (gforth)](http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/), 49 bytes
```
: b drop 1+ 8 ['] evaluate 2 base-execute 127 - ;
```
[Try it online!](https://tio.run/##S8svKsnQTU8DUf//WykkKaQU5RcoGGorWChEq8cqpJYl5pQmlqQqGCkkJRan6qZWpCaXArmGRuYKugrW/4uVFAwMDIEARIBYGNhQCWio3n8A "Forth (gforth) – Try It Online")
Done using (a lot of) Bubbler's help.
Forth has one(1) convenient command that allows interpretation of strings as binary, and that's used here(`base-execute`).
[Answer]
# [Rust](https://www.rust-lang.org/), 43 bytes
```
|s|Ok(i32::from_str_radix(&s[1..9],2)?-127)
```
[Try it online!](https://tio.run/##ZYyxCsIwFEV3vyJ2kATaklRBjFonByfFVaQEmkCxTfG9FATrt9fE4qIP3uUO9xzo0A3GkkZVlrInqbUjRhpLZ@iAJflZY1e7TTXPYnSllLZrpDwpQH2wbg/QQk62Q4/98Ub9SEoDbVN4tgBVVg@vuYg0XV3jjO0SkS3ZsJ4oRA2u0PcpNTTiXPgLEdrfi4jF3p0sGPslP5D40j5GCQ8lxEjyAL6GNw "Rust – Try It Online")
Returns a `Result<i32,ParseIntError>`. I would have loved to use an `i8` instead of an `i32` but that would panic if the leading bit is 1.
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), ~~174~~ ~~113~~ 111 bytes
-61 bytes for many optimizations
-2 bytes thanks to RezNesX
```
,>++++++++>>--[>-<--]>-<<<[>,>>>[--<+>]<[->+<]<-[<->-----]<+++[->>>[->+>+<<]>>[-<<+>>]<<<<<]<-]>>>>---[>-<--]>.
```
[Try it online!](https://tio.run/##ZYxBCoRQDEMP9H8G3YdcpHQxCoIILgTP/yeFwY2BhpK@Zrm@@7nd6zFGV/tLAkIgkHYy1CUFwKZkQI1JBGHOSvrHaSFqPjJrpWnjJdOOqvbp/YwxTbNVVttr5h8 "brainfuck – Try It Online")
Input is 32 bits taken one at a time.
Explanation:
```
,> first bit garbage
++++++++ set cur cell (cell 2) to 8
>>--[>-<--]>-<<< set cell 5 to 128 (so the first division moves it over)
[> do 8 times
, input to cell 3
>>> cell 6
[--<+>] div 2 to 5
<[->+<] move back to 6
< cell 4
-[<->-----]<+++ convert to bin value (sub 48) using cell 4
[- if 1 (in cell 3)
>>>[->+>+<<] add cell 6 into cell 7 & 8
>>[-<<+>>] move cell 8 back to 6
<<<<<
]
<-] ends on c2
>>>> to c6 (has 1 and next cell has value)
---[>-<--]> sub 127
. outputs final answer (as ASCII= if this is invalid let me know)
```
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), ~~11~~ 6 bytes
```
9ŻB⁺ɽ-
```
[Try it Online!](http://lyxal.pythonanywhere.com?flags=&code=9%C5%BBB%E2%81%BA%C9%BD-&inputs=00111101110011001100110011001101&header=&footer=)
I've got an advantage that y'all don't have: I actually sat the exam this question is based upon as well at the same time as the OP (#hsc\_gang #option\_topic\_2 #ir\_sw\_hw). Hopefully I got both that question and this answer right!
## Explained
```
9ŻB⁺ɽ-
9Ż      #     input[1:9]
  B     # int(          , 2)
   ⁺ɽ   #                      127 (N.B. Pushes the index of ɽ in the codepage + 101)
     -  #                    -
        # int(input[1:9], 2) - 127
```
**Update**: Turns out I got the wrong answer in the exam. Not pog.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
Ḋḣ8Ḅ_127
```
[Try it online!](https://tio.run/##y0rNyan8///hjq6HOxZbPNzREm9oZP7//38DHQMdQyg0gNMGRNKGAA "Jelly – Try It Online")
Takes input as a list of digits
## How it works
```
Ḋḣ8Ḅ_127 - Main link. Takes a list of bits B on the left
Ḋ        - Dequeue; Remove the first element of B
 ḣ8      - Take the first 8 elements
   Ḅ     - Convert from binary
    _127 - Subtract 127
```
[Answer]
# [K (ngn/k)](https://bitbucket.org/ngn/k), 11 bytes
```
-127+2/8#1_
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NStfQyFzbSN9C2TD@f5qCARAaQqEBnDYgkjb8DwA "K (ngn/k) – Try It Online")
```
         1_        drop one
       8#          take 8
     2/            to binary
-127+              subtract 127
```
# [K (ngn/k)](https://bitbucket.org/ngn/k), 14 bytes
Just an experiment - a port of [Lynn's Haskell answer](https://codegolf.stackexchange.com/a/214774/75681)
```
{y-1+2*x}/8#1_
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqrpS11DbSKuiVt9C2TD@f5qCARAaQqEBnDYgkjb8DwA "K (ngn/k) – Try It Online")
[Answer]
# [Befunge-98 (PyFunge)](https://pythonhosted.org/PyFunge/), 31 bytes
```
[[email protected]](/cdn-cgi/l/email-protection)~'$$<
_1->\2*~2%+\#^:!
```
[Try it online!](https://tio.run/##S0pNK81LT9W1tNAtqAQz//@vUzEvc9DTNdStU1dRseGKN9S1izHSqjNS1Y5RjrNS/P/fwMAQCEAEiIWBDQE "Befunge-98 (PyFunge) – Try It Online")
---
### Animation
[![enter image description here](https://i.stack.imgur.com/whCvt.gif)](https://i.stack.imgur.com/whCvt.gif)
### Explanation
`~$7v` is the initialization code:  
`~$` read and discard the first character  
`7` push initial value of the loop counter  
`v` redirect the instruction pointer down
The second line is the main loop, since `>` is the entry point it is executed as `\2*~2%+\#^:!_1-` from left to right. Initially the loop counter is on top of the stack and the current result on the second position:  
`\` swap to the current exponent value  
`2*` double it  
`~` read the next digits  
`2%` modulo `2` maps `'0'` to `0` and `'1'` to `1`  
`+` add to the current result  
`\` swap to the loop counter  
`#` skips the the command (`^`)  
`:!` duplicate the loop counter and boolean negate the copy
If the value on the top of the stack is now falsey (loop counter was `>0`), `_` lets the IP move east and the main loop continues by subtracing `1` from the loop counter (`1-`). Otherwise the IP moves west and executes the following:
```
    @.-1-~'$$<
_            ^:!
```
By removing the IP movement commands and writing everything from left to right, this results in `!:$$'~-1-.@`:  
`!` invert the loop counter again  
`:$$` duplicate the value and pop both copies  
`'~-` subtract `'~' = 126`  
`1-.` subract `1` and print  
`@` terminate the program
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 27 bytes
```
/.(.{8})/;$_=-127+oct"0b$1"
```
[Try it online!](https://tio.run/##K0gtyjH9/19fT0Ov2qJWU99aJd5W19DIXDs/uUTJIEnFUOn/fwMDQyAAESAWBjbkAssZwhQBCYhaAxADRPzLLyjJzM8r/q@bUwAA "Perl 5 – Try It Online")
[Answer]
# [Nim](http://nim-lang.org/), 58 bytes
```
import strutils
echo stdin.readAll[1..8].fromBin[:int]-127
```
[Try it online!](https://tio.run/##y8vM/f8/M7cgv6hEobikqLQkM6eYKzU5Ix/IS8nM0ytKTUxxzMmJNtTTs4jVSyvKz3XKzIu2yswridU1NDL//9/AwBAIQASIhYENAQ "Nim – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 13 bytes
```
Ｉ⁻⍘Φ…Ｓ⁹κ²¦¹²⁷
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwzczr7RYwymxODW4BCiaruGWmVOSWqThXJmck@qckV@g4ZlXUFoCldTUUbAE4mwgNgJiQyNzTU1N6///DQwMgQBEgFgY2PC/blkOAA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
     Ｓ          Input string
    … ⁹         Take first 9 characters
   Φ   κ        Remove first character
  ⍘     ²       Convert from base 2
 ⁻        ¹²⁷   Subtract 127
Ｉ               Cast to string
                Implicitly print
```
[Answer]
# [Canvas](https://github.com/dzaima/Canvas), 13 [bytes](https://github.com/dzaima/Canvas/blob/master/files/chartable.md)
```
［］９＠ｊ｛┤］２┴‾p－
```
[Try it here!](https://dzaima.github.io/Canvas/?u=JXVGRjNCJXVGRjNEJXVGRjE5JXVGRjIwJXVGRjRBJXVGRjVCJXUyNTI0JXVGRjNEJXVGRjEyJXUyNTM0JXUyMDNFcCV1RkYwRA__,i=JTIyMDAxMTExMDExMTAwMTEwMDExMDAxMTAwMTEwMDExMDElMjI_,v=8)
## Explanation
```
[]9@j{⊣]2⊥¯p⊣-
[]              get all prefixes
  9@            9th prefix
    j           remove first element
     {⊣]        cast each element to integer
        2⊥      decode from base 2
          ¯p    127
            -   subtract that from it 
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~26~~ 24 bytes
```
x=>'0b'+x.slice(1,9)-127
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/Z9m@7/C1k7dIEldu0KvOCczOVXDUMdSU9fQyPx/cn5ecX5Oql5OfrpGmoa6gYEhEIAIEAsDG6prav4HAA "JavaScript (Node.js) – Try It Online")
]