Originally posted by Dale Yarker
View Post
Announcement
Collapse
No announcement yet.
Find Lowest Set Bit
Collapse
X

FIX(LOG2(q)), where q > 0, is the value of the exponent q would have if it were converted to a 754 IEEE floatingpoint 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

Originally posted by Mark Bullivant View PostFIX(LOG2(q)), where q > 0, is the value of the exponent q would have if it were converted to a 754 IEEE floatingpoint 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

Sheesh. Next we are going to have a pseudocontest 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

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 oddas 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

Originally posted by Johan Klassen View Postsometimes 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
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

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

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