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

    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