If the lowest bit is 0, then you need to add 1.
If the lowest bit is 1, then you need to add -1.
So if input is a, then:
x = a and 1 ; x = 0/1 depending on lowest bit
x = x + x ; x = 0/2
x = not x ; x = -1/-3
x = x + 2 ; x = +1/-1
r = a + x ; will toggle lowest bit of original a
I assume the two's complement negative integer values is used in LC3, so NOT 2 == -3
, and -1 + 2 = +1
.
Unfortunately I don't know LC3, so I just hope my steps are reasonably simple to be implemented by LC3 instructions and the algorithm makes sense.
Other option is to use the NOT
to toggle the bit (not addition):
x = a and (not(1)) ; (0xFFFE in case of 16b word)
; x = copy of all bits of A except bottom bit (bottom bit = 0)
y = not(a) and 1 ; extract toggled bottom bit (1/0)
r = x + y ; compose the value back from the upper:lowest bit(s)
edit: which is what Mitch does in his answer.