March 04, 2021, 12:46:12 AM

Author Topic: [bb] Writebits/Readbits by aab [ 1+ years ago ]  (Read 638 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Writebits/Readbits by aab [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Writebits/Readbits
Author : aab
Posted : 1+ years ago

Description : -This is for all those variables that need to be stored as either one or zero
eg: one if you've completed that level, 0 if you havent
1 if you have that weapon, 0 if you dont.

Because it uses readbyte/writebyte, then eight variables must be written at a time, although they default to 0, so you can write as little as you like, and your still optimizing, rather than writing a byte for each.

when writing use:
to write between 1 and eight bits to the file. any numbers you pass that are >1 or <0 will be adjusted to 1 and 0 respectively

when reading from a file use:
sReadbits8(readingfile) ;before reading the bits
-this reads the next 8 bits in the file, and allows you to use ReturnReadbits.-this also returns the byte used to store the data.

Use :
ReturnReadbits(bit to return)
-to return each of the bits that have been read, on the last call of sReadbits8(readingfile)

so ReturnReadbits(1) for the first, ReturnReadbits(2) for the second etc.

Just make this an file to include with "include".
enjoy (if u can)

Code :
Code: BlitzBasic
  1. Function Writebits8(wr8_readingfile,wr8_var1,wr8_var2=0,wr8_var3=0,wr8_var4=0,wr8_var5=0,wr8_var6=0,wr8_var7=0,wr8_var8=0);use this ONCE to write 8 numbers in the range 0 to 1
  2.         wr8_finalwrite=(wr8_var8 Shl 7)+(wr8_var7 Shl 6)+(wr8_var6 Shl 5)+(wr8_var5 Shl 4)+(wr8_var4 Shl 3)+(wr8_var3 Shl 2)+(wr8_var2 Shl 1)+(wr8_var1)
  3.         WriteByte(wr8_readingfile, wr8_finalwrite)
  4.         Return wr8_finalwrite
  5. End Function
  7. Dim ReturnReadbits(7)
  8. Function sReadbits8(srb8_readingfile);Use this to set up the eight variables form the file, then use ReturnReadbits to return each read bit
  9.         srb8_conceptread=ReadByte(srb8_readingfile)
  10.         For srb8_t=0 To 7
  11.                 ReturnReadbits(t)=(srb8_conceptread Shr srb8_t) And $1
  12.         Next
  13.         If(Eof(srb8_readingfile))Return(Eof);
  14.         Return srb8_conceptread
  15. End Function
  18. ;EG:
  19. fileW=writefile("file.file")
  20. b1=1
  21. b2=0
  22. b3=1
  23. Writebits8(fileW,1,1,1,0,1,b3,b2,b1)
  24. closefile(fileW)
  26. filer=readfile("file.file")
  27. sReadbits8(filer)
  28. b3=ReturnReadbits(2)
  29. b2=ReturnReadbits(1)
  30. b1=ReturnReadbits(0)
  31. closefile(filer)

Comments :

N(Posted 1+ years ago)

 You have no idea how useful this is to me.  Thank you very, very much, aab.

aab(Posted 1+ years ago)

 ...funnily i didnt know what Hex was when i first wrote this...memories......I removed the Confinement on the values so that it'll be alot faster.


SimplePortal 2.3.6 © 2008-2014, SimplePortal