New answers tagged

2

Stax, 14 bytes ò≥úû╙2ï⌡T5è╧²■ Run and debug it


0

Zsh, 20 bytes <<<$[0b${n:1:8}-127] Try it online!


2

brainfuck, 174 113 bytes -61 bytes for many optimizations ,>++++++++>>--[>-<--]>-<<<[>>>>[--<+>]<[->+<]<<,>-[<->-----]<+++[->>>[->+>+<<]>>[-<<+>>]<<<<<]<-]>>>>---[>-<--]>. Try it online! Input is 32 bits taken ...


0

Perl 5 -MList::Util=max -a, 64 bytes say$"x((length max@F)-y///c),y/0/ /r for@F=map{sprintf"%b",$_}@F Try it online!


1

Japt, 15 bytes Returns 0 for falsey. ¤ôÍmÊÍf Ê*UeUÊõ Try it or run all test cases ¤ôÍmÊÍf\nÊ*UeUÊõ :Implicit input of integer > 17602 ¤ :To binary string > "100010011000010" ô :Split at elements that return truthy Í : When ...


2

Java (JDK), 126 117 116 bytes Saved 1 byte thanks to ceilingcat Saved 6 bytes - 9, really - thanks to Arnauld q->{int C[]=new int[9],s=0,n=0;for(;q>0;q/=2)C[s]-=q%2<1?(n=++s>n?s:n)-n:~(s=0);while(q++<n)n=C[q]!=1?0:n;return n;} Try it online! Returns 0 for non-Neil numbers. I feel like this should be smaller, even though it is in Java. ...


0

Scala 2.12, 47 bytes _ split " "map(_./:(0)(_*2+_-48)toChar)mkString Try it online! Splits on spaces, maps each string representing a binary number to an integer, turns into a Char, and then joins all those characters together. Scala 2.12 is required because of postfix operators.


0

