Android Question Bits from byte.

Lars Andersson

Member
Licensed User
Longtime User
What is the simplest way to get or set single bits in a byte?

Ok, maybe this shouldn't be under questions but.... I'm used to use the following (MCU programming)
Ex:
Dim bit0, bit1 as boolean
Dim byte as byte
bit0 = byte.0 'Get bit 0 in byte
byte.1 = bit1 'Set bit 1 in byte

Lars
 

sirjo66

Well-Known Member
Licensed User
Longtime User
I don't know if it works but.......
B4X:
Dim bit0, bit1, bit2 as boolean
Dim mybyte as byte
bit0 = Bit.And(mybyte, 1) 'Get bit 0 in byte
bit1 = Bit.And(mybyte, 2) 'Get bit 1 in byte
bit2 = Bit.And(mybyte, 4) 'Get bit 2 in byte
.... and so on

Sergio
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
Thanks Sergio,
Getting close to the simplicity I'm used to but to set the value?

Lars
 
Upvote 0

udg

Expert
Licensed User
Longtime User
To set a bit use OR.

From example above
B4X:
mbyte = Bit.Or(mbyte, 4) 'sets bit 2

But Erel's SetBit function showed on first link in post#2 is better and of more general use.

Umberto
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
OK, thanks!
My suggestion earlier maybe a challange for Erel... ;-)

Lars
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…