Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.sys.acorn.programmer > #615 > unrolled thread

Is there an equivalent piece of BASIC that will return the size (in bits) of a number.

Started byGazza <usenet@garethlock.com>
First post2011-08-14 04:39 -0700
Last post2011-08-16 18:39 +0200
Articles 12 — 8 participants

Back to article view | Back to comp.sys.acorn.programmer


Contents

  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

#615 — Is there an equivalent piece of BASIC that will return the size (in bits) of a number.

FromGazza <usenet@garethlock.com>
Date2011-08-14 04:39 -0700
SubjectIs there an equivalent piece of BASIC that will return the size (in bits) of a number.
Message-ID<d515591d-7af9-4e9d-afb9-110bdc721e53@a31g2000vbt.googlegroups.com>
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.
So...

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.)

Thanks.

[toc] | [next] | [standalone]


#618

FromMichael Gerbracht <nospam@cityweb.de>
Date2011-08-14 14:17 +0100
Message-ID<5202a4a9aanospam@cityweb.de>
In reply to#615
In article
<d515591d-7af9-4e9d-afb9-110bdc721e53@a31g2000vbt.googlegroups.com>,
   Gazza <usenet@garethlock.com> 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.
> So...

> 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.)

If I understand you correctly you would like to know how many bits are
required to store e.g. the number 15. Just calculate ln(15)/ln(2) and then
round to the next higher number. Here: ln(15)/ln(2) = 3.91, so you need 4
bits to store the number.

Michael

-- 
Please replace nospam by m.gerbracht when replying by e-mail

[toc] | [prev] | [next] | [standalone]


#620

FromMatthew Phillips <spam2011m@yahoo.co.uk>
Date2011-08-14 15:02 +0100
Message-ID<24c8a80252.Matthew@sinenomine.freeserve.co.uk>
In reply to#615
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

[toc] | [prev] | [next] | [standalone]


#621

Fromdruck <news@druck.org.uk>
Date2011-08-14 15:23 +0100
Message-ID<j28loh$5p7$1@dont-email.me>
In reply to#615
On 14/08/2011 12:39, 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.
> So...

No it doesn't! C/C++ size of returns the number of bytes of the whole 
variable.

> Dim j as INT32
> Dim k as INT32

That is not BBC BASIC, so has no place here!

> j=2
> k=sizeof(j)
>
> Variable "k" here would return 2, NOT 32. (To express 2 in binary
> requires 2 bits.)

No sizeof function does that. What you need to do is to take the base 2 
log of the number and round up to the nearest integer.

e.g. bitsused% = (LOG(number%)/LOG(2))+0.5

---druck

[toc] | [prev] | [next] | [standalone]


#626

FromGazza <usenet@garethlock.com>
Date2011-08-14 08:43 -0700
Message-ID<b76f5e47-5006-44eb-9e50-10dbd26c768b@v7g2000vbk.googlegroups.com>
In reply to#621
Thanks to all who have answered this one. I'll give both methods a
whirl and see what happens.

[toc] | [prev] | [next] | [standalone]


#632

Fromcferris@freeRemoveuk.com.invalid
Date2011-08-15 09:24 +0100
Message-ID<56b10d0352.cferris@cferris.freeuk.com>
In reply to#621
In message <j28loh$5p7$1@dont-email.me>
          druck <news@druck.org.uk> wrote:

> On 14/08/2011 12:39, 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. So...
[snip]

> 
> No sizeof function does that. What you need to do is to take the base
> 2  log of the number and round up to the nearest integer.
> 
> e.g. bitsused% = (LOG(number%)/LOG(2))+0.5

As a small note - you can use/call the 'SharedCLib' from BASIC.
 
-- 
Colin Ferris Cornwall UK

[toc] | [prev] | [next] | [standalone]


#633

Fromdruck <news@druck.org.uk>
Date2011-08-15 10:04 +0100
Message-ID<j2anfj$un8$1@dont-email.me>
In reply to#632
On 15/08/2011 09:24, cferris@freeRemoveuk.com.invalid wrote:
> As a small note - you can use/call the 'SharedCLib' from BASIC.

It can be done, but it is moderately complex, certainly not worth it for 
a single C function - even if it worked in the way the OP thought it did!

---druck

[toc] | [prev] | [next] | [standalone]


#635

FromMatthew Phillips <spam2011m@yahoo.co.uk>
Date2011-08-15 20:07 +0100
Message-ID<7e99480352.Matthew@sinenomine.freeserve.co.uk>
In reply to#633
In message <j2anfj$un8$1@dont-email.me>
 on 15 Aug 2011 druck  wrote:

> On 15/08/2011 09:24, cferris@freeRemoveuk.com.invalid wrote:
> > As a small note - you can use/call the 'SharedCLib' from BASIC.
> 
> It can be done, but it is moderately complex, certainly not worth it for 
> a single C function - even if it worked in the way the OP thought it did!

Remember that sizeof is not actually a C function at all.

-- 
Matthew Phillips
Durham

[toc] | [prev] | [next] | [standalone]


#628

FromRick Murray <heyrickmail-usenet@yahoo.co.uk>
Date2011-08-14 18:48 +0200
Message-ID<4E47FC50.5020903@yahoo.co.uk>
In reply to#615
On 14/08/2011 13:39, Gazza wrote:

> sizeof(<num>) function. This returns the number of bits taken up by
> the number currently stored, NOT the memory footprint of the variable.

...but the memory footprint is exactly what sizeof() returns!


> 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.)

Try it on C, you'd get 32 every time. ;-)


For what you are wanting, you want to know the base 2 table, so some 
code such as:
   DEF FNsizeof(n%)
     IF (n% <   2) : =1
     IF (n% <   4) : =2
     IF (n% <   8) : =3
     IF (n% <  16) : =4
     IF (n% <  32) : =5
     IF (n% <  64) : =6
     IF (n% < 128) : =7
   =8

There's probably a sexy single-line version that can cater for up to 32 
bit, but maths isn't my strong point...


Best wishes,

Rick.

[toc] | [prev] | [next] | [standalone]


#629

FromStewart Brodie <stewart.brodie@ntlworld.com>
Date2011-08-14 18:27 +0100
Message-ID<gemini.lpxhuc0029tnj08mg.stewart.brodie@ntlworld.com>
In reply to#628
Rick Murray <heyrickmail-usenet@yahoo.co.uk> wrote:

> For what you are wanting, you want to know the base 2 table, so some 
> code such as:
>    DEF FNsizeof(n%)
>      IF (n% <   2) : =1
>      IF (n% <   4) : =2
>      IF (n% <   8) : =3
>      IF (n% <  16) : =4
>      IF (n% <  32) : =5
>      IF (n% <  64) : =6
>      IF (n% < 128) : =7
>    =8
> 
> There's probably a sexy single-line version that can cater for up to 32 
> bit, but maths isn't my strong point...

http://graphics.stanford.edu/~seander/bithacks.html is your friend.

It doesn't look like this is a one-liner, but certainly it is shorter.


-- 
Stewart Brodie

[toc] | [prev] | [next] | [standalone]


#636

Fromjeff <jeffrey.a.doggett@gmail.com>
Date2011-08-16 01:00 -0700
Message-ID<d754b2a1-fe64-4e09-826d-bfe094983118@k8g2000yqk.googlegroups.com>
In reply to#628
> Try it on C, you'd get 32 every time. ;-)

No, You'll get 4, the number of bytes.

Jeff

[toc] | [prev] | [next] | [standalone]


#639

FromRick Murray <heyrickmail-usenet@yahoo.co.uk>
Date2011-08-16 18:39 +0200
Message-ID<4e4a9d4b$0$30786$ba4acef3@reader.news.orange.fr>
In reply to#636
On 16/08/2011 10:00, jeff wrote:

>> Try it on C, you'd get 32 every time. ;-)
> No, You'll get 4, the number of bytes.

<facepalm!>

<slinks away quietly...>


Best wishes,

Rick.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.sys.acorn.programmer


csiph-web