1.22. MANIPULATING SPECIFIC BIT(S)
x86
Non-optimizing MSVC
We get (MSVC 2010):
Listing 1.271: MSVC 2010
_rt$ = -4 ; size = 4
_a$ = 8 ; size = 4
_f PROC
push ebp
mov ebp, esp
push ecx
mov eax, DWORD PTR _a$[ebp]
mov DWORD PTR _rt$[ebp], eax
mov ecx, DWORD PTR _rt$[ebp]
or ecx, 16384 ; 00004000H
mov DWORD PTR _rt$[ebp], ecx
mov edx, DWORD PTR _rt$[ebp]
and edx, -513 ; fffffdffH
mov DWORD PTR _rt$[ebp], edx
mov eax, DWORD PTR _rt$[ebp]
mov esp, ebp
pop ebp
ret 0
_f ENDP
TheORinstruction sets one bit into a register while ignoring other 1 bits.
ANDresets one bit. It can be said thatANDjust copies all bits except one. Indeed, in the secondAND
operand only the bits that need to be saved are set, just the one do not want to copy is not (which is 0 in
the bitmask). It is the easier way to memorize the logic.