Invert a boolean array

A nice simple one

Input

Given a boolean array (Or an acceptable alternative), you can assume the array will never be more than 32 elements long.

[false, false, true, false, false]


Output

Invert every element of the array and output it.

[true, true, false, true, true]


Rules

• You can write a full program or just a function
• Standard loopholes apply
• Shortest code in bytes, per language, wins!

Test cases

Input:
[true, false]
Output:
[false, true]

Input: //Example of acceptable alternative
[0,1,1]
Output:
[1,0,0]

• How about arrays of 0 (false, all 0 bits) and -1 (true, all 1 bits)?
– lynn
Commented Sep 15, 2016 at 11:52
• @Lynn While it's the OPs decision, I'd say it should be up to whether your language considers though truthy/falsy. Commented Sep 15, 2016 at 11:53
• Related. (Given the simplicity of the core task, I'd say the differences in format are significant enough that these aren't duplicates.) Commented Sep 15, 2016 at 12:28
• More than code golf this looks to me like: what is the not operator in your favourite language? Additional points if it works on lists. Commented Sep 15, 2016 at 23:32

Python 2, 24 bytes (non-competing)

lambda a:[i-1for i in a]


Logic is similar to Steven's, but I tried to use this comment's idea, but different, because it still takes 0/1 arrays, not 0/-1. There is no byte shaving for using 0/-1, so let's be sane. Note that this is non-competing, until Steven or Lynn allows me to use the idea. If so, I might remove the non-competing mark. Note that this code cannot be shamelessly stolen, it's still here. Only Steven can use it for his answer.

Ruby, 14 bytes

Anonymous function:

->a{a.map &:!}


Test it:

->a{a.map &:!}.call([true, true, false, true, false, true, true])
# => [false, false, true, false, true, false, false]


Perl 6, 9 bytes

*.map: !*


Usage:

say (*.map: !*)((0, 1, 1)) # (True False False)


Actually, 2 bytes

♂Y


Try it online!

Map (♂) Boolean negate (Y)

Clojure, 12 bytes

#(map not %)


Basically the same as the Haskell answer. Unfortunately, Clojure doesn't have implicit partial application; thus the function macro.

Julia (1 Byte - Thanks to Dennis [See Comments])

!


Map the logical not to all elements of collection y, works due to automatic vectorization (as Dennis explained). My previous answer was basically using f(n)=map(!,n)' to map the logical not, but Julia does this on it's own. Jeez, what a cool language. Second time using it, still trying to learn here!

Try it here

