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 Maximilien

    if (( a +b+c ) == 0)

    Watched code never compiles.

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

    Unless the curious spacing changes the semantics of addition, I don't think so.

    1 Reply Last reply
    0
    • D Dalek Dave

      a=2 b=-1 c=-1 ?

      ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] Trolls[^]

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #5

      :~

      Watched code never compiles.

      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
        Minion no 5
        wrote on last edited by
        #6

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

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

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

                              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
                              • 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
                                #18

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

                                L 1 Reply 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
                                          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