BitModule

Share your advanced PureBasic knowledge/code with the community.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: BitModule

Post by said »

Thanks idle for sharing ... very nice and useful
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: BitModule

Post by idle »

fixed a bug in the bittrie reallocation
Windows 11, Manjaro, Raspberry Pi OS
Image
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: BitModule

Post by said »

Hi Idle

looks like something is wrong with BitArray (probably other structures as well haven't tested), try the below you will see that position 14 (or 15 if we start at 1) is being set not 9!

Code: Select all

Define ar.BitModule::IBitArray
ar = BitModule::New_BitArray(20)
ar\Set(9)
Define *q.ascii, *p = ar\GetBuffer()
Define i,txt.s
For i=0 To MemorySize(*p)
    *q = *p + i
    txt + RSet( Bin(*q\a), 8, "0") + ">"
Next
Debug txt
output

Code: Select all

00000000>00000010>00000000> ....
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: BitModule

Post by Demivec »

said wrote:looks like something is wrong with BitArray (probably other structures as well haven't tested), try the below you will see that position 14 (or 15 if we start at 1) is being set not 9!
You're examining the buffer directly and not in the manner that the BitArray does.

If you use the BitArray's interface instead:

Code: Select all

Define ar.BitModule::IBitArray
ar = BitModule::New_BitArray(20)
ar\Set(9)
Define i,txt.s
For i=0 To ar\BitSize()
    txt + Str(Bool(ar\get(i) = 1))
    If i % 8 = 0 And i > 0
      txt + ">"
    EndIf
Next
Debug txt
The output produced looks like:

Code: Select all

000000000>10000000>00000000> ....
Everything looks as it should.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: BitModule

Post by said »

Thanks Demivec for clarifying, now i looked in detail on what's going on and i see that each byte is being processed separately. You are right, yes it is working fine but honestly I was not expecting this ... (should never use blindly anything :oops: )
I was planning to use this to store blocks in files that could be read by other applications, this somehow put restrictions on the usage of this great module :(
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: BitModule

Post by idle »

Hi Said, it's easy enough fix I think

if you change the set and get parts after the "<<" to (7-(index & $07)

Code: Select all

  *ta\a | (1 << (7-(index & $07))) 
I've updated the module with the changes, seems to be ok
Windows 11, Manjaro, Raspberry Pi OS
Image
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: BitModule

Post by said »

Thanks Idle for updating the module (actually i had already changed it here to suit my needs, exactly as you are suggesting). It is your module and you know all the details, modifications coming from you are the most welcome ones :)

Thanks again for sharing this very useful work :D
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: BitModule

Post by Justin »

What is Toggle suposed to do? I thought would invert the bit value, but does nothing.

Code: Select all

EnableExplicit
Define ba.BitModule::IBitArray
ba = BitModule::New_BitArray(4)
ba\Set(2)
Debug ba\Get(2)
ba\Toggle(2)
Debug ba\Get(2)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: BitModule

Post by idle »

it's a bug left over from reversing the order, replace it with

Code: Select all

Procedure BitArray_Toggle(*this.bitbuffer,index)
   Protected *ta.Ascii 
    If ((index) >> 3) >= *this\inf\size
      *this\inf\size = index + 64
      *this\inf\BytesUsed = *this\inf\size
      *this\buf = ReAllocateMemory(*this\buf,*this\inf\size)
   EndIf
   If *this\buf
      *ta = *this\buf + ((index)>>3)
      *ta\a ! (1 << (7-(index & $07)))
   EndIf
EndProcedure   
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: BitModule

Post by idle »

v 1.3.8
Updated to pb 5.42 lts
exported hash functions
removed crc32
added asm popcount x86/x64 and SSE 4.2 for bitcount
added shift left / right (nicked from wilbert)
Windows 11, Manjaro, Raspberry Pi OS
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: BitModule

Post by davido »

@idle,
Very useful, thank you. :D
DE AA EB
Post Reply