• ! (1 byte) is enough. ! vectorizes automatically. Commented Sep 15, 2016 at 21:36
• In the console I had it didnt let me do that :(, plus thats a snippet, is it not? Commented Sep 16, 2016 at 2:03
• I've tested it with version 0.4; it's possible that it doesn't work in older versions of Julia. ! by itself evaluates to a function, which can be saved in a variable (just like a lambda), and is therefore considered a valid function submission. For.example, you can use it like f=!;print(f([true,false])). Commented Sep 16, 2016 at 2:24
• That's neat, technically 3 bytes for f=! though, unless just saying "!" is its own function works. Commented Sep 19, 2016 at 15:09
• By community consensus, ! is a valid answer on ois own. Commented Sep 19, 2016 at 15:22

APL (Dyalog Unicode), 1 byte

~


Try it online!

Factor, 4 bytes

vnot


Try it online!

Pyth, 2 bytes

!M


Explanation: (M)ap boolean not (!) over input.

Try it online!

• m! works aswell and makes more sense to me since you map the not over the input array. Commented Sep 15, 2016 at 12:25
• @KarlKastor They compile down to the same thing, assign("Q", eval_input()); imp_print(Pmap(lambda d: Pnot(d), Q)) Commented Sep 15, 2016 at 12:38

Hoon, 24 bytes

|*
*
(turn +< |=(? !+<))


Creates a generic gate, map over the contents of the list, negate all loobeans in it.

This uses the normal Hoon tricks, namely using a generic ("wet"/|*) gate to avoid having to specify the sample type, along with having unnamed samples (* or ?) and using tree navigation syntax (+<) to fetch them.

> a
~[%.y %.n %.n %.y %.n %.y]
> =f |*
*
(turn +< |=(? !+<))
> (f a)
~[%.n %.y %.y %.n %.y %.n]


Groovy (19 Bytes)

{x->x.collect{!it}}


Simple mapping function in a closure.

Stata, 20 bytes

recode x (0=1) (1=0)


x is the input, and the rest is self-explanatory

braingasm, 9 bytes

,[48-z:,]


Assumes that input from stdin is a string of only "0"s and "1"s, i.e. bytes containing the value 48 or 49. Prints the negated values to stdout.

How it works: Read one byte from stdin (,), and loop ([]) until the end of the input. For each byte, subtract 48 (48-) and print 1 if the result is 0 and vice versa (z is the zero-flag, : prints the given integer value), then get another byte from stdin (,)

Run it like e.g. $echo -n 001011 | braingasm invert.bg and get 110100 Scala, 27 bytes def f(a:Boolean*)=a map(!_)  Takes a bool array as varargs and maps each element to its inverse. • The type annotation isn't required is it? Shouldn't it be able to infer the type from !? Commented Sep 16, 2016 at 13:22 • @Carcigenicate Sadly, Scala can't infer the types of method arguments. Commented Sep 16, 2016 at 22:14 • Damn. Forgot about that. That's 9 bytes right there! Commented Sep 16, 2016 at 22:15 • @Carcigenicate I can't use Int*, because that's neither truthy nor falsey. Commented Sep 16, 2016 at 22:20 Kotlin, 13 bytes {it.map{!it}}  This is a lambda of (List<Boolean>)->List<Boolean> type (or (BooleanArray)->List<Boolean>, as map function on arrays returns lists instead of arrays). {it.map{!it}.toBooleanArray()} would return the result as an actual array. sed 8 y,01,10,  Takes input as 1s and 0s. Clojure, 12 bytes (map not x)  as in: (map not [true false true])  => (false true false) Updating due to the objection that this is not a function or program: #(map not %)  Returns a function that nots anything passed to it. • This isn't a function or program, and preassumes x exists. Commented Sep 16, 2016 at 13:16 • How is that different from the other examples? Commented Sep 16, 2016 at 23:08 • Because the other answers are (mostly) all functions, and don't assume the existence of a variable for their code to work. Your second example is fine though. Commented Sep 16, 2016 at 23:16 Scala, 3635 34 bytes def f(a:Array[Boolean])=a.map(!_) Discovered I could remove the second space. Discovered I could remove brackets. Gogh, 4 bytes {!}m  Usage: $ ./gogh -noa '{!}m' "1 0 1 0 0 0 1"
[0 1 0 1 1 1 0]


Racket, 15 bytes

(curry map not)


• Making this a lambda might make this more succinct, no? I was going to partially apply for my answer (partial map not, Clojure), but a function macro ended up being smaller. Idk what Rackets anonymous functions look like though honestly. Commented Sep 22, 2016 at 22:12
• Unfortunately Racket's shortest anonymous function uses the form of (λ (arguments) function-body), so it would be (λ(l)(map not l)) which is 18 bytes (the lambda symbol is two bytes). Interestingly, (map not(read)) is also 15 bytes. Commented Sep 24, 2016 at 20:18
• It actually uses a lambda symbol? Damn. That seems needlessly complicated :/ Commented Sep 24, 2016 at 21:09
• (There is also lambda, but that's not applicable to code golfing at all.) Commented Sep 25, 2016 at 9:48

Pyke, 2 bytes

m!


Try it here!

map(not, input)


Golang, 40 bytes

func(a[]bool){for i,b:=range a{a[i]=!b}}


usage

package main
import "fmt"
func main() {
a:=[]bool{true, true, false}
func(a []bool){for i,b:=range a{a[i]=!b}}(a)
fmt.Print(a) // => [false false true]
}


Brainfuck, 24 bytes

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


Try it online!

The same as my answer to a similar question.

Relies on 8-bit wrapping cells, the cell size might not matter (untested) but wrapping definitely is. The main part the program is the >++[->++[<]>-]>- does some rather convoluted things to flip the last bit of the number.

A shorter solution of 19 bytes is

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


but this requires the < to noop if the data index is 0, instead of the more typical implementation of going into negative indeces.

Brainfuck, 7269 61 bytes

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


Closest i managed to get as BF dosent have Array support

Formatting is stupid, i will fix it when i get a pc... Somehow it wont let me post it as a snippet

• You could put all the lines together into one long line, it doesn't really matter for us (because with newlines, the total byte length is 78 bytes). Commented Sep 22, 2016 at 23:14
• -4: ,>++++++++[<------>-]+<[->-<]>[<+>-]++++++++[<++++++>-]<. Commented Dec 20, 2020 at 10:24

PHP, 30 bytes

foreach($argv[1] as$i)echo!$i;  Testing code: $argv[1] = [true,true,false,true,false,false,true,false];
foreach($argv[1] as$i)echo!$i;  Test online C++11, 30 bytes As unnamed lambda: [](auto&v){for(auto&x:v)x=!x;}  Accepts any standard container like vector<int> (but not vector<bool>) or int[] or bool[]. • For the suggested edit: making this a function would require a return type Commented Nov 15, 2016 at 0:22 6502 Machine Language - 14 Bytes AE 00 01 CA B5 00 49 01 95 00 8A D0 F6 00  Array of bytes is in zero page. Length of array is in$0100.

Flipped array is in zero page.

Assembler code:

AE 00 01 - ldx $0100 CA - dex B5 00 - lda$00, X
49 01    - eor #$01 95 00 - sta$00, X
8A       - txa
D0 F6    - BNE (-10)
00       - BRK


(My first golf answer - sorry if format is off.)

• Welcome to Code Golf, nice first answer! Your formatting is fine, don't worry :p Commented Dec 21, 2020 at 4:01

Vyxala, 2 1 byte

-1 thanks to lyxal

†


Try it Online!

• Try it Online! for 1 byte. Commented May 15, 2021 at 13:09

Python 3, 26 bytes

lambda s:[~x+2 for x in s]


Try it online!

Lexurgy, 12 bytes

Input is a string of as for truthy and bs for falsy. These can be replaced with any two distinct characters, as a "proper" falsy value in Lexury would be an empty string, however since empty string is empty string, and infinite number of empty strings can be anywhere within a string, so two distinct characters are required.

a:
a=>b
b=>a


If you want numbers (16 bytes):

a:
\1=>\0
\0=>\1
`