Sono nuovo alla programmazione. Recentemente mi sono imbattuto in un problema nel quale devo fare un particolare bit 0 di un numero.Fare un particolare bit "0" in C++
Ad esempio:
Ho un certo numero p
p = 73
binary = 1001001
Ora voglio fare 4 bit a 0, che è 1000001 (2) = 65 (10)
ho fatto questo nel modo seguente:
int p = 73;
int pos = 1<<3; // 4th bit
int max_bit = (1<<31) - 1; // making all bit to 1
int mask = pos^max_bit; // making 4th bit to 0 except others
p = p & mask; // changing 4th bit of p to 0
cout<<p<<endl;
C'è un modo migliore per farlo?
solo come "lo sapevate": che si chiama compensazione un po '. Rendendolo "1" si chiama impostando un bit – bolov
no non lo sapevo :(ma lo faccio ora :) grazie – Elliot