| Bytes | Lang | Time | Link |
|---|---|---|---|
| 122 | Pascal | 240727T200012Z | Kai Burg |
| 004 | Thunno 2 | 230624T140900Z | The Thon |
| 005 | Vyxal | 221015T094125Z | DialFros |
| 007 | Carrot | 170719T090022Z | TheLetha |
| 020 | AWK | 210522T013319Z | Pedro Ma |
| 082 | Starry | 210521T165505Z | Jay Ryan |
| 012 | Julia 0.6 | 170822T020254Z | Frames C |
| 005 | Stax | 210520T073353Z | Razetime |
| 025 | JavaScript ES6 | 170719T112518Z | Justin M |
| nan | Perl 5 | 170805T043735Z | Xcali |
| 009 | MY | 170719T224318Z | Adalynn |
| 029 | C# .NET Core | 170719T005146Z | LiefdeWe |
| 011 | Japt | 170719T014549Z | Oliver |
| 015 | Python 3 | 170719T001505Z | wrymug |
| 008 | Braingolf | 170719T085631Z | Mayube |
| 040 | Java | 170719T123945Z | V. Court |
| 010 | K/Kona | 170720T112255Z | Simon Ma |
| 032 | Scala | 170719T101917Z | V. Court |
| 036 | Brainfuck | 170720T052323Z | Rapha |
| 011 | Octave | 170719T131615Z | rahnema1 |
| 013 | Ly | 170719T015016Z | LyricLy |
| 369 | ArnoldC | 170720T010314Z | Temporal |
| 017 | Groovy | 170719T223342Z | GolfIsAG |
| 008 | V | 170719T002310Z | DJMcMayh |
| 011 | Perl 6 | 170719T213432Z | Sean |
| 021 | QBIC | 170719T200625Z | steenber |
| 024 | Cubix | 170719T183647Z | Giuseppe |
| 028 | PHP | 170719T173945Z | Jared Me |
| 006 | CJam | 170719T161826Z | geokavel |
| 021 | Ruby | 170719T170115Z | daniero |
| 015 | Dyvil v0.33.0 | 170719T123130Z | Clashsof |
| 071 | Common Lisp | 170719T155502Z | Cheldon |
| 032 | Haskell | 170719T003305Z | ბიმო |
| 026 | C | 170719T152815Z | cmaster |
| 016 | x8616 Machine Code DOS | 170719T105831Z | Cody Gra |
| 007 | Unwanted | 170719T135929Z | faso |
| 016 | Befunge98 | 170719T122812Z | ovs |
| 018 | Excel | 170719T124304Z | Engineer |
| 025 | Python 2 | 170719T101053Z | Adnan |
| 005 | Neim | 170719T011334Z | LiefdeWe |
| 005 | 05AB1E | 170719T114755Z | Okx |
| 016 | ><> | 170719T111005Z | Teal pel |
| 023 | Javascript | 170719T112109Z | SuperSto |
| 025 | JavaScript | 170719T111808Z | Grax32 |
| 028 | Clojure | 170719T111244Z | Joshua |
| 027 | Java 8 | 170719T085204Z | Kevin Cr |
| 017 | Octave | 170719T105303Z | Luis Men |
| 030 | Lua | 170719T105202Z | Liam Ham |
| 023 | Octave | 170719T103736Z | Stewie G |
| 005 | Charcoal | 170719T103540Z | Charlie |
| 005 | MATL | 170719T083445Z | Luis Men |
| 013 | Retina | 170719T095019Z | Neil |
| 009 | Pyth | 170719T032558Z | K Zhang |
| 023 | Python 3 | 170719T013122Z | C McAvoy |
| 008 | Jelly | 170719T011319Z | Dennis |
| 011 | J | 170719T005549Z | Jonah |
| 036 | Mathematica | 170719T001323Z | ZaMoC |
| 011 | APL | 170719T001712Z | Uriel |
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
Thunno 2, 4 bytes
Ṙ32ṛ
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.
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
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)
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
1`6+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.
Julia 0.6, 12 bytes
~x=bin(x,32)
Nothing clever here, there is basically a built in for it.
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}
MY, 10 9 bytes
𝕫BṄiℑpέ←←
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.
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.
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.
Braingolf, 10 8 bytes
#␟>[0_]N
␟ 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
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;}
Valid response : Java, 60 chars, 60 bytes
c->{String k="";for(k+=c?1:0;k.length()<32;k=0+k);return k;}
I know there is already a Java answer which is shorter than this one, but still :)
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"
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.
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
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!
++++ - 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
Octave,16 11 bytes
@(x)x(1:32)
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.
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.
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
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".
V, 8 bytes
32é0Àñl
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
Perl 6, 11 bytes
'0'x 31~+?*
'0' x 31produces the first 31 zeroes.*is the argument to the function.?coerces it to a boolean, and then+coerces that boolean into a number, either0or1, which is appended to the leading zeroes with~.
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.
Cubix, 24 bytes
1&u3I!|1^...0O;($\!vuO;@
Nonzero values are truthy in Cubix (as handled by !)
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 push0
truthy:
1: push1
both branches reach here:
^3u1&\: put31to the top of the stack and enter the loop
Loop:
!v0O;($\: if top is zero, go south, otherwise push0,Output as a number, pop (;), decrement ((), then$kip\and continue.
Finally:
;uO@: pop the zero,Output the0/1, and@exits.
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
CJam, 6 bytes
q31Te[
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.
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!
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.
Common Lisp, 71 bytes
(defun f(i)(format t "~v@{~A~:*~}" 31 0)(prin1(cond((null i) 0)(t 1))))
Because nil is a falsey value and anything else is truthy:
(f nil) = 00000000000000000000000000000000
(f 123) = 00000000000000000000000000000001
(f t) = 00000000000000000000000000000001
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");
}
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. :-)
Befunge-98, 16 bytes
'0'k:'k,&!!+,@
Hexdump:
00000000: 2730 271e 6b3a 271e 6b2c 2621 212b 2c40 '0'.k:'.k,&!!+,@
output as space sperated digits, 9 bytes:
'k.&!!.@
Hexdump:
00000000: 271e 6b2e 2621 212e 40 '.k.&!!.@
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.
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'))
Neim, 6 5 bytes
ᛝΨβ_I
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
><>, 18 16 bytes
<n0v?)*3a:l
;oi<
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
Javascript, 23 bytes
a=>'0'.repeat(31)+ +!!a
!!a coerces a into a boolean, which the unary plus turns into an int.
JavaScript, 25 bytes
v=>'0'.repeat(31)+(v?1:0)
31 zeroes and conditionally add a 1 or 0.
Clojure, 28 bytes
#(format "%031d" (if % 1 0))
Truthy is everything but nil and false
Octave, 23 bytes
@(x)[48+[!(1:31),x],'']
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]
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.
MATL, 5 bytes
32&YB
Uses inputs 1/ 0, or T/ F.
Explanation
32 % Push 32
&YB % Convert implicit input to binary with 32 digits. Gives char output
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.
Pyth, 9 bytes
+*31\0-1!
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
J, 11 bytes
(31#'0'),":
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
Mathematica, 36 bytes
""<>ToString/@PadLeft[{Boole@#},31]&
Mathematica, 26 bytes
Row@PadLeft[{Boole@#},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