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

55 Answers 55

11
\$\begingroup\$

Python 3, 33 25 18 15 bytes

Thanks @jurjen-bos for the __mod__ tip.

'%.32d'.__mod__

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Would lambda b:'%0.32d'%b work? \$\endgroup\$ Jul 19, 2017 at 0:39
  • \$\begingroup\$ Oh, is None truthy or falsy? \$\endgroup\$ Jul 19, 2017 at 0:41
  • \$\begingroup\$ @BruceForte Falsey \$\endgroup\$
    – wrymug
    Jul 19, 2017 at 0:42
  • 1
    \$\begingroup\$ also 25 bytes, but for python3.6+: lambda b:f'{bool(b):032}' or lambda b:f'{not b<1:032}' \$\endgroup\$ Jul 19, 2017 at 10:46
  • 1
    \$\begingroup\$ You can save 1 byte by removing the leading zero in 0.32d. \$\endgroup\$
    – GarethPW
    Jul 19, 2017 at 14:48
10
\$\begingroup\$

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

\$\endgroup\$
8
\$\begingroup\$

Python 3, 23 bytes

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

Try it online!

\$\endgroup\$
3
  • 8
    \$\begingroup\$ '10'[not n] would suffice if those need to be supported. \$\endgroup\$ Jul 19, 2017 at 2:41
  • \$\begingroup\$ '{:032}'.format for 15 bytes and work the same as your current solution \$\endgroup\$ Jul 19, 2017 at 10:40
  • \$\begingroup\$ @rosslh bool(n) would be sufficient \$\endgroup\$ Jul 19, 2017 at 10:42
7
\$\begingroup\$

Javascript, 23 bytes

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

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

\$\endgroup\$
5
  • \$\begingroup\$ a=>'0'.repeat(31)+(+a) is one byte shorter. \$\endgroup\$
    – user41805
    Jul 20, 2017 at 9:09
  • \$\begingroup\$ @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 \$\endgroup\$ Jul 20, 2017 at 11:23
  • \$\begingroup\$ also empty objects, arrays, Infinity and undefined \$\endgroup\$ Jul 20, 2017 at 11:29
  • 1
    \$\begingroup\$ ... but now ... Your program doesn't have to work for every truthy/falsy type, only what your programming language work best for. \$\endgroup\$
    – edc65
    Jul 20, 2017 at 12:32
  • 1
    \$\begingroup\$ a=>'0'.repeat(31)+~~a does work with true,false,1,0 \$\endgroup\$
    – edc65
    Jul 20, 2017 at 12:40
6
\$\begingroup\$

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
\$\endgroup\$
5
\$\begingroup\$

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

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

Octave, 17 bytes

@(n)dec2bin(n,32)

Anonymous function. Works in MATLAB too.

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'll be damned... \$\endgroup\$ Jul 19, 2017 at 11:09
4
\$\begingroup\$

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!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – Stephen
    Jul 20, 2017 at 1:08
4
\$\begingroup\$

Brainfuck, 61 60 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

\$\endgroup\$
3
  • \$\begingroup\$ Thanks to entropy, I made it 1 byte shorter to get to the number 16. \$\endgroup\$ Jul 20, 2017 at 6:03
  • \$\begingroup\$ Just for fun here's a 60 byte version: >-[-[-<]>>+<]>--<<-[>+<-----]>--->[-<.>],. (takes input as 0 or 1) Try It Online! \$\endgroup\$
    – Graviton
    Jul 20, 2017 at 6:17
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 20, 2017 at 6:35
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Here is the tio link with the 0x1F character in it TIO \$\endgroup\$
    – PunPun1000
    Jul 20, 2017 at 12:27
  • \$\begingroup\$ @PunPun1000 Oh thanks! I'll update the post \$\endgroup\$
    – Mayube
    Jul 20, 2017 at 12:37
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Java 8, 31 27 bytes

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

-4 bytes thanks to @OlivierGrégoire.

Try it here.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Yep, still here to outgolf you: "".format ;) \$\endgroup\$ Jul 19, 2017 at 10:51
  • 2
    \$\begingroup\$ @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. ;) \$\endgroup\$ Jul 19, 2017 at 10:54
2
\$\begingroup\$

05AB1E, 5 bytes

¾31׫

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Smarter than 31úð0:! \$\endgroup\$ Jul 19, 2017 at 17:16
2
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 36 bytes

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

Mathematica, 26 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 5 bytes

×0³¹S

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.

\$\endgroup\$
1
1
\$\begingroup\$

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!

\$\endgroup\$
2
1
\$\begingroup\$

Unwanted, Unnecessary, Opportunistic, 7 bytes

P0M31P*

Pass either 1 or 0 into the register.

\$\endgroup\$
1
\$\begingroup\$

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");
}
\$\endgroup\$
1
\$\begingroup\$

Haskell, 37 32 bytes

(('0'<$[1..31])++).show.fromEnum

Try it online!

Thanks @nimi for -5 bytes!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ show(fromEnum x) instead of last(...). Pointfree even shorter: (('0'<$[1..31])++).show.fromEnum. \$\endgroup\$
    – nimi
    Jul 19, 2017 at 15:09
  • \$\begingroup\$ @nimi Didn't know that Bool is an Enum, thanks! \$\endgroup\$ Jul 19, 2017 at 15:34
1
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 3 bytes shorter: printf('%032d',$argv[1]); (requires the -r flag). \$\endgroup\$
    – user63956
    Jul 20, 2017 at 1:19
1
\$\begingroup\$

Ly, 20 15 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.

\$\endgroup\$
1
  • \$\begingroup\$ Would 65*1+["0"o1-]nu work? \$\endgroup\$
    – ovs
    Jul 19, 2017 at 5:50
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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 :)

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 19, 2017 at 21:12
  • \$\begingroup\$ I see. I'll modify it soon enough. Thanks. \$\endgroup\$ Jul 19, 2017 at 21:49
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ Jul 20, 2017 at 9:37
  • \$\begingroup\$ @OlivierGrégoire Thanks but why isn't the final semicolon mandatory? \$\endgroup\$ Jul 20, 2017 at 10:23
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jul 20, 2017 at 12:02
1
\$\begingroup\$

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 (2 bytes).


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

0 and "" are falsy in Japt.

Try it online!

Test suite

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

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!

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

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.

\$\endgroup\$
1
\$\begingroup\$

Stax, 5 bytes

⌡Ö├⌡→

Run and debug it

Numeric 0 and empty lists are considered falsy. All other values are truthy.

\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 12 bytes

~x=bin(x,32)

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

\$\endgroup\$

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.