Announcement

Collapse
No announcement yet.

Find Lowest Set Bit

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Originally posted by Dale Yarker View Post
    Code:
    x AND -(x AND 1)
    if x is odd it returns x, if x is even it returns 0

    Thinking on 2nd one.
    Yep,

    Comment


    • #22
      Originally posted by Dale Yarker View Post
      2nd
      x XOR x is less steps
      That's not what 2nd does, x XOR x always returns 0
      HInt: 2nd is similar to 1st

      Comment


      • #23
        returns even or 0
        got me!
        Dale

        Comment


        • #24
          FIX(LOG2(q)), where q > 0, is the value of the exponent q would have if it were converted to a 754 IEEE floating-point number

          So...

          Code:
          'Returns the bit number of the MSb of a positive integer...
          FUNCTION PBMAIN () AS LONG
            LOCAL q AS QUAD
            LOCAL x AS EXT
          DO
            INPUT q
          
            x = q
          
            ?PEEK(WORD, VARPTR(x) + 8)  - &H3fff
          
            LOOP
          
          END FUNCTION

          Comment


          • #25
            Originally posted by Dale Yarker View Post
            returns even or 0
            got me!

            Comment


            • #26
              Originally posted by Mark Bullivant View Post
              FIX(LOG2(q)), where q > 0, is the value of the exponent q would have if it were converted to a 754 IEEE floating-point number

              There is an FPU instruction which extracts the Mantissa and Exponent from a floating point number.
              Code:
              'PBCC 6 program
              #COMPILE EXE
              #DIM ALL
              
              FUNCTION PBMAIN () AS LONG
              #REGISTER NONE
              
              LOCAL Number AS SINGLE  'or any other numeric data type
              LOCAL Exponent AS LONG
              
              
              FOR Number = 1 TO 17
              
                  !fld Number     'use FLD to load a FP number
                '  !fild Number    'use FILD to load an integer
                  !fxtract        'extract the mantissa into ST(0) and exponent into ST(1)
                  !fstp st(0)     'throw away the mantissa
                  !fistp Exponent 'pop the exponent into the integer result
              
              PRINT Number,Exponent
              
              NEXT
              
              
              WAITKEY$
              
              END FUNCTION

              Comment


              • #27
                Sheesh. Next we are going to have a pseudo-contest to see who can use IMP in the most unwieldy way.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #28
                  Dale

                  Comment


                  • #29
                    Originally posted by Michael Mattias View Post
                    Sheesh. Next we are going to have a pseudo-contest to see who can use IMP in the most unwieldy way.
                    You are slipping. You neglected to include an irrelevent, sarcastic reference to EQV.

                    Comment


                    • #30
                      sometimes you need to know whether a number is odd or even, you can use n mod 2 but this might be a tad bit faster, not tested
                      simply if (n and -n)=1 means it's odd
                      as the salmon fish is compelled to go back to it's birthplace to spawn, so comes a day when man is compelled to go back to it's source.. GOD

                      Comment


                      • #31
                        Originally posted by Johan Klassen View Post
                        sometimes you need to know whether a number is odd or even, you can use n mod 2 but this might be a tad bit faster, not tested
                        simply if (n and -n)=1 means it's odd
                        Simpler: IF (x AND 1) THEN ...' x is odd.
                        I think you will find that AND is about 3 times faster than MOD

                        A similar concept was discussed recently in another thread where integer division, MOD and AND were compared:
                        https://forum.powerbasic.com/forum/u...970#post807970

                        Comment


                        • #32
                          MACRO FUNCTION IsOdd (n)
                          END MACRO= -(n AND 1)

                          MACRO FUNCTION IsEven (n)
                          END MACRO= ISFALSE (n AND 1)
                          Last edited by Rodney Hicks; 9 Jun 2021, 05:55 AM. Reason: Fixed as per post #33.
                          Rod
                          In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                          Comment


                          • #33
                            END MACRO= - (n AND 1)
                            would be preferable since then IsOdd() and IsEven() both return -1 for True.

                            Comment


                            • #34
                              To summarise:
                              Code:
                              ' Macros don't work with negative values (i.e signed integers must be >= 0)  
                              ' Lo/Hi bit numbers return -1 for a value of 0.
                              
                              MACRO FUNCTION IsOdd (x)
                              END MACRO= -(x AND 1)
                              
                              MACRO FUNCTION IsEven (x)
                              END MACRO= ISFALSE (x AND 1)
                              
                              MACRO FUNCTION LoBitNum(x)
                              MACROTEMP n
                              LOCAL n AS QUAD
                              IF x = 0 THEN n = -1 ELSE n = FIX(LOG2(x AND -x))
                              END MACRO = n
                              
                              MACRO FUNCTION LoBitVal(x)
                              MACROTEMP n
                              LOCAL n AS QUAD
                              IF x = 0 THEN n = 0 ELSE n = (x AND -x)
                              END MACRO = n
                              
                              MACRO FUNCTION HiBitNum(x)
                              MACROTEMP n
                              LOCAL n AS QUAD
                              IF x = 0 THEN n = -1 ELSE n = FIX(LOG2(x))
                              END MACRO = n
                              
                              MACRO FUNCTION HiBitVal(x)
                              MACROTEMP n
                              LOCAL n AS QUAD
                              IF x = 0 THEN n = 0 ELSE n = 2^FIX(LOG2(x))
                              END MACRO = n
                              
                              MACRO FUNCTION BitsSet(x)
                              MACROTEMP y,n
                              LOCAL y, n AS QUAD
                              y = x
                              WHILE y > 0
                              y AND= (y -1)
                              INCR n
                              WEND
                              END MACRO = n

                              Comment


                              • #35
                                You are slipping. You neglected to include an irrelevent[SIC], sarcastic reference to EQV.
                                Pick one:
                                • That's because I've used IMP. I've never used EQV.
                                • I missed it in the help file.
                                • Senior Moment
                                MCM
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #36
                                  Originally posted by Michael Mattias View Post
                                  That's because I've used IMP. I've never used EQV.
                                  I've never used either because it's quicker and more readable to use IF..THEN than it is to look up what IMP and EQV do.

                                  Comment


                                  • #37
                                    Thanks for that list Stuart!
                                    Rod
                                    In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

                                    Comment

                                    Working...
                                    X