vendredi 29 mai 2015

Check if bit has toggled in C

I am working on bitwise manipulation (in C) and I wanted to know how to check if a bit has toggled between a previous value and the new value.

Example :
    oldValue = 0x0FF //0000 1111 1111 in binary
    newValue = 0x100 //0001 0000 0000 in binary

In this example I want to check if the bit8 (9th bit) has toggled from 0 to 1.

I know that if I want to know if a bit is set, is can use :

value & (1 << 8)

So, is this correct ? :

if( (oldValue & (1 << 8)) == (newValue & (1 << 8)) ) //return 0 if toggled

Aucun commentaire:

Enregistrer un commentaire