Forum
Bool - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Wishlist (https://www.boriel.com/forum/forumdisplay.php?fid=14)
+---- Thread: Bool (/showthread.php?tid=460)



Bool - slenkar - 04-30-2012

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?


Re: Bool - boriel - 05-01-2012

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.


Re: Bool - LCD - 05-01-2012

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.


Re: Bool - slenkar - 05-01-2012

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 Big Grin


Re: Bool - boriel - 05-01-2012

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 Big Grin
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


Re: Bool - slenkar - 05-02-2012

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


Re: Bool - boriel - 05-02-2012

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.


Re: Bool - LCD - 05-02-2012

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:
<!-- l --><a class="postlink-local" href="http://www.boriel.com/forum/post2064.html#p2064">post2064.html#p2064</a><!-- l -->
I found the english precedence table of Freebasic which shows that SHL and SHR have higher priority than +/- and binary AND, OR etc.
<!-- m --><a class="postlink" href="http://www.freebasic.net/wiki/wikka.php?wakka=OpPrecedence">http://www.freebasic.net/wiki/wikka.php ... Precedence</a><!-- m -->


Re: Bool - slenkar - 05-02-2012

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


Re: Bool - LCD - 05-03-2012

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.


Re: Bool - boriel - 08-07-2012

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


Re: Bool - LCD - 08-07-2012

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.