# 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 code-golf, so lowest bytes wins!

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

# Starry, 82 bytes

     +          + + +  **      +*      + *  + +.  + +', '.      +  '       +.  

5+                 Push 0
10+1+1+2*0*6+0*    Push 31 (5*(5+1)+1)
0                 [Label 0]
6+1*             Subtract 1 from top of stack
2+1+0.           Print '0'
2+1+0'           If top is not zero, goto label 0
0,1'               If input is non-zero, goto label 1
0.6+2'           Print '0', goto label 2
16+0.           [Label 1] print '1'
2                 [Label 2]


(The number before each symbol represents the number of spaces)

It's pretty straightforward: print 31 zeroes, then print zero if the input is zero, or one if the input is non-zero. Normally, you would need to push a zero to print on the eighth line, but I just used one the zero from the first half of the program. The [Label 2] at the end is important so that the falsey case doesn't fall into the truthy case, and instead jumps to the end.

Try it online!

# Vyxal, 8 5 bytes

S32↳›


Try it Online!

• Try it Online! for 5 bytes Commented Oct 15, 2022 at 11:37

# APL, 11 bytes

⍕¯32↑1↑1\⍨⊢


How?

1\⍨⊢ - repeat 1 input times - return empty array on falsy value

1↑ - take the first item

¯32↑ - align right with 31 zeros

⍕ - format as string

• Simply ⍕¯32↑⊢ should work Commented Jul 19, 2017 at 13:10
• @Cowsquack & Uriel Neither works as they include spaces. You need ∊⍕¨¯32↑⎕ or something.
Commented Jul 19, 2017 at 17:28

# J, 11 bytes

(31#'0'),":


Try it online!

## how?

31 zeros
00...000  append     turn the 0 or 1 input into string
(31#'0')    ,       ":


note: in J, booleans are 0 or 1, also known as "the iverson convention," after ken iverson, creator of J and APL

• I understand that 1 and 0 are booleans in J, but you're just appending the input to 31 0s. Shouldn't there be some form of boolean validation? Commented Jul 19, 2017 at 1:05
• @Oliver The problem specifies "Given a truthy/falsey value..." so no, I don't think you should be validating... Also, note that ": is required for this to work -- it changes a boolean to a string. Commented Jul 19, 2017 at 1:06

# Jelly, 8 bytes

31”0x⁸ṆṆ


Try it online!

# 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


# 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. # 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  # Lua, 30 bytes ("%032d"):format(b and 1 or 0)  Try it online! • i'm afraid that's not currently valid, you need to submit a full program or function like here Commented Jul 19, 2017 at 10:55 # Clojure, 28 bytes #(format "%031d" (if % 1 0))  Truthy is everything but nil and false # JavaScript, 25 bytes v=>'0'.repeat(31)+(v?1:0)  31 zeroes and conditionally add a 1 or 0. # ><>, 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  # Excel, 2220 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. • Can drop quotation marks for 2 bytes. =REPT(0,31)&1*A1 also handles falsy Empty cell. Commented Jul 19, 2017 at 12:52 # 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.&!!.@  # 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 ## 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. # 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.  • You could do 31T instead of GF+0 to save 1 byte Commented Jul 19, 2017 at 17:07 • @BusinessCat Thanks, definite overthinking there! Commented Jul 19, 2017 at 17:18 # 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. # 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.  # 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 ~. # 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! ## 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"


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

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


Try it online!

# 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}

• b=>'0'.repeat(31)+(+b) is three bytes shorter. Commented Jul 20, 2017 at 6:18
• @Cowsquack That's already done here; I decided to just demonstrate a different method. Commented Jul 20, 2017 at 9:06

# AWK, 20 bytes

{printf"%032d",!!$0}  Try it online! Here, to deal with multiple lines of input, I added \n to the printf modifier. { } For every input, printf"% ", prints, without a trailing newline, ​0 with leading zeros, 32d a 32-digit number, !!$0   and this number is the negative of the negative of the input.
In awk, only zero(s) and blank lines return false.
Anything else return true.
See examples in the TIO link above.


Another working code, with 24 bytes: $0=sprintf("%032d",!!$0)

# Carrot, 7 bytes

0^*30^#


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


# Thunno 2, 4 bytes

Ṙ32ṛ


Attempt This Online!

Input as a Python boolean: True or False.

#### Explanation

      # Implicit input.
# The interpreter implicitly converts
# True and False to 1 and 0, since
# integers look better than booleans.
Ṙ     # Convert this integer into a string.
32ṛ  # And pad it with zeros on the left
# until the length is equal to 32.
# Implicit output.


## Pascal, 122 B

This is a function written in Extended Pascal as laid out in ISO standard 10206. In Pascal you cannot read/readLn values of the data type Boolean, only char, integer and real. Therefore a function is used.

type s=packed array[1..32]of char;function f(x:Boolean)=r:s;var i:1..32;begin
for i:=1 to 32 do r[i]:=succ('0',ord(x))end;


Full working example program:

program theRedundantBoolean(output);

const
duotrigesimalCharMaximum = 32;

type
duotrigesimalCharDimension = 1‥duotrigesimalCharMaximum;
{ In Pascal, a packed array[1‥N] of char is a string data type. }
duotrigesimalChar = packed array[duotrigesimalCharDimension] of char;

function redundantBitString(
protected truth: Boolean
) = result: duotrigesimalChar;
var
charIndex: duotrigesimalCharDimension;
begin
for charIndex ≔ 1 to duotrigesimalCharMaximum do
begin
result[charIndex] ≔ succ('0', ord(truth))
end
end;

begin
writeLn(redundantBitString(false));
writeLn(redundantBitString(true))
end.


Output:

00000000000000000000000000000000
11111111111111111111111111111111
`