QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: SMcNeill on July 12, 2021, 01:24:12 pm
-
Or is it supposed to work the way I'm seeing it work? Let me show a simple example:
A~%% = _SetBit(A~%%, 6) 'set the seventh bit of A~%%
B~%% = _SetBit(A~%%, 2) 'set the second bit of A~%%
? A~%%, B~%%
Now, from my understanding, this should simply set a bit for a variable. If we print the results, we see that A = 64 and B = 68...
From our results, it shows that what we're doing is copying the value of our internal variable, and then setting a bit over its existing values, and then we're returning that modified value back to our return variable -- which doesn't seem to be what this simple routine was meant to do at all!
Shouldn't this be a simple SUB, which we just call to set a bit?
_SETBIT variable, bit, (optional value for 0 or 1)?
Why is this a FUNCTION that only works when the return variable is the same as our internal variable? Something with this just seems intrinsically overly complicated and non-intuitive to me.
-
Unless you wanted to assign the new value to a different variable.
By all means it could be both, but its more simplistic and functional to be a function.
not sure what you mean by "Broken" though, it does what it says it will. It turns on the specified bit in the given variable and returns that value.
-
Unless you wanted to assign the new value to a different variable.
By all means it could be both, but its more simplistic and functional to be a function.
not sure what you mean by "Broken" though, it does what it says it will. It turns on the specified bit in the given variable and returns that value.
Except that’s not what it says it does:
The _SETBIT function is used to set a specified bit of a numerical value to 1 (on state).
B = _SETBIT(A, 2)
The above doesn’t set the #2 bit for A to be on. The value of A never changes.
And it’s not just setting the #2 bit of B — it’s setting ALL the set bits of A, and then setting the #2 bit, and returning that value to B.
It isn’t a _SETBIT at all — it’s more of an _ADDBIT type command.
You dont have B = B OR (2 ^ setBit); instead it’s B = A OR (2 ^ setBit).
A = &B00000001
B = _SETBIT(A, 1)
PRINT BIN$(B)
00000011 — This didn’t just set a singular bit; it copied all the preset bits of A, and then made certain a bit was on, before returning the value.
There’s a lot more than just setting a single bit going on here.
-
I see no problem in having both a function, which works exactly like Cobalt wrote, and a sub, that actually changes the bit on a given variable. We have the statement MID$ and the function MID$(), so it's just a matter of getting it there.
-
Seems to work with
DIM A AS _UNSIGNED _BYTE
b = _SETBIT(A, 0)
PRINT b
-
@SMcNeill
check this out
https://www.felixcloutier.com/x86/bts