using bitwise operators
-
I have the following data DWORD data, mask, pre_result, result; int nShift; data = 0xF4; // 11110100 mask = 0x04; // 00000100 pre_result = data | mask; // = 00000100 switch (mask) { case 0x01: nShift=0; break; // mask = 00000001 case 0x02: nShift=1; break; // mask = 00000010 case 0x04: nShift=2; break; // mask = 00000100 // other cases } result = pre_result >> nShift; // result = 1 is there a method to dynamically shift the bits of pre_result to obtain the bit value for result without using case statements? pls help.
-
I have the following data DWORD data, mask, pre_result, result; int nShift; data = 0xF4; // 11110100 mask = 0x04; // 00000100 pre_result = data | mask; // = 00000100 switch (mask) { case 0x01: nShift=0; break; // mask = 00000001 case 0x02: nShift=1; break; // mask = 00000010 case 0x04: nShift=2; break; // mask = 00000100 // other cases } result = pre_result >> nShift; // result = 1 is there a method to dynamically shift the bits of pre_result to obtain the bit value for result without using case statements? pls help.
Given the values displayed for mask in the switch, m = mask >> 1; pre_result >> m; should provide the same result. BTW, the result of pre_result = data | mask is display incorrectly. It should be 11110100. The value shown 00000100 would be the result of data & mask.
-
I have the following data DWORD data, mask, pre_result, result; int nShift; data = 0xF4; // 11110100 mask = 0x04; // 00000100 pre_result = data | mask; // = 00000100 switch (mask) { case 0x01: nShift=0; break; // mask = 00000001 case 0x02: nShift=1; break; // mask = 00000010 case 0x04: nShift=2; break; // mask = 00000100 // other cases } result = pre_result >> nShift; // result = 1 is there a method to dynamically shift the bits of pre_result to obtain the bit value for result without using case statements? pls help.
First of all, a small correction: 11110101 | 00000100 = 11110101 11110101 & 00000100 = 00000100 What are you trying to do? Do you want to check whether a specific bit is on? Here's some quick information on bitwise operators: OR (|) : 0 | 0 = 0, 1 | 0 = 1, 1 | 0 = 1, 1 | 1 = 1 AND (&) : 0 & 0 = 0, 1 & 0 = 0, 0 & 1 = 0, 1 & 1 = 1 XOR (^) : 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0 Now some examples: value = 0xF4; // 76543210 // 11110100 To turn on bit 1 : value |= 2; (value = value | 2; 2 because 2^1 = 2) To turn on but 4 : value |= 16; (because 2^4 = 16) To turn off bit 2: value &= ~4; (~4 is an int, all bits on except bit 2) To check whether bit 5 is on: if (value & 32) // 2^5 = 32, or 0x20 // it's on Note that value & 32 does not return 1, but 32. To get a result of 1 or 0 only, you can: 1) Use the and operator to turn off all other bits 2) Use a right shift (>>) to move bit 5 to 0 Example: (value&32) >> 5; (returns 1 or 0) Hope this answers your question (and I hope I did't make any ridiculous errors :eek: )
-
Given the values displayed for mask in the switch, m = mask >> 1; pre_result >> m; should provide the same result. BTW, the result of pre_result = data | mask is display incorrectly. It should be 11110100. The value shown 00000100 would be the result of data & mask.
pre_result >> m should be pre_result >>= m
-
First of all, a small correction: 11110101 | 00000100 = 11110101 11110101 & 00000100 = 00000100 What are you trying to do? Do you want to check whether a specific bit is on? Here's some quick information on bitwise operators: OR (|) : 0 | 0 = 0, 1 | 0 = 1, 1 | 0 = 1, 1 | 1 = 1 AND (&) : 0 & 0 = 0, 1 & 0 = 0, 0 & 1 = 0, 1 & 1 = 1 XOR (^) : 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0 Now some examples: value = 0xF4; // 76543210 // 11110100 To turn on bit 1 : value |= 2; (value = value | 2; 2 because 2^1 = 2) To turn on but 4 : value |= 16; (because 2^4 = 16) To turn off bit 2: value &= ~4; (~4 is an int, all bits on except bit 2) To check whether bit 5 is on: if (value & 32) // 2^5 = 32, or 0x20 // it's on Note that value & 32 does not return 1, but 32. To get a result of 1 or 0 only, you can: 1) Use the and operator to turn off all other bits 2) Use a right shift (>>) to move bit 5 to 0 Example: (value&32) >> 5; (returns 1 or 0) Hope this answers your question (and I hope I did't make any ridiculous errors :eek: )
to check and set certain bits of a data value, that was what i was trying to get. Thanks for reading through my blunder of code, and samples code. That helps a lot. Just another questions. What if I need to check 2 bits? for eg I want to set bits 1 and 2 to 10. Can I do it in the same way as you described? mask = 6 // 00000110, for checking bits 1 and 2 value |= mask; // is this correct?