# 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 Jul 19 '17 at 0:31
• If my language supports types and has boolean can I use 1's (int) as truthy? – LiefdeWen Jul 19 '17 at 0:47
• @LiefdeWen of course – Graviton Jul 19 '17 at 5:59
• No longer duplicate as truthy/falsy inputs can be differen't for each answer/language. – Graviton Jul 19 '17 at 6:04
• I don't see why but huh, okay ~ – V. Courtois Jul 19 '17 at 10:10

# Python 3, 332518 15 bytes

Thanks @jurjen-bos for the __mod__ tip.

'%.32d'.__mod__


Try it online!

• Would lambda b:'%0.32d'%b work? – ბიმო Jul 19 '17 at 0:39
• Oh, is None truthy or falsy? – ბიმო Jul 19 '17 at 0:41
• @BruceForte Falsey – rosslh Jul 19 '17 at 0:42
• also 25 bytes, but for python3.6+: lambda b:f'{bool(b):032}' or lambda b:f'{not b<1:032}' – Felipe Nardi Batista Jul 19 '17 at 10:46
• You can save 1 byte by removing the leading zero in 0.32d. – GarethPW Jul 19 '17 at 14:48

# x86-16 Machine Code (DOS), 16 bytes

B4 02          mov  ah,  2
B2 30          mov  dl, '0'
B9 1F 00       mov  cx, 31

PrintZeros:
CD 21          int  0x21
E2 FC          loop PrintZeros

00 CA          add  dl, bl
CD 21          int  0x21
C3             ret


The above function receives a boolean value (0 == falsey, 1 == truthy) in the BL register (low byte of BX), and prints a "redundant boolean" string to the standard output.

It works by invoking an interrupt (0x21) to make a DOS function call (selected by setting AH to 2) that prints a single character (in DL) to the standard output.

First, the ASCII character '0' is loaded into DL, the counter (CX) is set to 31, and it loops to print the "redundant" bytes. Then, the input boolean value is added to DL (if BL is falsey, adding 0 will leave DL unchanged as ASCII '0'; if BL is truthy, DL will be incremented by one to ASCII '1'), and the final byte is printed.

The function does not return a value.

Pretty decent for a language that doesn't really do strings.

### Full Program, 21 bytes

If you want to make it into a full program, only 5 more bytes are required. Instead of passing the input in a register, this reads the input from the arguments passed on the command line when invoking the application. An argument of 0 is interpreted as falsey, as is the complete lack of arguments; an argument greater than 0 is interpreted as truthy.

Simply assemble the following code as a COM program, and then execute it on the command line.

B4 02            mov   ah,  2
B2 30            mov   dl, '0'
B9 1F 00         mov   cx, 31

PrintZeros:
CD 21            int   0x21
E2 FC            loop  PrintZeros

3A 16 82 00      cmp   dl, BYTE PTR [0x82]  ; compare to 2nd arg, at offset 0x82 in PSP
D6               salc                       ; equivalent to sbb al, al
28 C2            sub   dl, al
CD 21            int   0x21
C3               ret                        ; you can simply 'ret' to end a COM program


Sample Output:

C:\>bool.com
00000000000000000000000000000000
C:\>bool.com 0
00000000000000000000000000000000
C:\>bool.com 1
00000000000000000000000000000001
C:\>bool.com 2
00000000000000000000000000000001
C:\>bool.com 7
00000000000000000000000000000001


How does it work? Well, it's basically the same thing, until you get down to the CMP instruction. This compares the command-line argument with the value of the DL register (which, you recall, contains an ASCII '0'). In a COM program, the bytes of code are loaded at offset 0x100. Preceding that is the program segment prefix (PSP), which contains information about the state of a DOS program. Specifically, at offset 0x82, you find the first (actually the second, since the first is a space) argument that was specified on the command line when the program was invoked. So, we are just comparing this byte against an ASCII '0'.

The comparison sets the flags, and then the SALC instruction (an undocumented opcode prior to the Pentium, equivalent to sbb al, al, but only 1 byte instead of 2) sets AL to 0 if the two values were equal, or -1 if they were different. It is then obvious that when we subtract AL from DL, this results in either ASCII '0' or '1', as appropriate.

(Note that, somewhat ironically, you will break it if you pass an argument with a leading 0 on the command line, since it looks only at the first character. So 01 will be treated as falsey. :-)

# Python 3, 23 bytes

lambda n:'0'*31+'01'[n]


Try it online!

• '10'[not n] would suffice if those need to be supported. – Anders Kaseorg Jul 19 '17 at 2:41
• '{:032}'.format for 15 bytes and work the same as your current solution – Felipe Nardi Batista Jul 19 '17 at 10:40
• @rosslh bool(n) would be sufficient – Felipe Nardi Batista Jul 19 '17 at 10:42

# Javascript, 23 bytes

a=>'0'.repeat(31)+ +!!a


!!a coerces a into a boolean, which the unary plus turns into an int.

• a=>'0'.repeat(31)+(+a) is one byte shorter. – Cows quack Jul 20 '17 at 9:09
• @Cowsquack that fails on strings, empty arrays, NaN,functions, and other values where the coercion into an number doesn't result in 0 or 1 – SuperStormer Jul 20 '17 at 11:23
• also empty objects, arrays, Infinity and undefined – SuperStormer Jul 20 '17 at 11:29
• ... but now ... Your program doesn't have to work for every truthy/falsy type, only what your programming language work best for. – edc65 Jul 20 '17 at 12:32
• a=>'0'.repeat(31)+~~a does work with true,false,1,0 – edc65 Jul 20 '17 at 12:40

# V, 8 bytes

32é0Àñl


Try it online!

Explanation:

32é0            " Insert 32 '0's
Àñ          " Arg1 times...
<C-a>     "   Increment the number under the cursor
l    "   Move one char to the right. This will break the loop since there is
"   no more room on this line


# Neim, 6 5 bytes

ᛝΨβ_I


Try it online!

Explanation:

 ᛝ        # Constant 31
Ψ       # Apply next token to all in list
β     # Constant 0
_    # Push each element to stack
I   # Take Line of input.


Saved a byte thanks to Okx

• 5 bytes - ᛝΨβ_I – Okx Jul 19 '17 at 10:33
• ᛝ isn't on the wiki, where did you find it? – LiefdeWen Jul 19 '17 at 10:50
• It's on the list of variables – Okx Jul 19 '17 at 11:43
• @Okx Okay, thanks for tip. – LiefdeWen Jul 19 '17 at 12:19

# Octave, 17 bytes

@(n)dec2bin(n,32)


Anonymous function. Works in MATLAB too.

Try it online!

• I'll be damned... – Stewie Griffin Jul 19 '17 at 11:09

# ArnoldC, 369 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE A
YOU SET US UP 0
GET YOUR ASS TO MARS A
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
BECAUSE I'M GOING TO SAY PLEASE A
TALK TO THE HAND "00000000000000000000000000000001"
BULLSHIT
TALK TO THE HAND "00000000000000000000000000000000"
YOU HAVE NO RESPECT FOR LOGIC
YOU HAVE BEEN TERMINATED


Try it online!

• Welcome to PPCG! – Stephen Jul 20 '17 at 1:08

# Brainfuck, 6160 36 bytes

++++[>++++<-]>[>++>+++<<-]>-[>.<-]>>,.


I'm sure there is a clever way of not moving around so much with the pointers.

I was right. There were. Thanks to @Graviton for giving me the idea!

Next step: Get the values 32 and 48 quicker!

Try it online!

++++        - Increment 1st slot by 4
[           - Loop until 4 becomes 0
>++++   - Add 4 to 2nd slot
<-      - Decrement loop
]           - At this point, we point to slot 1 and slot 2 contains 16, which is the Greatest Common Divisor of 48 (value 0 in ASCII) and 32 (final length of answer)
>           - Point to value 16 (slot 2)
[           - Start loop to get our target values 32 and 48
>++     - Point to slot 3 and multiply 16 by 2 = 32
>+++    - Point to slot 4 and multiply 16 by 3 = 48
<<-     - Decrement the loop so that slot 2 becomes 0
]           - We now point slot 2
>-          - Move to slot 3 and remove one so we can spam (output) 31 zeroes
[           - Start outputting until slot 3 is empty
>.      - Move to slot 4 where our ASCII value for 0 is
<-      - Decrement the loop so that slot 3 becomes 0
]           - We are now at slot 3 and it is empty.
,.          - We can now gather input from the user and output it.


Was fun for a first Golf!

It has gotten way too late now. What am I even doing

• Thanks to entropy, I made it 1 byte shorter to get to the number 16. – Raphaël Côté Jul 20 '17 at 6:03
• Just for fun here's a 60 byte version: >-[-[-<]>>+<]>--<<-[>+<-----]>--->[-<.>],. (takes input as 0 or 1) Try It Online! – Graviton Jul 20 '17 at 6:17
• Well, thanks so much @Graviton. You made me realize that I was putting way too much effort in dropping the ASCII value to 0 while I just had to output it. – Raphaël Côté Jul 20 '17 at 6:35

# Scala, 32 bytes

Sorry but I was forced to do it in 32 bytes >_<

var s=t
for(u<-0 to 30)s="0"+s
s


It's enclosed by a function taking t as parameter (as a string that can be "0" or "1" for resp. falsy or truthy), and s is returned.

Try It Online!

# Valid response : Scala, 46 bytes

