Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.acorn.programmer > #620
| Date | 2011-08-14 15:02 +0100 |
|---|---|
| From | Matthew Phillips <spam2011m@yahoo.co.uk> |
| Newsgroups | comp.sys.acorn.programmer |
| Subject | Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. |
| Message-ID | <24c8a80252.Matthew@sinenomine.freeserve.co.uk> (permalink) |
| References | <d515591d-7af9-4e9d-afb9-110bdc721e53@a31g2000vbt.googlegroups.com> |
In message <d515591d-7af9-4e9d-afb9-110bdc721e53@a31g2000vbt.googlegroups.com> on 14 Aug 2011 Gazza wrote: > I'm looking for a routine that performs the equivalent of C/C++ > sizeof(<num>) function. This returns the number of bits taken up by > the number currently stored, NOT the memory footprint of the variable. Well, in C sizeof *does* return the memory footprint of the whole variable. > Dim j as INT32 > Dim k as INT32 > j=2 > k=sizeof(j) > > Variable "k" here would return 2, NOT 32. (To express 2 in binary > requires 2 bits.) One way to do this is using logarithms. Anyone who studied maths to a decent level before the advent of the pocket calculator will be aware that with base-10 logarithms, the log of any number greater than 1 and less than 10 will be between 0 and 1. The log of any number between 10 and 100 will be between 1 and 2, and so on. For expressing numbers in binary, you want to look at logarithms to base 2, which can easily be found from base 10 logarithms by dividing by the logarithm of 2. So: DEF FNbits(i%) =1+INT(LOG(i%)/LOG(2)) You'd actually need to be a bit more careful than that, because passing in i% equal to zero or less than 0 would cause nasty things to happen, so you'll need to test for those cases and take whatever action you feel is appropriate. However, using logs, while easily generalisable to other number bases, is not the fastest way of determining the number of bits required by any means. Whether you need to use a faster method depends on the profile of usage in your application. A faster method would probably involve repeated right-shifting of the number to count the number of bits required: DEFFNbits(i%) LOCAL n% n%=0 WHILE i% i%=i%>>1 n%+=1 ENDWHILE =n% I have not tested which is actually faster in BASIC. -- Matthew Phillips Durham
Back to comp.sys.acorn.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Gazza <usenet@garethlock.com> - 2011-08-14 04:39 -0700
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Michael Gerbracht <nospam@cityweb.de> - 2011-08-14 14:17 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Matthew Phillips <spam2011m@yahoo.co.uk> - 2011-08-14 15:02 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. druck <news@druck.org.uk> - 2011-08-14 15:23 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Gazza <usenet@garethlock.com> - 2011-08-14 08:43 -0700
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. cferris@freeRemoveuk.com.invalid - 2011-08-15 09:24 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. druck <news@druck.org.uk> - 2011-08-15 10:04 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Matthew Phillips <spam2011m@yahoo.co.uk> - 2011-08-15 20:07 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Rick Murray <heyrickmail-usenet@yahoo.co.uk> - 2011-08-14 18:48 +0200
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Stewart Brodie <stewart.brodie@ntlworld.com> - 2011-08-14 18:27 +0100
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. jeff <jeffrey.a.doggett@gmail.com> - 2011-08-16 01:00 -0700
Re: Is there an equivalent piece of BASIC that will return the size (in bits) of a number. Rick Murray <heyrickmail-usenet@yahoo.co.uk> - 2011-08-16 18:39 +0200
csiph-web