Binary input

pavelpivo

Member
Licensed User
Longtime User
Hi,
is there any possibility to input in code a binary number.
Something like
Dim i as Int
.
.
.
i=0b110011

As far as I found, hex can be entered by prefix 0x, but 0b doesn't work.

Why I need this? I need to use some value as status flag and entering this in hex or decimal may reproduce a typing error.
Thank you for reply.
 

stevel05

Expert
Licensed User
Longtime User
I just stumbled across this post which I think answers your question.
 
Upvote 0

pavelpivo

Member
Licensed User
Longtime User
Just for case, that anybody will need this feature in future:
The flag bit can be easily set by:
B4X:
i=Bit.Or(i,Power(2,a))
where i is status word and a is bit, which has to be set
Note that 0 is first bit, 1 is second bit etc.

Bit can be cleared by:
B4X:
i=Bit.And(i,Power(2,b)-1-Power(2,a))
for b bit long status word.
Note that b can be higher than length of word, but if it is lower, then all higher bits will be cleared.

Bit can be read by:
B4X:
flagbit=Bit.And(i,Power(2,a))
This will return 2^a if bit is set. If this is not acceptable add following if
B4X:
If flagbit>0 Then flagbit=1
so the code will look
B4X:
flagbit=Bit.And(i,Power(2,a))
If flagbit>0 Then flagbit=1
 
Upvote 0

pavelpivo

Member
Licensed User
Longtime User
Thank you for your input Erel.
I have another question. I know that power takes a lot of CPU cycles to calculate. As I want to use this set/clear instructions in cycle that repeats more than 100.000x I am looking for something really fast. What do you think, what is faster? Bit.ShiftLeft or once create an array of integers (element(a)=Power(2, a)) and then instead of Power(2, a) use element(a) in cycles?
 
Upvote 0
Top