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.
  • M Minion no 5

    if((a * b * c) == 0) The clue was in the ||s.

    F Offline
    F Offline
    Firo Atrum Ventus
    wrote on last edited by
    #7

    a=0 b=2 c=0

    harold aproot wrote:

    which is a nice try but tests whether all of them are zero instead of any of them

    [edit] ouch, misread. :-O

    Oxfords English < Official CCC Players Dictionary Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D

    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?

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

      harold aptroot wrote:

      which is wrong in general, bonus points if you know why

      Overflow could cause multiple non-zero values to appear to equal zero?

      Thou mewling ill-breeding pignut!

      L 1 Reply Last reply
      0
      • A AspDotNetDev

        harold aptroot wrote:

        which is wrong in general, bonus points if you know why

        Overflow could cause multiple non-zero values to appear to equal zero?

        Thou mewling ill-breeding pignut!

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

        Points awarded :)

        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
          #10

          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

          L A 2 Replies 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?

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

            harold aptroot wrote:

            a * b * c == 0 (which is wrong in general, bonus points if you know why)

            [edit]Sh*t! Too late for the party :( [/edit]

            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

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

              Point awarded :) Very good. I had a different one though, that used fewer operations, can you find that one?

              L 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?

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

                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!

                Sander RosselS L C 4 Replies Last reply
                0
                • M Mladen Jankovic

                  harold aptroot wrote:

                  a * b * c == 0 (which is wrong in general, bonus points if you know why)

                  [edit]Sh*t! Too late for the party :( [/edit]

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

                  Or to put it differently, the ring Z/232Z has zero divisors because 232 is not a prime number.

                  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!

                    Sander RosselS Offline
                    Sander RosselS Offline
                    Sander Rossel
                    wrote on last edited by
                    #15

                    My thought exactly! I don't think Harold would approve of it though :laugh:

                    It's an OO world.

                    public class Naerling : Lazy<Person>{
                    public void DoWork(){ throw new NotImplementedException(); }
                    }

                    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!

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

                      Ok you win :laugh:

                      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?

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

                        bloody hell. (a & b & c) == 0 ?

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          Point awarded :) Very good. I had a different one though, that used fewer operations, can you find that one?

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

                          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 A 2 Replies Last reply
                          0
                          • M Mladen Jankovic

                            bloody hell. (a & b & c) == 0 ?

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

                            What if they're all different powers of two?

                            M 1 Reply Last reply
                            0
                            • 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
                                          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