Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:03 07 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Address a Bit in a Byte

Author Message
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 724
Posted: 10:03am 08 Sep 2016
Copy link to clipboard 
Print this post

Hi,

Is this possible in mmbasic?

DIM A as Byte

Bit1 = Byte.1
Bit2 = Byte.2

.
.
.
.
Edited by atmega8 2016-09-09
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4036
Posted: 10:40am 08 Sep 2016
Copy link to clipboard 
Print this post

Not like that, but you can do things like
bitN = value AND (1 << N)

The parens aren't needed but I would say more readable.

(not tested, sorry)

Or simply
bit0 (bit1 if you prefer that terminology) = value AND 1
bit1 (bit2 if you prefer that terminology) = value AND 2
bit2 (bit3 if you prefer that terminology) = value AND 4

hmm, if you want them each to be 0 or 1 then you need more like
bitN = (value >> N) AND 1

JohnEdited by JohnS 2016-09-09
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 12:53am 22 Sep 2016
Copy link to clipboard 
Print this post

Some code I use a lot... any use?

FUNCTION BitSet(value,bit)
BitSet=value or (2^bit)
end function


FUNCTION BitRes(value,bit)
BitRes=(value or (2^bit)) xor (2^bit)
end function


FUNCTION BitTest(value,bit)
BitTest=sgn(value and (2^bit))
end function


you then use it like:
flags=BitSet(flags,0) to set a bit in a variable
...
flags=BitRes(flags,0) to clear a bit in a variable
...
x=BitTest(flags,0) to test a bit in a variable


etc...

all the best

h
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025