Author Topic: Isn't _SETBIT broken?  (Read 3150 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Isn't _SETBIT broken?
« 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:

Code: [Select]
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.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Isn't _SETBIT broken?
« Reply #1 on: July 12, 2021, 03:34:38 pm »
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.
Granted after becoming radioactive I only have a half-life!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Isn't _SETBIT broken?
« Reply #2 on: July 12, 2021, 03:53:29 pm »
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:

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

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Isn't _SETBIT broken?
« Reply #3 on: July 12, 2021, 05:07:36 pm »
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.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Isn't _SETBIT broken?
« Reply #4 on: July 13, 2021, 03:19:27 am »
Seems to work with

 
Quote
DIM A AS _UNSIGNED _BYTE
b = _SETBIT(A, 0)
PRINT b

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Isn't _SETBIT broken?
« Reply #5 on: July 15, 2021, 03:45:47 am »