Description of bit type a bit unclear

This forum is dedicated to those who want support programming in ugBASIC.
Questo forum è dedicato a chi desidera supporto per programmare in ugBASIC.
Ce forum est dédié à ceux qui souhaitent prendre en charge la programmation en ugBASIC.
Post Reply
Hrundy
Posts: 1
Joined: Tue Mar 25, 2025 7:38 pm

Description of bit type a bit unclear

Post by Hrundy »

Hello,

I refer to https://ugbasic.iwashere.eu/manual/datatypes, especially the text:

x = y HAS BIT 4 : ' this is like y AND 32
x = y HAS NOT BIT 4 : ' this is like y AND 239
x = BIT 4 OF y : ' this is like y AND 32

it seems to me that the second line is somewhat wrong in the comment. I would think that this stands for "y and 223" (239 is 255-16, not -32)? Semantics would then be that this one bit is not set? Or should it mean that all the other bits are set? This is not so well described, I must say. It looks from this formula that all the other bits are meant to be set, no matter whether bit 4 is set or not. Is that correct?
spotlessmind1975
Site Admin
Posts: 204
Joined: Fri Oct 06, 2023 8:25 pm

Re: Description of bit type a bit unclear

Post by spotlessmind1975 »

Hi Hrundy and welcome! :D
Hrundy wrote: Wed Mar 26, 2025 12:08 pm it seems to me that the second line is somewhat wrong in the comment. I would think that this stands for "y and 223" (239 is 255-16, not -32)?
I think you are right about the comment, but not on the value, that seems not exact. Infact, the HAS NOT BIT instruction will check a bit off (0) by checking the state of a specific bit inside the given data. The bit position is zero based, and starts from the less significative bit and go on. In implementation terms, the instruction is like NOT( x HAS BIT n). Since x HAS BIT n can be expressed as x AND 2^n, and NOT( a AND b) can be expressed as a AND NOT b. So, when we are talking about BYTE data type:

Code: Select all

x = y HAS NOT BIT 4 
x = NOT (y HAS BIT 4)
x = NOT (y AND 2^4)
x = NOT (y AND 16)
x = y AND NOT %00010000
x = y AND %11101111
x = y AND 239
Hrundy wrote: Wed Mar 26, 2025 12:08 pmSemantics would then be that this one bit is not set?
The desired semantics would be to test that a bit is zero. Honestly, since you pointed this out to me, I'm not so sure that it's computed correctly. We should do some test cases to see that.
Hrundy wrote: Wed Mar 26, 2025 12:08 pmOr should it mean that all the other bits are set? This is not so well described, I must say. It looks from this formula that all the other bits are meant to be set, no matter whether bit 4 is set or not. Is that correct?
You are absolutely right about this. At the moment the current implementation, which I repeat I do not know if it is completely correct, is the one I illustrated above. Since the semantics should be to test that a bit is zero, in reality I believe that the correct function is:

Code: Select all

x = (y AND 2^n) = 0
At this point I ask for confirmation to fix the behavior.

Thank you again!
Post Reply