0

I have some more questions,

(Q1) Based on what I found out on google and based on what I understand, mask is used for calculation. Example I am given 0xF7 and I want to toggle the third bit.

The 0xF7 is the mask.

The number that you wanted to use in toggling is the flags.

The final output is called mask. and we use

mask=mask^flags(since we want to use it for toggling)


Am I right?

0

I think it would help if you look at it in binary perspective.

int x = 0x26;

in binary 0x26 =
0010 0110 //its separated for you to see it better

now we have this binary number : 0010 0110 and we want to toggle
the 3rd bit : the third bit is 00100110 the one on bold.

Lets use & to toggle it
int x = 0x26; // 0010 0110
int y = 0x22;// 0010 0010
int z = x&y; // ????

Lets see what happens in binary form.

Note that :
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0
0 & 1 = 0

0010 0110 //x
&
0010 0010 //y
--------------
0010 0010 //z

so , you see that the 3rd bit is 0 while rest is remained unchanged.

0

Then after that you know that the question wanted you to toggle bit 3. Then you just need to assure that the 3rd bit(counting from right to left) needs to change from a 1 to a zero. Hence you just create the flag to be 0000 0100 (the 3rd bit to be a 1) so that the 3rd bit of the mask can change from a 0 to a 1. Am I right?

0000 0100   0x04
  |||| ||||
  |||| |||+-- bit 0
  |||| ||+--- bit 1
  |||| |+---- bit 2
  |||| +----- bit 3
  ||||
  |||+------- bit 4
  ||+-------- bit 5
  |+--------- bit 6
  +---------- bit 7
0

(Q1) Based on what I found out on google and based on what I understand, mask is used for calculation. Example I am given 0xF7 and I want to toggle the third bit.

The 0xF7 is the mask.

Well, you've got the exact opposite mask to do the toggling -- 0xF7, used with XOR, will toggle every bit except the third bit.

0

Here are 2 question,

(Q1) Which is the third bit?

Firstperson posted that 00100110 counting from right to left, the 3rd number is the 3rd bit.

Dave stated that 0000 0100 0x04
|||| ||||
|||| |||+-- bit 0
|||| ||+--- bit 1
|||| |+---- bit 2
|||| +----- bit 3
||||
|||+------- bit 4
||+-------- bit 5
|+--------- bit 6
+---------- bit 7

which means the couting from right to left, the number fourth number is the 3rd bit.

(Q2)It seems that in some condition, AND operators can be used to turn off a bit like what a XOR operator can do. But when the question says toggle, does it mean you need to make sure that the given mask is able to change from 1(if it intially starts from 1) to 0 and also if I turn on the result , it will give me back a 1 am I right?


(Q3) Based on what the definition, it said flag refers to one or more bits that are used to store a binary value or code that has an assigned meaning. I am not so clear about this part, can anyone explain it in a simpler form?


if I am not wrong can I say 0xF7 is a flag?

0

Thank you everyone for helping me out:) I am now more clearer on what is happpening:) Thanks:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.