MUMPS, 77 76 bytes r s,! f i=1:1:$l(s) s r=$a($e(s,i)) s b=" " f s b=(r#2)_b,r=r\2 i r=0 w b q Example REPL > r s w ! f i=1:1:$l(s) s r=$a($e(s,i)) s b=" " f s b=(r#2)_b,r=r\2 i r=0 w b q Hello World! 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 100001 Explanation M is great at keeping ...


0

Rust, 40 bytes |s|for c in s.bytes(){print!("{:b} ",c)} Try it online! An anonymous function that takes in an &str, iterates over bytes, and prints them as a binary string.


2

Nim, 58 bytes import strutils echo stdin.readAll[1..8].fromBin[:int]-127 Try it online!


1

05AB1E, 2 bytes Çb Try it online! Ç # list of ASCII values of... # implicit input... b # in binary # implicit output


2

Rust, 43 bytes |s|Ok(i32::from_str_radix(&s[1..9],2)?-127) 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.


0

Husk, 7 bytes wm(ṁsḋc Try it online!


4

Vyxal, 11 6 bytes 9ūBª⎣- 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 ...


4

Bash, 22 bytes echo $[2#${1:1:8}-127] Try it online! Bash, 37 27 bytes Input is from STDIN, output is to STDOUT. expr $[2#`cut -b2-9`] - 127 Try it online! Explanation expr # Evaluate the following: `cut -b2-9` # Slice all characters from the 2nd character # to the ...


3

Forth (gforth), 49 bytes : b drop 1+ 8 ['] evaluate 2 base-execute 127 - ; 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).


4

Desmos, 42+9=51 40+9=49 bytes Expression 1, 42 40 bytes: f(l)=\operatorname{total}(l[9-b]2^b)-127 Expression 2, 9 bytes: b=[0...7] Input as a list of 1's and 0's. Try It On Desmos!


1

Husk, 11 bytes mḋTmOT0mo↔ḋ Try it online!


1

Japt, 9 bytes m¤ù yñ mÍ Try it m¤ù yñ mÍ :Implicit input of array m :Map ¤ : To binary strings ù :Left pad each with spaces to the length of the longest y :Transpose ñ :Sort each and transpose back m :Map Í : To decimal


2

PowerShell, 98 bytes $p=$args|%{$n=$_;31..0|?{($n-shr$_)%2}}|sort $args|%{$n=$_;-join($p[-1]..0|%{' *'[($n-shr$_)%2]})} Try it online! The script uses the fact that an integer contains no more than 32 bits. The first line calculates positions of the bits with 1. The second line renders a binary wall.


0

Canvas, 9 bytes H2┬!0∙ ╋∔ Try it here! Uses 1 as the display character.


1

Husk, 18 17 bytes #oṗḋużoJ1eḣḋ¹mtṫḋ Try it online! Different approach to Razetime's Husk answer, saving 2 3 bytes. How? #oṗḋużoJ1eḣḋ¹mtṫḋ #o # how many ṗ # prime numbers ḋ # among numbers formed by binary digits of u # unique elements of żo # zip ...


3

><>, 30 bytes r~8[01:@{2%*+$2*60l4)?.{""-n; (contains an unprintable DEL between the quotes) Try it Online! r~8[ grab relevant part of string 01 accumulator and exponent initialization main loop @{2% convert current digit charcter ...


2

Perl 5 -p, 27 bytes /.(.{8})/;$_=-127+oct"0b$1" Try it online!


1

C# (Visual C# Interactive Compiler), 40 bytes Convert.ToInt32(s.Substring(1,8),2)-127; Try it online!


1

k4, 9 bytes " *"@+2\: Takes input as a list of integers, outputs as a list of strings. 2\: convert to base 2 representation + transpose result " *"@ replace 0's with and 1's with *


0

Japt, 7 bytes Outputs an array of lines, using 1 for the bricks ®¤r0SÃù Try it (footer replaces 1s with *s) or run all test cases ®¤r0SÃù :Implicit input of array ® :Map ¤ : To binary string r0 : Replace 0s with S : Space à :End map ù :Left pad each with spaces to the length of the longest


6

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 ...


1

Julia 0.7, 25 bytes n->parse("0b"*n[2:9])-127 Try it online!


2

Befunge-98 (PyFunge), 31 bytes ~$7v@.-1-~'$$< _1->\2*~2%+\#^:! Try it online! Animation 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- ...


3

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!...


3

Scala, 27 bytes Uses Lynn's clever approach _.slice(1,9)reduce(_*2+_-1) Try it online Scala, 29 bytes n=>BigInt(n.slice(1,9),2)-127 Try it online! 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 ...


8

Haskell, 33 bytes foldl1(\x y->x*2+y-1).take 8.tail 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 ...


1

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


1

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 ...


2

K (ngn/k), 11 bytes -127+2/8#1_ Try it online! 1_ drop one 8# take 8 2/ to binary -127+ subtract 127 K (ngn/k), 14 bytes Just an experiment - a port of Lynn's Haskell answer {y-1+2*x}/8#1_ Try it online!


3

J, 12 bytes 127-~1{8#.\] 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


1

Japt, 8 bytes Note the unprintable (charcode 127) at the end. ů8 Í-# Try it


4

Kotlin,  26   31  29 bytes Converted to a valid anonymous function, thanks to Bubbler -2 bytes thanks to user {it.slice(1..8).toInt(2)-127} Try it online!


5

Ruby -n, 24 23 20 bytes -1 byte thanks to Sisyphus -3 bytes using -n command-line flag, inspired by this answer p$_[1,8].to_i(2)-127 Try it online!


1

Canvas, 13 bytes []9@j{┤]2┴‾p- Try it here! 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


2

Jelly, 8 bytes Ḋḣ8Ḅ_127 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


1

JavaScript (Node.js),  26  24 bytes x=>'0b'+x.slice(1,9)-127 Try it online!


3

Retina 0.8.2, 63 bytes 2=M&!`.{8} 1 01 +`10 011 $ -127$* (1*)-\1 - ^0*(-)?(1*)-? $1$.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 0s). $ -127$* (1*)-\1 - Subtract 127 (may leave trailing -). ^0*(-)?(1*)-? $1$.2 Convert to decimal, removing garbage.


7

C (gcc), 55 \$\cdots\$ 40 36 bytes Saved 2 6 bytes thanks to ceilingcat!!! Saved 2 a whopping 13 bytes thanks to Arnauld!!! #define f(s)strtol(s+1,s[9]=0,2)-127 Try it online!


1

Charcoal, 13 bytes I⁻⍘Φ…S⁹겦¹²⁷ Try it online! Link is to verbose version of code. Explanation: S Input string … ⁹ Take first 9 characters Φ κ Remove first character ⍘ ² Convert from base 2 ⁻ ¹²⁷ Subtract 127 I Cast to string Implicitly print


5

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.


5

MathGolf, 7 bytes ╞8<å♣(- Try it online. 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 ...


5

05AB1E, 8 7 bytes ¦8£CƵQ- 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


6

Python 3, 26 bytes lambda s:int(s[1:9],2)-127 Try it online!


Top 50 recent answers are included