19
\$\begingroup\$

Introduction

Classically, booleans are one bit; true or false, 1 or 0. Leading zeros would just be redundant. For example, 001 means the same as 00001 or just 1.

The 32-bit boolean

Given a truthy/falsey value, output the equivalent 32-bit boolean as a string. (Or as a number if for some reason your language supports leading zeros.)

Your program doesn't have to work for every truthy/falsy type, only what your programming language work best for.

Example i/o

Input >> Output

truthy >> 00000000000000000000000000000001
falsey >> 00000000000000000000000000000000

This is , so lowest bytes wins!

\$\endgroup\$
  • 6
    \$\begingroup\$ Do we need to handle any possible truthy or falsey value, or just booleans? \$\endgroup\$ – xnor Jul 19 '17 at 0:31
  • \$\begingroup\$ If my language supports types and has boolean can I use 1's (int) as truthy? \$\endgroup\$ – LiefdeWen Jul 19 '17 at 0:47
  • \$\begingroup\$ @LiefdeWen of course \$\endgroup\$ – Graviton Jul 19 '17 at 5:59
  • 1
    \$\begingroup\$ No longer duplicate as truthy/falsy inputs can be differen't for each answer/language. \$\endgroup\$ – Graviton Jul 19 '17 at 6:04
  • \$\begingroup\$ I don't see why but huh, okay ~ \$\endgroup\$ – V. Courtois Jul 19 '17 at 10:10

53 Answers 53

0
\$\begingroup\$

Jelly, 8 bytes

31”0x⁸ṆṆ

Try it online!

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

Pyth, 9 bytes

+*31\0-1!

Test suite

Explanation:

+*31\0-1!
+*31\0-1!Q | Implicit Q (input)
 *31\0     | repeat the character '0' 31 times
        !Q | logical negation of input
      -1   | subtract from 1
+          | concatenate
\$\endgroup\$
0
\$\begingroup\$

Carrot, 7 bytes (Non-competing)

0^*30^#

Non-competing because there is no truthy/falsey type in Carrot at the moment. However, 1 and 0 are usually considered to be truthy/falsey so that is how I am expecting the input.

Explanation:

0   //Set the string stack to 0
^   //Change to operations mode
*30 //Duplicate the string stack 30 times
^   //Convert to string stack mode
#   //Add the input to the end of the stack
    //Implicitly output the string stack
\$\endgroup\$
0
\$\begingroup\$

Retina, 13 bytes

Ms`.+
^
31$*0

Try it online! Assumes empty input is falsy and everything else is truthy, which is about all you can do when your only type is string.

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

MATL, 5 bytes

32&YB

Uses inputs 1/ 0, or T/ F.

Try it online!

Explanation

32     % Push 32
&YB    % Convert implicit input to binary with 32 digits. Gives char output
\$\endgroup\$
0
\$\begingroup\$

Lua, 30 bytes

("%032d"):format(b and 1 or 0)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ i'm afraid that's not currently valid, you need to submit a full program or function like here \$\endgroup\$ – Felipe Nardi Batista Jul 19 '17 at 10:55
0
\$\begingroup\$

Clojure, 28 bytes

#(format "%031d" (if % 1 0))

Truthy is everything but nil and false

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

JavaScript, 25 bytes

v=>'0'.repeat(31)+(v?1:0)

31 zeroes and conditionally add a 1 or 0.

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

><>, 18 16 bytes

<n0v?)*3a:l
;oi<

Try it online!

Explanation

<n0v?)*3a:l
;oi<
<              | Move the direction left
         :l    | Push the length of the stack onto the stack and duplicate it
      *3a      | Push 10*3 onto the stack
     )         | Compare the stack top item is greater than the next stack item, push 1 is true else 0
   v?          | Check if stack top is 0, if 0 then jump else go to line 2
 n0            | Print 0 (this will then loop line 1 again)
;oi<           | Take input (1 byte), print input then end program
\$\endgroup\$
0
\$\begingroup\$

C++14, 29 bytes

[](int i){printf("%032d",i);}

At least bools are treated as 1 and 0 anyway :P

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

Excel, 22 20 18 bytes

Saved two bytes thanks to Wernisch and my own lack of critical analysis before posting.
Saved two bytes by changing the formula completely

=BASE(OR(A1),2,32)

It feels like there should be a shorter answer than this but I can't think of one.
I found the shorter function that I felt must exist.

\$\endgroup\$
  • \$\begingroup\$ Can drop quotation marks for 2 bytes. =REPT(0,31)&1*A1 also handles falsy Empty cell. \$\endgroup\$ – Wernisch Jul 19 '17 at 12:52
0
\$\begingroup\$

Befunge-98, 16 bytes

'0'k:'k,&!!+,@

Try it online!

Hexdump:

00000000: 2730 271e 6b3a 271e 6b2c 2621 212b 2c40  '0'.k:'.k,&!!+,@

output as space sperated digits, 9 bytes:

'k.&!!.@

Try it online!

Hexdump:

00000000: 271e 6b2e 2621 212e 40                   '.k.&!!.@
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 71 bytes

(defun f(i)(format t "~v@{~A~:*~}" 31 0)(prin1(cond((null i) 0)(t 1))))

Try it online!

Because nil is a falsey value and anything else is truthy:

(f nil) = 00000000000000000000000000000000

(f 123) = 00000000000000000000000000000001

(f t) = 00000000000000000000000000000001

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

Dyvil v0.33.0, 15 Bytes

b=>"0"*31+b?1:0

Usage:

let f: boolean -> String =
b=>"0"*31++b?1:0

print f(true)  // 00000000000000000000000000000001
print f(false) // 00000000000000000000000000000000

Explanation:

Returns the string 0 repeated 31 times followed by 1 for true and 0 for false. This does not work in v0.34.0 anymore because it removes the + operator for Strings.

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

CJam, 6 bytes

q31Te[

Try it Online

Takes 1 for true and 0 for false. pads input with 31 0's on the left.

q     e# read input
31Te[ e# pad input with 31 0's on the left.
\$\endgroup\$
  • \$\begingroup\$ You could do 31T instead of GF+0 to save 1 byte \$\endgroup\$ – Business Cat Jul 19 '17 at 17:07
  • \$\begingroup\$ @BusinessCat Thanks, definite overthinking there! \$\endgroup\$ – geokavel Jul 19 '17 at 17:18
0
\$\begingroup\$

Cubix, 24 bytes

1&u3I!|1^...0O;($\!vuO;@

Nonzero values are truthy in Cubix (as handled by !)

Try it online!

    1 &
    u 3
I ! | 1 ^ . . .
0 O ; ( $ \ ! v
    u O
    ; @

Explanation:

  • I : read in input as a number
  • !| : reflect go left if falsey, proceed if truthy

falsey:

  • I : read in input; it's blank, so push 0

truthy:

  • 1 : push 1

both branches reach here:

  • ^3u1&\ : put 31 to the top of the stack and enter the loop

Loop:

  • !v0O;($\ : if top is zero, go south, otherwise push 0, Output as a number, pop (;), decrement ((), then $kip \ and continue.

Finally:

  • ;uO@ : pop the zero, Output the 0/1, and @ exits.
\$\endgroup\$
0
\$\begingroup\$

QBIC, 21 bytes

[31|?A';`]~:|?!1$\?@0

