# Explanation of SBB instruction

I have this Asm from a book that suppose to do a Boolean cast: `rax := rax ? 1 : 0`

``````1. neg      rax
2. sbb      rax,rax
3. neg      rax
``````

But as i understand this code and the instructions (below), this would work only if the register will have 0 in it.

because lets assume that (register have some positive number in it):

``````rax = 9    ;initial register
rax = -9   ;after line 1
rax = -17  ;after line 2
rax = 17   ;after line 3
``````

register have 0 in it:

``````rax = 0    ;initial register
rax = 0    ;after line 1
rax = 0    ;after line 2
rax = 0    ;after line 3
``````

I think that the author meant to do `add` in line 2 instead of `sbb` am i correct?

Instruction explanations: neg instruction:

``````neg Destination      ;Destination = -Destination
if(Destination == 0)
CF = 0;
else
CF = 1;
``````

sbb instruction:

``````sbb Destination, Source      ;Destination = Destination - (Source + CF);
``````

``````-9 - ( -9 + 1 ) = -9 - ( -8 ) = -9 + 8 = -1, not -17