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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. bit operation challenge

bit operation challenge

Scheduled Pinned Locked Moved The Lounge
16 Posts 12 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.
  • A alex barylski

    It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

    I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

    A Offline
    A Offline
    Anton Afanasyev
    wrote on last edited by
    #2

    well, if you have the value of all three bits, then the following would do: answer = ((not b) AND (not c)) OR ((not a) AND (not b) AND c) OR ((not a) AND b AND (not c)) where a, b, c are your three bits. I'm not sure if this can be simplified further, but it would do the trick.


    :badger:

    1 Reply Last reply
    0
    • A alex barylski

      It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

      I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

      A Offline
      A Offline
      Andy Brummer
      wrote on last edited by
      #3

      I'm not sure what you are asking for. (x & 111b) > 0 will tell you if one or more of the 3 bits are set. Using and is essentially a bit mask.


      I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

      C A 2 Replies Last reply
      0
      • A Andy Brummer

        I'm not sure what you are asking for. (x & 111b) > 0 will tell you if one or more of the 3 bits are set. Using and is essentially a bit mask.


        I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

        C Offline
        C Offline
        Chris Maunder
        wrote on last edited by
        #4

        that yields true if 1 or more bits are set. How do you check if 1 and only 1 bit is set? (apart from "if value=1 or value=2 or value=4" ;))

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        A 1 Reply Last reply
        0
        • A alex barylski

          It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

          I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          How's about this? !(((a^110b)+((a^101b)>>1)+((a^011b)>>2))>>1) Edit - maybe I should explain a bit! Essentially just sum the binary digits (what the xor's and shifts are doing), the most you can have is 3 (or 11b) since we're limited to 3 binary digits. Shift that right one place and anything 2 or more leaves a 1 in the right most bit which, when not'd leaves a 0 (false). Anything else leaves a 1 (true). Edit again: This can be simplified !(((a^110b)+((a^101b)>>1)+(a>>2))>>1) since the effect of shifting two spots right removes the need for the xor. Cheers, Drew.

          1 Reply Last reply
          0
          • A alex barylski

            It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

            I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #6

            Drew came up with the only solution I like (though I played with several less elegant forms), but another approach I like is to define your flag variable as a bitset, then use the count operator when you need to test it.

            "A Journey of a Thousand Rest Stops Begins with a Single Movement"

            1 Reply Last reply
            0
            • A alex barylski

              It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

              I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

              R Offline
              R Offline
              Robert Surtees
              wrote on last edited by
              #7

              b & (b - 1) is false for all powers of two

              A P S 3 Replies Last reply
              0
              • A Andy Brummer

                I'm not sure what you are asking for. (x & 111b) > 0 will tell you if one or more of the 3 bits are set. Using and is essentially a bit mask.


                I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

                A Offline
                A Offline
                alex barylski
                wrote on last edited by
                #8

                I wanted to see if it was possible to check if *only* a single bit was set, nothing more, nothing less otherwise return FALSE. Not using anything but a single complicated bitwise statement (no ternary, conditionals, switch, etc).

                I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                G 1 Reply Last reply
                0
                • C Chris Maunder

                  that yields true if 1 or more bits are set. How do you check if 1 and only 1 bit is set? (apart from "if value=1 or value=2 or value=4" ;))

                  cheers, Chris Maunder

                  CodeProject.com : C++ MVP

                  A Offline
                  A Offline
                  alex barylski
                  wrote on last edited by
                  #9

                  Yes...yeah exactly :)

                  I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                  1 Reply Last reply
                  0
                  • R Robert Surtees

                    b & (b - 1) is false for all powers of two

                    A Offline
                    A Offline
                    alex barylski
                    wrote on last edited by
                    #10

                    Hhuh... i'm missing something, you need to explain more...this does the same as above? :(

                    I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                    G P 2 Replies Last reply
                    0
                    • A alex barylski

                      It's technical in nature but doesn't allpy to any specific language...so I'm asking it here: 1) I have a variable which is three bits long. 2) I need a quick bitwise operation (single statement) that returns FALSE if anymore than one bit is set (no ternary operator). Mutex type check I guess :) The user should only ever specify one field, but they might do otherwise which would yield inaccurate results. So say: 011 is passed, I need a quick check to confirm it's more than a single bit set and thus returning false, so the function can be unit tested I can't think of any combination of bitwise operators which would yield such a result - is it possible? Cheers :)

                      I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                      J Offline
                      J Offline
                      John R Shaw
                      wrote on last edited by
                      #11

                      Here are a couple of more for you: (((x&1) + ((x&2)>>1) + ((x&4)>>2)) == 1); ((x&7) && !((x&3) || (x&5) || (x&6));

                      Hockey wrote:

                      I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time

                      I still enjoy it when I work on something new (with no pressure). ;) Other times it is: (1) I know the problem, (2) I know the solution, (3) Blast! Now I have to type all that before tomorrow. :sigh:

                      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                      1 Reply Last reply
                      0
                      • A alex barylski

                        Hhuh... i'm missing something, you need to explain more...this does the same as above? :(

                        I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                        G Offline
                        G Offline
                        gmarian
                        wrote on last edited by
                        #12

                        Now that's the elegant solution! Try it for some numbers: 010b: 010b - 1 = 001b 010b & 001b = 0 100b: 100b - 1 = 010b 100b & 010b = 0 110b: 110b - 1 = 101b 110b * 101b = 100b so, if (bitFlag & (bitFlag - 1) == 0) { }

                        1 Reply Last reply
                        0
                        • A alex barylski

                          Hhuh... i'm missing something, you need to explain more...this does the same as above? :(

                          I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

                          P Offline
                          P Offline
                          peterchen
                          wrote on last edited by
                          #13

                          Distinguish three cases: power of two, zero, and all other values You understand binary notation and bitwise operators? If b is a power of two (b = 2^N): b-1 is a number with all bits 0..N-1 set (e.g. for 2^3=8=1000b, 8-1 = 0111b) In this casethere are not bits set in both b and (b-1), so the '&' yields 0 b=0: 0 '&' (anything) remains zero For the rest: they can be represented as b = 2^N+g with a g>0 && g<2^N since g>0, both b and 8b-1) will have the bit^N set, so the '&' operation will return a non-zero value.


                          Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                          Linkify!|Fold With Us!

                          1 Reply Last reply
                          0
                          • R Robert Surtees

                            b & (b - 1) is false for all powers of two

                            P Offline
                            P Offline
                            peterchen
                            wrote on last edited by
                            #14

                            You beat me :mad: so I voted you a five, so you stick out ;)


                            Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                            Linkify!|Fold With Us!

                            1 Reply Last reply
                            0
                            • A alex barylski

                              I wanted to see if it was possible to check if *only* a single bit was set, nothing more, nothing less otherwise return FALSE. Not using anything but a single complicated bitwise statement (no ternary, conditionals, switch, etc).

                              I finally got a job doing something I enjoy and I"m good at...15 hour days seem like play time :P

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

                              inline bool SingleBitOf3(unsigned X)
                              {
                              return (X & 0x04) || (X & 0x02) || (X & 0x01);
                              }

                              Kinda icky, mixing bitwise operators and logical operators in the same expression, but hey.


                              Software Zen: delete this;

                              Fold With Us![^]

                              1 Reply Last reply
                              0
                              • R Robert Surtees

                                b & (b - 1) is false for all powers of two

                                S Offline
                                S Offline
                                Sebastian Schneider
                                wrote on last edited by
                                #16

                                Brilliant. I am gonna kidnap you and force you to programm stuff in my basement.

                                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