Lars Andersson Member Licensed User Longtime User Jul 26, 2014 #1 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
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
udg Expert Licensed User Longtime User Jul 26, 2014 #2 Hi, have a look at this post. And on Bit object in general: http://www.b4x.com/android/help/core.html#bit Umberto Upvote 0
Hi, have a look at this post. And on Bit object in general: http://www.b4x.com/android/help/core.html#bit Umberto
sirjo66 Well-Known Member Licensed User Longtime User Jul 26, 2014 #3 I don't know if it works but....... Code: Copy 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
I don't know if it works but....... Code: Copy 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
Lars Andersson Member Licensed User Longtime User Jul 26, 2014 #4 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 Jul 26, 2014 #5 To set a bit use OR. From example above Code: Copy 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
To set a bit use OR. From example above Code: Copy 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
Lars Andersson Member Licensed User Longtime User Jul 27, 2014 #6 OK, thanks! My suggestion earlier maybe a challange for Erel... ;-) Lars Upvote 0