mercredi 10 février 2021

What does this main function calculate the odd integer values?

0x1 is the hex for 1 if i am not wrong, but how is that (i & 0x1) checking if the value is odd?

int main()
{
    vector<int> ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for (auto i : ivec) {
        cout << ((i & 0x1) ? i * 2 : i) << " ";
    }
    cout << endl;
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire