Bit strings ? ( "Bit string datatype" )

General FreeBASIC programming questions.
ppf
Posts: 38
Joined: Oct 10, 2017 6:41

Bit strings ? ( "Bit string datatype" )

Postby ppf » Jun 19, 2019 13:47

Hi,
a special question.Is possible to work with 'bit strings' in easy way in Freebasic ?
I would like to spare significant amount of memory and rapidly increase the speed of computations.
Say, 10 bit strings with length 50000 each.

Code: Select all

dim as bit string a(10)
a(1)="01110001110100101100....."
? len a(1) >> result 50000

Now I use 'DIM as string*1 a(10), but this is highly unefficient..
Best way & solution would be conversion the string*1 to bit string for me.

Thank you.

edit:
changed 8 to 10, could be misleading..
Last edited by ppf on Jun 19, 2019 15:55, edited 1 time in total.
MrSwiss
Posts: 3220
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bit strings ?

Postby MrSwiss » Jun 19, 2019 14:21

ppf wrote:I would like to spare significant amount of memory and rapidly increase the speed of computations.
You won't achieve any one of the two because:
    1) String uses more memory, than binary numbers (int/float).
    2) String calculations must be coded by yourself entirely.
    This then means: hardly likely to be speed efficient ...
ppf wrote:Say, 10 bit strings with length 50000 each
Are you really referring to the String size, or the max. value to represent?
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: Bit strings ?

Postby srvaldez » Jun 19, 2019 14:29

@ppf
what are you trying to accomplish with bit-strings?
perhaps there's a better way.
ppf
Posts: 38
Joined: Oct 10, 2017 6:41

Re: Bit strings ?

Postby ppf » Jun 19, 2019 15:51

Right, I really talk about bit arrays, aka "Bit string datatype"
In my code I need to summarize happened events (in boolean sense happened/NOT happened equal to 0/1).
My above VERY simple example you can understand as 10 events monitored in time of 50000 seconds.
For example
event 1 would be - is sunny / is NOT sunny
event 2 would be - is rainy / is NOT rainy
event 3 would be - is windy / is NOT windy
event 4 would be - I am happy / I am NOT happy
etc., etc.
event 10 would be - 'something' happened / 'something' NOT happened

And now I need only to know a numerical sum of happened events in any time moment, in each second, how much events happened
and store it in variable happenedX(50000) as result (with value range 0 to 10 in meaning 'None of 10 events happened'
to 'everything happened')

I made a tricky code - raw data 2D array (events,seconds)

Code: Select all

 What(10,50000) as String*1 and HappenedX(50000) as integer

'summing is easy

for i= 0 to 50000
 var sum=0
 for j= 1 to 10
    sum+=valint(What(j,i))
 next
 HappenedX(i)=sum
next

then show result HappenedX() in sensible diagram.That's all.

Problem is, this goes up to millions of seconds (increasing time) to hundreds megabytes array in memory.
After that computing such array in memory is significantly slow.
Therefore manipulating with bits ,assuming, would keep the computing speed on acceptable level.
Lost Zergling
Posts: 240
Joined: Dec 02, 2011 22:51
Location: France

Re: Bit strings ?

Postby Lost Zergling » Jun 19, 2019 16:18

@ppf
First Point :
As mentionned in documentation, FB does not support Bit Ptr but Byte Ptr, this not being really a problem.
Considering 99,99999999% use cases, working bit per bit wouldn't be faster than byte per byte.
This because processors standards (in general) is to work Byte per byte or beyond.
Second point :
Consider using Byte
I may suggest easy solution : your key is : number of seconds (3)+1 lenght string representing events
No key would means 0 events, too see (depending the average number of no event).
Updating keys or removing/maintaining each seconds : an issue linked to buffer size, meaning the amout of datas to memorize or to record(2)
This would be a possible (1) solution using List engine LZLE that I had already suggested.
(1) : Probably not the best way but it would "do the job" I think.
(2) : Ps : working using timer always been an issue.
(3) : also have a look to DateValue and so on
ppf
Posts: 38
Joined: Oct 10, 2017 6:41

Re: Bit strings ? ( "Bit string datatype" )

Postby ppf » Jun 19, 2019 17:17

@Lost Zergling
thanks for LZLE tip.
No worries about timer, using external device data, adjusted perfectly.Just only data for computing.
MrSwiss
Posts: 3220
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Bit strings ? ( "Bit string datatype" )

Postby MrSwiss » Jun 19, 2019 21:40

Seems to me, that what you are looking for, is probably best described as "BitField".

The manipulators (in code) whould therefore be: BitSet()/BitReSet() ...

E.g. in a UShort you can store 16 different (Boolean) instances. ULong = 32 e.t.c..
(remark: a 1 means TRUE, a 0 means FALSE)
badidea
Posts: 1461
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Bit strings ? ( "Bit string datatype" )

Postby badidea » Jun 19, 2019 22:15

Not sure if this is what you are looking for, but I post my today's bit exercise anyway:

Code: Select all

type bit_array
   redim as ulong buffer(any) '32 bit buffer
   dim as integer numBits
   declare sub addBit(bitValue as integer)
   declare sub setBit(bitIndex as integer, bitValue as integer)
   declare function getBit(bitIndex as integer) as integer
end type

sub bit_array.addBit(bitValue as integer)
   dim as integer bufferIndex, bitRelIndex, ub
   if (numBits and &b11111) = 0 then
      ub = ubound(buffer) + 1
      redim preserve buffer(ub)
   end if
   bufferIndex = numBits shr 5
   bitRelIndex = numBits and &b11111
   buffer(bufferIndex) or= (bitValue shl bitRelIndex)
   numBits += 1
end sub

sub bit_array.setBit(bitIndex as integer, bitValue as integer)
   dim as integer bufferIndex, bitRelIndex, ub
   if (bitIndex >= numBits) or (bitIndex < 0) then
      print "error" : exit sub
   end if
   bufferIndex = bitIndex shr 5
   bitRelIndex = bitIndex and &b11111
   if bitValue = 1 then
      buffer(bufferIndex) or= (1 shl bitRelIndex)
   else
      buffer(bufferIndex) and= (not (1 shl bitRelIndex))
   end if
end sub

function bit_array.getBit(bitIndex as integer) as integer
   dim as integer bufferIndex, bitRelIndex, ub
   if (bitIndex >= numBits) or (bitIndex < 0) then
      print "error" : return -1
   end if
   bufferIndex = bitIndex shr 5
   bitRelIndex = bitIndex and &b11111
   return (buffer(bufferIndex) shr bitRelIndex) and 1
end function

'-------------------------------------------------------------------------------

dim as bit_array bits

for i as integer = 1 to 15
   bits.addBit(1)
   bits.addBit(0)
   bits.addBit(0)
   bits.addBit(1)
   bits.addBit(1)
next

bits.setBit(0, 0)
bits.setBit(1, 1)
bits.setBit(bits.numBits - 1, 0)

print "bits.numBits: " + str(bits.numBits)

for i as integer = bits.numBits - 1 to 0 step -1 'in reverse
   print str(bits.getBit(i));
next
print
while inkey = "" : wend

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests