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. The Lounge
  3. Your mission, should you choose to accept it..

Your mission, should you choose to accept it..

Scheduled Pinned Locked Moved The Lounge
questionlounge
41 Posts 10 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.
  • L Lost User

    What if they're all different powers of two?

    M Offline
    M Offline
    Mladen Jankovic
    wrote on last edited by
    #20

    it won't work :)

    1 Reply Last reply
    0
    • L Luc Pattyn

      No problem, same principle, now taking advantage of the product having a limited number of factors:

      h(x) = (x | x>>8 | x>>16 | x>>24) & 0xFF

      and if ( h(a)*h(b)*h(c) == 0)... :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

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

      Very clever! But I had fewer operators still, 12 in total if the function is inlined

      L 1 Reply Last reply
      0
      • L Luc Pattyn

        due to overflow a*b*c may result in false zeroes when a power of 2 aliases to zero (say a=2^16, b=2^16, c=1) For 32-bit integers the function

        f(x) = x | x>>1 | x>>2 | ... | x>>31

        calculates the next power of 2 minus 1, resulting in a non-zero and odd number for all non-zero integers. (add terms for wider integers, and add parentheses if you must). therefore f(a)*f(b)*f(c) is zero if and only if a*b*c is zero, and it doesn't suffer from overflow aliasing, as the lowest bit gets preserved by multiplication. BTW: To completely avoid overflow, one could also use

        g(x) = f(x) & 1

        :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #22

        If we can use assembly instructions, we can compact your code using BSR or BSF. :)

        Thou mewling ill-breeding pignut!

        L 1 Reply Last reply
        0
        • A AspDotNetDev

          If we can use assembly instructions, we can compact your code using BSR or BSF. :)

          Thou mewling ill-breeding pignut!

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

          Ah yes, that was my "other" solution (the one that does not only use basic operators)

          1 Reply Last reply
          0
          • L Lost User

            Very clever! But I had fewer operators still, 12 in total if the function is inlined

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #24

            12? that is a lot. Lets use the sign bit now:

            if ( (a|-a)&(b|-b)&(c|-c) < 0 ) log("all non-zero");

            :-D

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            L A C 4 Replies Last reply
            0
            • L Luc Pattyn

              No problem, same principle, now taking advantage of the product having a limited number of factors:

              h(x) = (x | x>>8 | x>>16 | x>>24) & 0xFF

              and if ( h(a)*h(b)*h(c) == 0)... :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #25

              I think that since the cube root of Int32.MaxValue is greater than 1024, we can further reduce it using your same technique (10-bit chunks instead of 8-bit chunks):

              if (((a | (a << 10) | (a << 20)) & 0xFF7) * ((b | (b << 10) | (b << 20)) & 0xFF7) * ((c | (c << 10) | (c << 20)) & 0xFF7) == 0) { }

              Thou mewling ill-breeding pignut!

              L A 2 Replies Last reply
              0
              • L Luc Pattyn

                12? that is a lot. Lets use the sign bit now:

                if ( (a|-a)&(b|-b)&(c|-c) < 0 ) log("all non-zero");

                :-D

                Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                Ok you beat me ;P I had (-(a & (a - 1)) & .. etc, yours is obviously better

                L 1 Reply Last reply
                0
                • A AspDotNetDev

                  I think that since the cube root of Int32.MaxValue is greater than 1024, we can further reduce it using your same technique (10-bit chunks instead of 8-bit chunks):

                  if (((a | (a << 10) | (a << 20)) & 0xFF7) * ((b | (b << 10) | (b << 20)) & 0xFF7) * ((c | (c << 10) | (c << 20)) & 0xFF7) == 0) { }

                  Thou mewling ill-breeding pignut!

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #27

                  Actually, I think bit shifting might do funny things depending on the sign bit, so instead of shifting by 20, might just want to shift by 14.

                  Thou mewling ill-breeding pignut!

                  1 Reply Last reply
                  0
                  • A AspDotNetDev

                    I think that since the cube root of Int32.MaxValue is greater than 1024, we can further reduce it using your same technique (10-bit chunks instead of 8-bit chunks):

                    if (((a | (a << 10) | (a << 20)) & 0xFF7) * ((b | (b << 10) | (b << 20)) & 0xFF7) * ((c | (c << 10) | (c << 20)) & 0xFF7) == 0) { }

                    Thou mewling ill-breeding pignut!

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #28

                    That technique is so outdated now. :laugh:

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    A 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      That technique is so outdated now. :laugh:

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #29

                      Yep. :rolleyes:

                      Thou mewling ill-breeding pignut!

                      1 Reply Last reply
                      0
                      • L Lost User

                        Ok you beat me ;P I had (-(a & (a - 1)) & .. etc, yours is obviously better

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #30

                        :jig:

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          12? that is a lot. Lets use the sign bit now:

                          if ( (a|-a)&(b|-b)&(c|-c) < 0 ) log("all non-zero");

                          :-D

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          A Offline
                          A Offline
                          AspDotNetDev
                          wrote on last edited by
                          #31

                          Well done. :thumbsup:

                          Thou mewling ill-breeding pignut!

                          L 1 Reply Last reply
                          0
                          • A AspDotNetDev

                            Well done. :thumbsup:

                            Thou mewling ill-breeding pignut!

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #32

                            Thanks. The nice thing about this approach is it works for all widths, and for any number of product factors, as long as they are all signed integers. :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              12? that is a lot. Lets use the sign bit now:

                              if ( (a|-a)&(b|-b)&(c|-c) < 0 ) log("all non-zero");

                              :-D

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                              Wait a minute.. the challenge was for any zero (ie the reverse condition) Which is a trivial change to this solution, but still..

                              1 Reply Last reply
                              0
                              • L Lost User

                                .. is to write a replacement for if (a == 0 || b == 0 || c == 0), that - uses at most one comparison. - uses only integer arithmetic. - does not make assumptions about the values of a b and c, except that they are 32-bit 2's complement integers. This entirely useless challenge (is there any other kind?) was inspired by a question on a site that shall not be named, asking for a shorter way to write it. But then people started answering with a * b * c == 0 (which is wrong in general, bonus points if you know why) and (a | b | c) == 0 which is a nice try but tests whether all of them are zero instead of any of them. That inspired me to search for a solution like that, and I found 2, one of which uses only basic operators. Can you find it?

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #34

                                harold aptroot wrote:

                                entirely useless challenge (is there any other kind?)

                                Some of these wouldn't be called entirely useless, would they? :) PS: the damn link-paste bug is acting up again.

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                L 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  harold aptroot wrote:

                                  entirely useless challenge (is there any other kind?)

                                  Some of these wouldn't be called entirely useless, would they? :) PS: the damn link-paste bug is acting up again.

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                  Aren't they just some other kind of useless? The kind of useless where you won't solve the challenge anyway so why bother.. But you have a point

                                  L 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Aren't they just some other kind of useless? The kind of useless where you won't solve the challenge anyway so why bother.. But you have a point

                                    L Offline
                                    L Offline
                                    Luc Pattyn
                                    wrote on last edited by
                                    #36

                                    When everything were useless, then so would be the word itself. :)

                                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                                    L 1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      When everything were useless, then so would be the word itself. :)

                                      Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                      Ok that's a bit too deep for me at 1 in the morning..

                                      1 Reply Last reply
                                      0
                                      • A AspDotNetDev

                                        Pfft, I don't even need a comparison:

                                        try
                                        {
                                        int x = 1 / a / b / c;
                                        }
                                        catch (DivideByZeroException)
                                        {
                                        // One of them was zero.
                                        }

                                        Thou mewling ill-breeding pignut!

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

                                        My eyes! It burnssses!

                                        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                        1 Reply Last reply
                                        0
                                        • L Luc Pattyn

                                          12? that is a lot. Lets use the sign bit now:

                                          if ( (a|-a)&(b|-b)&(c|-c) < 0 ) log("all non-zero");

                                          :-D

                                          Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                          You're an artist, Luc.

                                          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                          L 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