Bit field support in PB Structures.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Bit field support in PB Structures.

Post by wilbert »

Thunder93 wrote:I don't know if there's a better way to unset and set. However, here's what I have..
Don't know what is better but here's another way.

Code: Select all

Macro BitSet(var, bit)
  var | (1 << bit)
EndMacro

Macro BitReset(var, bit)
  var & ~(1 << bit)
EndMacro

Macro BitToggle(var, bit)
  var ! (1 << bit)
EndMacro

Macro BitGet(var, bit)
  (var >> bit) & 1
EndMacro


; *** test ***

Structure myStruct
  bitfields.w
  long.l
EndStructure  

Global a
Global b.myStruct

a = -1

BitReset(a, 2)
BitSet(b\bitfields, 1)

Debug BitGet(a, 1)
Debug BitGet(a, 2)

Debug BitGet(b\bitfields, 1)
Debug BitGet(b\bitfields, 2)

BitToggle(a, 1)
Debug BitGet(a, 1)

BitToggle(a, 1)
Debug BitGet(a, 1)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Bit field support in PB Structures.

Post by idle »

made an initial equivalent to c bit fields, with some limitations.
http://www.purebasic.fr/english/viewtop ... 12&t=65518
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
NicknameFJ
User
User
Posts: 90
Joined: Tue Mar 17, 2009 6:36 pm
Location: Germany

Re: Bit field support in PB Structures.

Post by NicknameFJ »

Thorium wrote: -copy or move a subset of the bit field

@Thorium:

Take a look at my CopyBitField Procedure in the german forum if you need it.

Look here

OK, you´re right, a native command would be great.


NicknameFJ
PS: Sorry for my weird english, but english is not my native language.



Image
Post Reply