Page 1 of 1
how do I chande one bit in variable
Posted: Fri Dec 01, 2006 11:09 am
by martin66119
Hi to all PB-user!
In my program I have to change one bit in a register. For example
Var_a.b = %10001010:
I like to change bit number 3 of Var_a depending on an third bit of other byte by using the funktions or, and, xor, not ....
thanks a lotI in advance;
Martin
Posted: Fri Dec 01, 2006 11:27 am
by srod
Hi, the macros below count bits from 0, 1, 2, ... etc.
Code: Select all
Macro SetBit(value, bit)
value|1<<bit ;Translates as 'value' ORed with 2^bit
EndMacro
Macro ClearBit(value, bit)
value&~(1<<bit) ;Translates as 'value' ANDed with NOT(2^bit)
EndMacro
;Examples:
Var_a.b = %00000001
SetBit(Var_a, 3)
Debug var_a
ClearBit(Var_a, 3)
Debug var_a
Posted: Fri Dec 01, 2006 12:53 pm
by martin66119
Thank you very much!
It is exactly what I need for my procedure!
Martin
Posted: Fri Dec 01, 2006 1:27 pm
by srod
You're welcome.
Posted: Fri Dec 01, 2006 1:59 pm
by Froggerprogger
In addition here's a ToggleBit that toggles the given bit between 0 and 1 regardless of what it has been before, and a GetBit:
Code: Select all
Macro ToggleBit(Value, bit)
Value!(1<<bit) ;Translates as 'value' XORed with 2^bit
EndMacro
Macro GetBit(Value, bit)
(Value&(1<<bit))>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
Posted: Fri Dec 01, 2006 4:07 pm
by Max
Froggerprogger wrote:Macro GetBit(Value, bit)
(Value&(1<<bit))>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
It must delete external (), produces a compiler error
Code: Select all
Macro GetBit(Value, bit)
Value&(1<<bit)>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
Posted: Fri Dec 01, 2006 5:08 pm
by Froggerprogger
You must not delete external brackets (), because >> has a higher precedence than &, so a wrong result would be given, see here:
Code: Select all
Macro GetBit(Value, bit)
(Value&(1<<bit))>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
Macro GetBitWrong(Value, bit)
Value&(1<<bit)>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
a.l = %10
Debug GetBit(a, 1)
Debug GetBitWrong(a, 1)
And no compiler-errors here with PB4
Posted: Mon Dec 04, 2006 10:02 am
by Max
I'm on windows 2000 PB4.01 and command line compiler.
I'm not using /DEBUGGER, but old OpenConsole()...printn() structure.
Code: Select all
Macro SetonBit(Value, bit)
Value|1<<bit ;Translates as 'value' ORed with 2^bit
EndMacro
Macro SetoffBit(Value, bit)
Value&~(1<<bit) ;Translates as 'value' ANDed with NOT(2^bit)
EndMacro
Macro ToggleBit(Value, bit)
Value!(1<<bit) ;Translates as 'value' XORed with 2^bit
EndMacro
Macro GetBit(Value, bit)
(Value&(1<<bit))>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
OpenConsole()
Var_a.b = %00110001
printn(str(var_a))
SetonBit(Var_a, 3)
printn(str(var_a))
SetoffBit(Var_a, 0)
printn(str(var_a))
ToggleBit(Var_a, 3)
printn(str(var_a))
GetBit(Var_a, 3)
printn(str(var_a))
input()
END
At compiler time:
Code: Select all
******************************************
PureBasic v4.00 (Windows - x86)
******************************************
Compiling c:\MP\MPETC\setbit.PB
Loading external libraries...
Starting compilation...
Error: Line 27 - Syntax error!
at line 1 of the expanded macro (Macro.out)
Could be a compiler bug?
Posted: Mon Dec 04, 2006 11:38 am
by netmaestro
Written correctly, as Froggerprogger has done, the macro's not meant to stand by itself. It produces a result that you have to do something with, such as:
Code: Select all
If GetBit(var_a, 3)
; do something
EndIf
Max, in your code your GetBit call is all by itself, which is indeed a syntax error. Froggerprogger's is working because he has Debug GetBit(a, 1). In fact the macro is useless anyway if you don't do something with the result, so it should always work in a real app.
Posted: Mon Dec 04, 2006 1:16 pm
by Max