x86 machine code, 8 bytes
Load register al
with an 8 bit integer
A8 01 (test al, 1; and whatever is in al with one, fast and small method for checking odd/even)
74 04 (jz 8; jump to even code if zf (zero flag, will be set if above instruction returns 0) is set)
04 02 (add al, 2; Two, because the next instruction will subtract one again, as if I incremented and jumped past the dec but shorter and faster)
FE C8 (dec al; Explained above, this is the code that is jumped to if input is even)
If you want, here is some actual assembly in boot-sector format. Just assemble this with nasm filename.asm
and run qemu-system-i386 filename
to test it. Press a key, and you will see that it will be the next letter if the ascii code was odd, or previous if it was even.
[ORG 0x7C00]
start:
xor ah, ah ;bios keyboard: get character (xoring a register with itself is a fast way to set it to 0 which is what we need)
int 0x16 ; keyboard io bios interrupt
mov ah, 0x0e ;bios graphics: display character (get ready)
test al, 0x01
jz even
add al, 2
even:
dec al
int 0x10 ; we already set al to 0E: display character, which takes a character from al and outputs that.
jmp start ; just go back to the start and get another character
times 510-($-$$) db 0 ; bootsector padding/signature
db 0x55
db 0xAA
Almost forgot, I should probably add the original assembly code that I used to create the bytes above
test al, 0x01
jz even
;odd code
add al, 2
even:
dec al