# The shortest code to invert bit-wise a binary string

Me thinks there aren't enough easy questions on here that beginners can attempt!

The challenge: Given a random input string of 1's and 0's such as:

10101110101010010100010001010110101001010


Write the shortest code that outputs the bit-wise inverse like so:

01010001010101101011101110101001010110101


# R, 49 bytes,

bitwXor(strtoi(unlist(strsplit(scan(,""),""))),1)


Not the shortest answer, but eh !

Here, we :

• take as input the sequence as a character string scan(,""),
• split it into its digits strsplit(x, ""), creating a list (an R object),
• flatten the list unlist(x), creating a vector (another R object).

The items in the vector are converted to integers strtoi, on which the XOR logical operation is applied bitwXor, with respect to 1.

Here we're taking advantage of R's recycling. Both elements of the fonction bitwXor are vectors, but have different length. The function reuse the shortest vector to apply the operation to each element of the longest vector.

Briefly, XOR (⊕) table of truth is :

1 ⊕ 1 = 0
1 ⊕ 0 = 1
0 ⊕ 1 = 1
0 ⊕ 0 = 0

# Excel VBA, 15 Bytes

y=StrReverse(x)


Assuming that x is a String Input, StrReverse Function can be used and the output is y As String. (Credit: Grant Peters)

A code to test the result:

Sub test()
Dim x As String, y As String
x = "10101110101010010100010001010110101001010"
y = StrReverse(x)
Debug.Print y
'Output: 01010010101101010001000101001010101110101
End Sub


# Pyth, 6 bytes

jkms!s

Try it!

Takes input as a string. New at this so it probably isn't optimal but the shortest pyth one here so far!

# Braingolf, 12 bytes

i{#0-}n&,{_}


Try it online!

## Explanation

i{#0-}n&,{_}
i             Read line from STDIN, convert each char to ordinal and push to stack
{...}        Foreach, runs on each item in stack
#0-         Subtract 48 from each ordinal
n       Negate, all truthy values become 0, all falsey values become 1
&,     Flip entire stack
{_}  Print each item


# Braingolf, 7 bytes [kinda double non-competing]

dn&,{_}


Try it online!

Kinda non-competing because braingolf cannot accept integer input from command-line arguments, it will always implicitly convert it to an integer if the input is only digits.

## Explanation

dn&,{_}  Implicit input from command-line args
d        Split number into digits
n       Negate, all truthy values become 0, all falsey values become 1
&,     Flip entire stack
{_}  Print each item


# J, 5 Bytes

That is, if array input is allowed:

i=:-.


Defines the verb i to be the 'not' function.

If string input is mandatory, there's this 10 byte solution:

i=:-.@"."0


If string output is also mandatory, there's this 18 byte solution:

i=:,@:(":@-.@"."0)


# Charcoal, 6 bytes

ＦθＩ¬Ｉι


Try it online!

Link is to the verbose version of the code. Explanation:

Ｆθ             for each character in the input as string
implicitly print
Ｉι       the character as a number,
¬          inverted
Ｉ           and casted again as string


Or is it 01 bytes?

~O!-84i@j~


Try it online!

# C# (Visual C# Interactive Compiler), 39 bytes

b=>b.Aggregate("",(c,d)=>c+(char)(d^1))


Try it online!

Shortest of all the C# solutions! You can make it even shorter with Select instead of Aggregate if the rules allow you to return a sequence of chars instead of a string.

# SmileBASIC, 38 bytes

INPUT S$WHILE""<S$?SHIFT(S\$)<"1";
WEND


# Keg, 4 bytes

÷(¬"


Try it online!

• ÷ splits the number into individual bits
• ( does the following length of stack times
• .... ¬ logically not the top
• .... " right shifts to the next item

Uses latest github interpreter.

# TI-BASIC, 3 bytes

B=0


Same "feature" as J; assuming the binary is stored in B, it will display the output. Here is an explanation:

The TI-BASIC comparison operator is =. However, = doesn't work like the normal == operator: it compares item-by-item. Keep in mind that an array is formed like this: {0,2,3}. 2={0,2,3} returns {0,1,0} for example. Since TI-BASIC's bools are 1 and 0, this results in a list of 1's and 0's (They aren't strings, and every other character other than 1 would also result in 0.)

• As a side note, not(B would do the same thing. – Timtech Jun 11 '14 at 11:06
• Works only on lists, not strings; B isn't even a list or string variable. – lirtosiast Jun 8 '15 at 2:27