P pavelpivo Member Licensed User Longtime User Nov 18, 2011 #1 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.
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 Nov 18, 2011 #2 I just stumbled across this post which I think answers your question. Upvote 0
P pavelpivo Member Licensed User Longtime User Nov 19, 2011 #3 Thank you, that confirms my thoughts. Upvote 0
P pavelpivo Member Licensed User Longtime User Nov 26, 2011 #4 Just for case, that anybody will need this feature in future: The flag bit can be easily set by: Code: Copy 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: Code: Copy 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: Code: Copy flagbit=Bit.And(i,Power(2,a)) This will return 2^a if bit is set. If this is not acceptable add following if Code: Copy If flagbit>0 Then flagbit=1 so the code will look Code: Copy flagbit=Bit.And(i,Power(2,a)) If flagbit>0 Then flagbit=1 Upvote 0
Just for case, that anybody will need this feature in future: The flag bit can be easily set by: Code: Copy 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: Code: Copy 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: Code: Copy flagbit=Bit.And(i,Power(2,a)) This will return 2^a if bit is set. If this is not acceptable add following if Code: Copy If flagbit>0 Then flagbit=1 so the code will look Code: Copy flagbit=Bit.And(i,Power(2,a)) If flagbit>0 Then flagbit=1
Erel B4X founder Staff member Licensed User Longtime User Nov 27, 2011 #5 It is a bit more elegant to use: Code: Copy Bit.ShiftLeft(1, a) instead of: Power(2, a) Upvote 0
P pavelpivo Member Licensed User Longtime User Nov 27, 2011 #6 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
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?
Erel B4X founder Staff member Licensed User Longtime User Nov 27, 2011 #7 Bit.ShiftLeft is extremely fast. You will not need to further optimize it. Upvote 0