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. about a = b | c | ...

about a = b | c | ...

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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
    HOW WHAT
    wrote on last edited by
    #1

    a = b | c | ... (1) if (a & b) { .... } (2) if ((a & b) == b) { .... } (1) and (2), use which is better? why? thanks.

    G C 2 Replies Last reply
    0
    • H HOW WHAT

      a = b | c | ... (1) if (a & b) { .... } (2) if ((a & b) == b) { .... } (1) and (2), use which is better? why? thanks.

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      If the value 'b' is a single bit, then the two approaches are equivalent. If there are multiple bits set in 'b', then they are not. (1) will be true if any of the bits in b are also set in a. (2) will be true if and only if the same bits that are set in b are also set in a.


      Software Zen: delete this;

      Fold With Us![^]

      H 1 Reply Last reply
      0
      • G Gary R Wheeler

        If the value 'b' is a single bit, then the two approaches are equivalent. If there are multiple bits set in 'b', then they are not. (1) will be true if any of the bits in b are also set in a. (2) will be true if and only if the same bits that are set in b are also set in a.


        Software Zen: delete this;

        Fold With Us![^]

        H Offline
        H Offline
        HOW WHAT
        wrote on last edited by
        #3

        umm... Can you example some?

        G 1 Reply Last reply
        0
        • H HOW WHAT

          a = b | c | ... (1) if (a & b) { .... } (2) if ((a & b) == b) { .... } (1) and (2), use which is better? why? thanks.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          It depends. If each of b,c,... corrensponds to just one bit set (e.g. b=1; c=2;...), then the expressions are equivalent (maybe (1) is faster). On the other hand, if b,c,... holds composite bit values (e.g. b=3; c=5;...) then you have to carefully choose the expression you need, since (1) meets the requisite of an OR condition, while (2) corrensponds to an AND one). For instance, conside the following code snippet:

          enum
          {
          FEMALE = 1, //MALE: bit unset
          YOUNG = 2, //OLD: bit unset
          ...
          };

          if b=FEMALE; then both the (1) and (2) meet the condition, but if you are searching for a young female (b=YOUNG|FEMALE;), the you have to be careful, because (1) holds true also when a represents a young guy or an old lady! :):-D:)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          H 1 Reply Last reply
          0
          • C CPallini

            It depends. If each of b,c,... corrensponds to just one bit set (e.g. b=1; c=2;...), then the expressions are equivalent (maybe (1) is faster). On the other hand, if b,c,... holds composite bit values (e.g. b=3; c=5;...) then you have to carefully choose the expression you need, since (1) meets the requisite of an OR condition, while (2) corrensponds to an AND one). For instance, conside the following code snippet:

            enum
            {
            FEMALE = 1, //MALE: bit unset
            YOUNG = 2, //OLD: bit unset
            ...
            };

            if b=FEMALE; then both the (1) and (2) meet the condition, but if you are searching for a young female (b=YOUNG|FEMALE;), the you have to be careful, because (1) holds true also when a represents a young guy or an old lady! :):-D:)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            H Offline
            H Offline
            HOW WHAT
            wrote on last edited by
            #5

            :confused: a = 1 | 0x10000000 a & 3 = 1 -> if (a & 3) { ...} error a & 4 = 0 not understand about bit a = b | c | d how to define value of b,c, d

            C 1 Reply Last reply
            0
            • H HOW WHAT

              umm... Can you example some?

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              Example 1: Suppose a=0x1A (00011010 binary), and b=0x08 (00001000). In that case, 'if (a & b)' and 'if ((a & b) == b)' are both satisfied, since 'a & b' evaluates to 0x08. Example 2: Suppose a=0x1A (00011010 binary) again, but this time b=0x09 (00001001 binary). In that case, 'if (a & b)' is satisfied, yet 'if ((a & b) == b)' is not. Remember that the first if is satisfied by any non-zero value, while the second one requires that the result of the '(a & b)' expression equal the value of b.


              Software Zen: delete this;

              Fold With Us![^]

              1 Reply Last reply
              0
              • H HOW WHAT

                :confused: a = 1 | 0x10000000 a & 3 = 1 -> if (a & 3) { ...} error a & 4 = 0 not understand about bit a = b | c | d how to define value of b,c, d

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                What I mean is that if you have b,c,d,e,f,... each corrensponding to only one bit set, for instance:

                b=0x00000001;// bit 0 set
                c=0x00000002;// bit 1 set
                d=0x00000004;// bit 2 set
                e=0x00000008;// bit 3 set
                f=0x00000010;// bit 4 set

                then a condition using (a & b) evaluates to true (non-zero) only if a has the bit 0 set and the same holds for a condition using ((a & b) == b). In the same way, a condition using (a & c) evaluates to non-zero only if a has the bit 1 set, the same holding for a condition using ((a & c)==c), and so on... On the other hand, suppose that x is a combination (sum using OR) of the above indipendent values, for instance x= b | e;, so that x=0x00000009, i.e. x has both the bits 0 and 3 set. In this case, a condition using (a & x) evaluates to non-zero either if a has:* only bit 0 set

                • only bit 3 set
                • both bit 0 and 3while a condition using ((a & x) == x) holds true only if both bit 0 and 3 are set. In other words, if x = 0x00000009; then (a & x) holds true if a = 0x00000001;, or a = 0x00000008;, or a = 0x00000009; while ((a & x) == x) holds true if a = 0x00000009. hope that helps :) BTW both conditions are also true if a = 0x00000019 and so on...

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                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