how do I chande one bit in variable

Just starting out? Need help? Post your questions and find answers here.
martin66119
User
User
Posts: 46
Joined: Sat Jan 08, 2005 7:46 pm

how do I chande one bit in variable

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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
I may look like a mule, but I'm not a complete ass.
martin66119
User
User
Posts: 46
Joined: Sat Jan 08, 2005 7:46 pm

Post by martin66119 »

Thank you very much!
It is exactly what I need for my procedure!

Martin
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

You're welcome.
I may look like a mule, but I'm not a complete ass.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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
%1>>1+1*1/1-1!1|1&1<<$1=1
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post 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
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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
%1>>1+1*1/1-1!1|1&1<<$1=1
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post 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?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post by Max »

:D :D :D
netmaestro: You're an "all master" , not only in the net
Post Reply