Explanation

[31|        DO 31 times
?           PRINT
 A          A$, which is later diefined as a literal 0 
            (represented as string, because QBasic spaces out printed numbers)
 ';`        Followed by a semicolon (to prevent newlines, tabs ect)
]           Close the loop
~:          IF the (numeric) input (is anything but 0)
|?!1$       THEN print a literal 1 (cast to string, to prevent the spacing thingy again)
\?@0        ELSE print a 0; Here we finally define A$. We could use it earlier,
            because the complie-process from QBIC to QBasic moves literal declarations to the
            top of the file.
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 11 bytes

'0'x 31~+?*

Try it online!

  • '0' x 31 produces the first 31 zeroes.
  • * is the argument to the function. ? coerces it to a boolean, and then + coerces that boolean into a number, either 0 or 1, which is appended to the leading zeroes with ~.
\$\endgroup\$
0
\$\begingroup\$

Groovy - 17 bytes

A Groovy closure which takes a value and according to "groovy truth" outputs either a falsey or truthy value.

{"0"*31+(it?1:0)}

Explanation

So, all we do is emit a string with 31 zeroes and then append either 0 or 1 onto the end, depending upon whether the input is false or true, according to "groovy truth".

Try it online!

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

K/Kona, 10 bytes

Sadly not as clever as I'd have liked, but it works.

(31#"0"),$

String-casts the input, and staples it to the end of 31 "0" chars

E.g.

k)(31#"0"),$1b
"00000000000000000000000000000001"
k)(31#"0"),$0b
"00000000000000000000000000000000"
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 17 + 1 (-p) = 18 bytes

$_=0 x31 .($_&&1)

Try it online!

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

Julia, 13 bytes

~x=bin(x,32)

Nothing clever here, there is basically a built in for it.

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

JavaScript (ES6), 25 bytes

b=>`${+b}`.padStart(0,32)

A different approach. Takes input as specifically true or false.

Test Snippet

let f=
b=>`${+b}`.padStart(32,0)

console.log(true, f(true))
console.log(false, f(false))
.as-console-wrapper{max-height:100%!important}

\$\endgroup\$
  • \$\begingroup\$ b=>'0'.repeat(31)+(+b) is three bytes shorter. \$\endgroup\$ – Cows quack Jul 20 '17 at 6:18
  • \$\begingroup\$ @Cowsquack That's already done here; I decided to just demonstrate a different method. \$\endgroup\$ – Justin Mariner Jul 20 '17 at 9:06

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.