Code archives/Graphics/Writebits/Readbits

This code has been declared by its author to be Public Domain code.

Download source code

Writebits/Readbits by aab2004
-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:
Writebits8(writingfile,var1,var2=0,var3=0,var4=0,var5=0,var6=0,var7=0,var8=0)
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)
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
	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)
	WriteByte(wr8_readingfile, wr8_finalwrite)
	Return wr8_finalwrite
End Function

Dim ReturnReadbits(7)
Function sReadbits8(srb8_readingfile);Use this to set up the eight variables form the file, then use ReturnReadbits to return each read bit
	srb8_conceptread=ReadByte(srb8_readingfile)
	For srb8_t=0 To 7
		ReturnReadbits(t)=(srb8_conceptread Shr srb8_t) And $1
	Next
	If(Eof(srb8_readingfile))Return(Eof);
	Return srb8_conceptread
End Function


;EG:
fileW=writefile("file.file")
b1=1
b2=0
b3=1
Writebits8(fileW,1,1,1,0,1,b3,b2,b1)
closefile(fileW)

filer=readfile("file.file")
sReadbits8(filer)
b3=ReturnReadbits(2)
b2=ReturnReadbits(1)
b1=ReturnReadbits(0)
closefile(filer)

Comments

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


aab2004
...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.


Code Archives Forum