how do I chande one bit in variable
-
- User
- Posts: 46
- Joined: Sat Jan 08, 2005 7:46 pm
how do I chande one bit in variable
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
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
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
I may look like a mule, but I'm not a complete ass.
-
- User
- Posts: 46
- Joined: Sat Jan 08, 2005 7:46 pm
-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
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
%1>>1+1*1/1-1!1|1&1<<$1=1
-
- User
- Posts: 67
- Joined: Thu Nov 30, 2006 4:57 pm
- Location: I long for the absolute loneliness of the death
It must delete external (), produces a compiler errorFroggerprogger wrote:Macro GetBit(Value, bit)
(Value&(1<<bit))>>bit ;Translates as 'value' ANDed with 2^bit and shifted back to bitposition 0
EndMacro
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
-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
You must not delete external brackets (), because >> has a higher precedence than &, so a wrong result would be given, see here:
And no compiler-errors here with PB4
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)
%1>>1+1*1/1-1!1|1&1<<$1=1
-
- User
- Posts: 67
- Joined: Thu Nov 30, 2006 4:57 pm
- Location: I long for the absolute loneliness of the death
I'm on windows 2000 PB4.01 and command line compiler.
I'm not using /DEBUGGER, but old OpenConsole()...printn() structure.
At compiler time:
Could be a compiler bug?
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
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)
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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:
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.
Code: Select all
If GetBit(var_a, 3)
; do something
EndIf
BERESHEIT