This question has already been answered:
I ask for help with the task in the title!
This question has already been answered:
I ask for help with the task in the title!
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
Prepare the mask - the bits in the inversion positions are single, in the others - zero. Perform a bitwise XOR operation ( ^ ).
I hope to create such a mask will not be?
unsigned a = 12345; unsigned p = 10; // ΠΏΠΎΠ·ΠΈΡΠΈΡ, Π½ΡΠΌΠ΅ΡΠ°ΡΠΈΡ Ρ 0 ΠΎΡ ΠΌΠ»Π°Π΄ΡΠΈΡ
Π±ΠΈΡΠΎΠ² ΠΊ ΡΡΠ°ΡΡΠΈΠΌ unsigned n = 2; // ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π±ΠΈΡΠΎΠ² Π΄Π»Ρ ΠΈΠ½Π²Π΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ // Π‘ΠΎΡΡΠ°Π²Π»ΡΠ΅ΠΌ ΠΌΠ°ΡΠΊΡ unsigned mask = (1u << n) - 1; // 'n' Π΅Π΄ΠΈΠ½ΠΈΡ Π² ΠΌΠ»Π°Π΄ΡΠΈΡ
ΡΠ°Π·ΡΡΠ΄Π°Ρ
mask <<= p; // 'n' Π΅Π΄ΠΈΠ½ΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΎΡ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ 'p' // ΠΠ½Π²Π΅ΡΡΠΈΡΡΠ΅ΠΌ Π±ΠΈΡΡ a ^= mask; Source: https://ru.stackoverflow.com/questions/589996/
All Articles