Same as my java response, I was supposed to take a boolean for the parameter. So :

var s=if(t)"1"else"0"
for(u<-0 to 30)s="0"+s
s


Try It Online!

# Braingolf, 10 8 bytes

#␟>[0_]N


Try it online!

␟ is a Unit Separator, ASCII 0x1F, or 31. Can't find the actually character to paste into TIO, so TIO instead uses # 1-, which pushes space (32) and decrements to 31.

## Explanation

#␟>[0_]N  Implicit input from commandline args
#␟        Push unit separator (31)
>       Move top item to bottom of stack
[..]   Loop, runs 31 times
0_    Print 0
N  Boolean conversion, truthy values become 1, falsey values become 0
Implicit output of top of stack

• Here is the tio link with the 0x1F character in it TIO – PunPun1000 Jul 20 '17 at 12:27
• @PunPun1000 Oh thanks! I'll update the post – Skidsdev Jul 20 '17 at 12:37

# Octave, 23 bytes

@(x)[48+[!(1:31),x],'']


Try it online!

This is shorter than all the approaches I tried with printf. I might have missed something though, since I did this on my phone.

Only one byte longer

@(x)[dec2bin(0,31),x+48]


This could be 18 bytes if I could take 1/0 as strings.

@(x)[48+!(1:31),x]


Try it online!

# Java 8, 31 27 bytes

b->"".format("%032d",b?1:0)


-4 bytes thanks to @OlivierGrégoire.

Try it here.

• Yep, still here to outgolf you: "".format ;) – Olivier Grégoire Jul 19 '17 at 10:51
• @OlivierGrégoire That was it! Damn I'm stupid.. xD When I was typing String.format I knew there was a shorter way somehow, but I thought I probably used a String variable last time... Thanks. ;) – Kevin Cruijssen Jul 19 '17 at 10:54

# 05AB1E, 5 bytes

¾31×«


Try it online!

• Smarter than 31úð0:! – Magic Octopus Urn Jul 19 '17 at 17:16

# Ruby, 21 bytes

Yeah, that space needs to be there.. :/

->x{"%032b"%(x ?1:0)}


In Ruby everything except false and nil is truthy; Try it online!

# Mathematica, 36 bytes

""<>ToString/@PadLeft[{Boole@#},31]&


# Mathematica, 26 bytes

Row@PadLeft[{Boole@#},31]&


Try it online!

# Charcoal, 5 bytes

×0³¹Ｓ


Try it online! (Link to the verbose version.)

As Charcoal understand 0 and 1 as True or False, this just prints 31 0s and the input (0 or 1) as string.

• :P That's actually correct, I thought I made the variables stringify to True though, weird – ASCII-only Jul 27 '17 at 1:48

# Python 2, 26 25 bytes

Thanks to Erik the Outgolfer for saving a byte!

Goes for the full program approach:

print+bool(input(31*'0'))


Try it online!

# Unwanted, Unnecessary, Opportunistic, 7 bytes

P0M31P*


Pass either 1 or 0 into the register.

# C, 26 bytes

Pretty much the same idea as 1bluestone's solution, but in C it's shorter, and it does work correctly for any integer input:

f(a){printf("%032i",!!a);}


Of course, this includes some implicitly typed variables/functions as all good C-golf answers do... The !! operator is the shortest way to convert any truthy value to 1 in C (via double negation, ! is defined to return either 1 or 0).

Test with:

#include <stdio.h>
f(a){printf("%032i",!!a);}
int main() {
f(0), printf("\n");
f(1), printf("\n");
f(2), printf("\n");
f(-1), printf("\n");
}


# Haskell, 37 32 bytes

(('0'<$[1..31])++).show.fromEnum  Try it online! Thanks @nimi for -5 bytes! • show(fromEnum x) instead of last(...). Pointfree even shorter: (('0'<$[1..31])++).show.fromEnum. – nimi Jul 19 '17 at 15:09
• @nimi Didn't know that Bool is an Enum, thanks! – ბიმო Jul 19 '17 at 15:34

# PHP, 28 bytes

<?=str_pad($argv[1],32,0,0);  Save as bool.php and run: $ php bool.php 0
00000000000000000000000000000000
$php bool.php 1 00000000000000000000000000000001  • 3 bytes shorter: printf('%032d',$argv[1]); (requires the -r flag). – user63956 Jul 20 '17 at 1:19

# Ly, 2015 13 bytes

65*1+[0u1-]nu


EDIT: Saved 5 bytes thanks to ovs.
EDIT: Saved another 2 bytes by printing 0 as a number rather than a character.

• Would 65*1+["0"o1-]nu work? – ovs Jul 19 '17 at 5:50

# Octave,16 11 bytes

@(x)x(1:32)


Try it online!

A function handle that takes "00000000000000000000000000000001" as truthy and "00000000000000000000000000000000\0" as falsey.

Explanation:

In Octave an array is considered as falsey if at least one of its elements is zero. The 33th element of the second string is a character with the ASCII value of 0 so it can be considered as falsey.

# Java, 40 chars, 40 bytes

This takes string as parameter, which is not correct (falsy/truthy java value is forced by OP to be represented by a boolean).

c->{for(int i=0;++i<32;c=0+c);return c;}


Try It Online!

# Valid response : Java, 60 chars, 60 bytes

c->{String k="";for(k+=c?1:0;k.length()<32;k=0+k);return k;}


Try It Online!

I know there is already a Java answer which is shorter than this one, but still :)

• Yes, the return statement is part of your code, so part of your byte-count. But your answer doesn't fulfill the rules: you must get a boolean as input. "Truthy/falsy" is translated in Java as either true or false, and nothing else. So you can't get a String as input parameter. – Olivier Grégoire Jul 19 '17 at 21:12
• I see. I'll modify it soon enough. Thanks. – V. Courtois Jul 19 '17 at 21:49
• You don't need to put the final semicolon (;). Also, you can shorten your code like this: c->{String k="";for(;k.length()<31;)k+=0;return k+=c?1:0;}. The k+=c?1:0 is to shorten k+(c?1:0). – Olivier Grégoire Jul 20 '17 at 9:37
• @OlivierGrégoire Thanks but why isn't the final semicolon mandatory? – V. Courtois Jul 20 '17 at 10:23
• It is mandatory, in the code, but not in the snippet. In the TIO, the footer can simply start with ;. A statement requires a semicolon. A lambda is not a statement. – Olivier Grégoire Jul 20 '17 at 12:02

# Japt, 13 11 bytes

?1:0 ¤i31ç0


## Explanation:

?1:0 ¤i31ç0
?              // If the input is a truthy, return:
1             //   1
:0           //   Else, 0
¤         // Convert to a base-2 string
i        // Insert at index 0:
31ç0    //   A string filled with 0s, length 31


Saved a byte using a base-2 conversion built-in!

To insert the string of 0s in front of the 1/0, I need to cast the 1 and 0 into a string. The typical way of doing that would be 1s  (3 bytes). But because we're only converting 1s and 0s, I can use the base-2 built-in 1¤ (2 bytes).

Input can be in the form of an integer or string.

0 and "" are falsy in Japt.

Try it online!

Test suite

# C# (.NET Core), 29 bytes

a=>new string('0',31)+(a?1:0)


OP said 1/0 can be used for truthy/falsey, so can make a an int and it becomes

a=>new string('0',31)+a


C# doesn't really have truthy/falsey though so I will not use this answer.

Try it online!

• I don't think the second one is valid. Integers are neither truthy nor falsey in C#, only bools are. – Skidsdev Jul 19 '17 at 9:02
• @Mayube I did ask and OP said its okay, but I kinda agree with you so will edit. – LiefdeWen Jul 19 '17 at 9:07
• Annoyingly Padleft comes in at the same byte count here. – TheLethalCoder Jul 19 '17 at 9:12
• @TheLethalCoder Started with PadLeft as well :) – LiefdeWen Jul 19 '17 at 9:34
• Not sure if this is possible, but interpolated strings can be quite useful here: b=>\$"{(b?1:0):D32}" 19 bytes – auhmaan Jul 19 '17 at 14:14

# MY, 10 9 bytes

𝕫BṄiℑpέ←←


Try it online!

Explanation (codepage [with reasoning behind the character]/hex code):

𝕫: 1A - Push an integer from STDIN (lowercase integers)
B: 0B - Push 11 (B is 11 in hex)
Ṅ: 36 - Pop n; push the nth prime, 11th prime is 31 (Ṅ-th)
i: 49 - Pop n; push [1,...,n] (index/iota)
ℑ: 34 - Pop n; push imag(n) (ℑmaginary part, applied to each element, which gives 0 for real numbers)
p: 60 - Pop n; push stringified n (0=>"0" ... 35=>"Z", the b in base upside down)
έ: 56 - Pop n; push n joined by "" (έmpty string)
←: 26 - Pop n; output n with no newline (out←in)
←: 26 - Pop n; output n with no newline (out←in)


I can't believe that this is possible without any two-argument commands whatsoever!

Edit: Saved 1 byte by using primes instead of arithmetic to get 31.

# 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 – Cows quack Jul 19 '17 at 13:10
• @Cowsquack & Uriel Neither works as they include spaces. You need ∊⍕¨¯32↑⎕ or something. – Adám Jul 19 '17 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? – Oliver Jul 19 '17 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. – Jonah Jul 19 '17 at 1:06