operators - Write C statement that clears two bits without disturbing the other bits -


my assignment write 1 or more c statements clears (i.e. sets 0) bits 11 , 12 of variable "x" without disturbing other bits using bit-level c-operators. professor says: "the variable "mask", declared below, may helpful."

int mask = 0x00001800;  int x = arbitrary_value; 

should using shift operations combined bit-level operations? i'm bit unclear how make happen.

@garrett, force or mask values use bitwise operands (&, |, ~, ^). how you. can create mask, 1 created 0x00001800. if convert mask binary this: ‭1100000000000‬. can see starting right bits 11 , 12 ones stay high.

now, use mask mask value need know want do. case want force bits 11 , 12 of variable 0, so, force 0 can use , bitwise operand (&) passing 0 want force 0 , 1 want stay how on other variable. if want use mask 0x1800 operation must first invert bits, because want 0s right mask has 1s , 1s right has 0s. invert bits can use not bitwise operand, like:

int mask = 0x1800; int invertedmask = ~mask; 

value of variables in binary:

mask = 1100000000000 invertedmask = 0011111111111

now can force bits 11 , 12 variable zero, example:

int mask = 0x1800; //value in binary 1100000000000 int value = ‭7347‬;   //value in binary 1110010110011 int finalvalue = 0;  mask = ~mask; // mask value 0011111111111 finalvalue = value&mask; //final value 0010010110011 

as can see, bits 11 , 12 (from right left) forced down while other stay right in value variable.

and using principles can use lot of other combination, including shift of bits deslocate bits of mask example.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -