FAQ  •  Register  •  Login

Bool

<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Mon Apr 30, 2012 9:31 pm

Bool

Are bools supported by zxbasic?

If so, would an array of bools take up only 1/8 of the room as an array of Bytes?
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue May 01, 2012 10:51 am

Re: Bool

Bools are not supported, and if they were, for performance reasons they will be bytes or uBytes. If you want syntactic sugar, just #define Bool uByte will do.
<<

LCD

Posts: 596

Joined: Fri Feb 13, 2009 3:11 pm

Location: Vienna, Austria

Post Tue May 01, 2012 8:38 pm

Re: Bool

Bools are better stored in Memory "by hand", where you can manipulate single bits of each byte really easy since the compiler supports binary operations BOR,BAND,... and bit shifting - SHL,SHR. I'm fine with this. So if you want to save memory and keep good speed, this is the best way.
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue May 01, 2012 10:37 pm

Re: Bool

so one byte is 8 bits or 8 'switches'?

so how would you say read or write a 'switch' in a byte? say I wanted to read switch 4 and read switch 5

if i have a byte array that is the size of the screen that is 32,24 which is 768 bytes

if I set up an array of bools it would only be 96 bytes :D
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue May 01, 2012 11:20 pm

Re: Bool

slenkar wrote:so one byte is 8 bits or 8 'switches'?

so how would you say read or write a 'switch' in a byte? say I wanted to read switch 4 and read switch 5

if i have a byte array that is the size of the screen that is 32,24 which is 768 bytes

if I set up an array of bools it would only be 96 bytes :D

ZX Basic supports BAND and bit shifting with SHR and SHL. In this case, you should use an array of 24 x 4 bytes => 24 Ulong and do the following (Note: This is *UNTESTED* :oops: )
  Code:
DIM ar(23, 3) as Ubyte ' 24 x 32 bits.

#define BitTest(x, i)  Cast(Ubyte, 1 & (x >> i))
#define GetBool(a, i, j)  BitTest(a(ii, j >> 3), j & 7)
#define SetBool(a, i, j) a(i, j >> 3)  a(i, j >> 3) | 1 << (j & 7)
#define ResetBool(a, i, j) a(i, j >> 3)  SetBool(a, i, j): a(i, j >> 3) = a(i, j >> 3) ~ 1 << (j & 7)

IF GetBool(ar, 3, 20) THEN
...
END IF

Bittest does not come with ZX Basic, but is defined in a similar way it is here
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Wed May 02, 2012 12:12 am

Re: Bool

  Code:
DIM ar(23, 3) as Ubyte ' 24 x 32 bits.

just curious, (in case I want to make an array of a different size)
how does 23,3 translate into 24,32

  Code:
IF GetBool(ar, 3, 20) THEN
...
END IF

so the numbers 3,20 correspond to my screen co-ordinates?

thanks for the help
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed May 02, 2012 6:56 am

Re: Bool

slenkar wrote:
  Code:
DIM ar(23, 3) as Ubyte ' 24 x 32 bits.

just curious, (in case I want to make an array of a different size)
how does 23,3 translate into 24,32

The array goes from 0 to 23 (24 rows) and from 0 to 3 (4 bytes = 32bits). So you have 24 x 32bits (1 bit per screen cell).
slenkar wrote:
  Code:
IF GetBool(ar, 3, 20) THEN
...
END IF

so the numbers 3,20 correspond to my screen co-ordinates?

thanks for the help

Yep, the are screen coordinates, and ar your byte array.
<<

LCD

Posts: 596

Joined: Fri Feb 13, 2009 3:11 pm

Location: Vienna, Austria

Post Wed May 02, 2012 8:07 pm

Re: Bool

slenkar wrote:so one byte is 8 bits or 8 'switches'?

so how would you say read or write a 'switch' in a byte? say I wanted to read switch 4 and read switch 5

Yes.
Reading:
  Code:
switch4=(byte>>4)&1

  Code:
switch5=(byte>>5)&1

Writing:
  Code:
poke address,(peek address)|(switch4<<4)

  Code:
poke address,(peek address)|(switch5<<5)

Note, I used parenhisis because I noted that bit shifting has lower priority in ZXBC than binary AND opposed to FreeBASIC and other BASIC dialects
  Code:
switch4=byte>>4&1
is not working as expected because Boriel is using C priority. See this:
post2064.html#p2064
I found the english precedence table of Freebasic which shows that SHL and SHR have higher priority than +/- and binary AND, OR etc.
http://www.freebasic.net/wiki/wikka.php ... Precedence
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
<<

slenkar

Posts: 282

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Wed May 02, 2012 11:24 pm

Re: Bool

thanks, what is the value of 'address'?
on the line that starts with:poke address
<<

LCD

Posts: 596

Joined: Fri Feb 13, 2009 3:11 pm

Location: Vienna, Austria

Post Thu May 03, 2012 8:41 am

Re: Bool

slenkar wrote:thanks, what is the value of 'address'?
on the line that starts with:poke address

Any address or pointer...
  Code:
poke @booltable+1,(peek booltable+1)|(switch4<<4) 'use second bytes 4 th bit
end

booltable:
asm
  defb 0,0,0,0,0,0,0,0  ;8 bytes=64 bool bits
end asm

Boriel also wanted to add SETBIT, RESETBIT and TESTBIT commands, but existing binary operations are good enough because you can manipulate multiple bits at once.
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Aug 07, 2012 9:40 am

Re: Bool

LCD wrote:
slenkar wrote:thanks, what is the value of 'address'?
on the line that starts with:poke address

Any address or pointer...
  Code:
poke @booltable+1,(peek booltable+1)|(switch4<<4) 'use second bytes 4 th bit
end

booltable:
asm
  defb 0,0,0,0,0,0,0,0  ;8 bytes=64 bool bits
end asm

Boriel also wanted to add SETBIT, RESETBIT and TESTBIT commands, but existing binary operations are good enough because you can manipulate multiple bits at once.

In fact, FreeBASIC implements them as Macros. You can also implement them as macros using #define as explained above (I've updated ZX Basic operator priorities to reflect FreeBasic's ones, but you can always enforce priorities using parenthesis; this won't affect performance).
<<

LCD

Posts: 596

Joined: Fri Feb 13, 2009 3:11 pm

Location: Vienna, Austria

Post Tue Aug 07, 2012 10:43 am

Re: Bool

boriel wrote:You can also implement them as macros using #define as explained above (I've updated ZX Basic operator priorities to reflect FreeBasic's ones, but you can always enforce priorities using parenthesis; this won't affect performance).

Great!!! I will check it today.
------------------------------------------------------------
http://lcd-one.da.ru redirector is dead
Visit my http://members.inode.at/838331/index.html home page!

Return to Wishlist

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO