Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using bitwise operators

using bitwise operators

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hearties
    wrote on last edited by
    #1

    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.

    S P 2 Replies Last reply
    0
    • H hearties

      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.

      S Offline
      S Offline
      Stan Shannon
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • H hearties

        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.

        P Offline
        P Offline
        Pavlos Touboulidis
        wrote on last edited by
        #3

        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: )

        H 1 Reply Last reply
        0
        • S Stan Shannon

          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.

          S Offline
          S Offline
          Stan Shannon
          wrote on last edited by
          #4

          pre_result >> m should be pre_result >>= m

          1 Reply Last reply
          0
          • P Pavlos Touboulidis

            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: )

            H Offline
            H Offline
            hearties
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups