x86 machine code on DOS - 14 13 11 bytes
Well, it did get shorter again! After writing a solution for an unrelated challenge, I noticed that the same trick could be applied even here. So here we go:
00000000 b4 08 cd 21 35 01 0a 86 c2 eb f7 |...!5......|
0000000b
Commented assembly:
org 100h
section .text
start:
mov ah,8 ; start with "read character with no echo"
lop:
; this loop runs twice per character read; first with ah=8,
; so "read character with no echo", then with ah=2, so
; "write character"; the switch is performed by the xor below
int 21h ; perform syscall
; ah is the syscall number; xor with 0x0a changes 8 to 2 and
; viceversa (so, switch read <=> write)
; al is the read character (when we did read); xor the low
; bit to change 0 to 1 and reverse
xor ax,0x0a01
mov dl,al ; put the read (and inverted character) in dl,
; where syscall 2 looks for the character to print
jmp lop ; loop
Previous solution - 13 bytes
I think it doesn't get much shorter than this. Actually, it did! Thanks to @ninjalj for shaving off one more byte.
00000000 b4 08 cd 21 34 01 92 b4 02 cd 21 eb f3 |...!4.....!..|
0000000d
This version features advanced interactivity™ - after running it from the command line, it spits out the "inverted" characters as long as you write the input digits (which are not echoed); to exit, just do a Ctrl-C.
Unlike the previous solution, this has some trouble running in DosBox - since DosBox doesn't support Ctrl-C correctly, you are forced to close the DosBox window if you want to exit. In a VM with DOS 6.0, instead, it runs as intended.
NASM source:
org 100h
section .text
start:
mov ah,8
int 21h
xor al,1
xchg dx,ax
mov ah,2
int 21h
jmp start
Old solution - 27 25 22 bytes
This accepted its input from the command line; runs smoothly as a .COM file in DosBox.
00000000 bb 01 00 b4 02 8a 97 81 00 80 f2 01 cd 21 43 3a |.............!C:|
00000010 1e 80 00 7c f0 c3 |...|..|
NASM input:
org 100h
section .text
start:
mov bx, 1
mov ah, 2
loop:
mov dl, byte[bx+81h]
xor dl, 1
int 21h
inc bx
cmp bl, byte[80h]
jl loop
exit:
ret
1What? It doesn't return the right result, does it? The result should have been a text string (without spaces), but with my limited knowledge of J, I think this will return a boolean list with a display form of 0 1 0 1 0 ... – Adám – 2015-09-25T15:14:55.553
The answer in J could also just be
-.
. – FUZxxl – 2016-01-02T17:38:02.1234This isn't allowed because you can't assume the input is stored in a certain variable.
=&'0'
works for the same number of bytes. – Esolanging Fruit – 2017-06-13T08:21:06.587Would you mind explaining how this works? I looked up some docs for J but I can't figure it out. – Jarett Millard – 2014-06-09T19:06:45.147
@Jarett There you go. – ɐɔıʇǝɥʇuʎs – 2014-06-09T19:36:35.767
1I have tried J. Can't bend my mind to it. Guess I haven't found good docs. Here, have a +1 for being a madman. – seequ – 2014-06-09T20:29:04.050
@TheRare I used 'Learning J' from the official J website. I still can't wrap my head around a lot of things, for example control structures and trains... – ɐɔıʇǝɥʇuʎs – 2014-06-09T21:07:16.697
1And this is why I will never use J. – Qix - MONICA WAS MISTREATED – 2014-06-09T23:49:42.857
2@Qix As unreadable as J is, this one actually makes sense to me, and other languages that have operators that take a vector LHS and a scalar RHS do behave similarly. – hvd – 2014-06-10T09:02:39.543