Writing bits
Blitz3D Forums/Blitz3D Programming/Writing bits
| ||
Is there any way to write bits to a file in Blitz? So, if you were just storing a true or false value, for example, and you didn't want to take up a whole 8 bits would it be possible to just store it as one bit in a file? |
| ||
Storage works in bytes, so really if you want to handle at the bit level, you have to separate your writes into bunches of 8 bits. |
| ||
Why not dedicate a byte of storage to say 8 bits? I mean, if you have for instance other instances when a bit needs to be stored, group them all together, in groups of 8. |
| ||
Ok, so if I wanted to store bits, then I would have to combine them into bunches of 8 bits which would make 1 byte that could be stored. But then how would I read that byte and be able to take from it the individual value of each bit? Maybe this is a stupid question, but I only half understand how the whole bit-byte system works so please excuse my ignorance. |
| ||
Function GetBit(value,position) Return (value Shr (position - 1)) And 1 End Function value is the byte, position is 8 to 1, left to right |
| ||
and PutBit()Function PutBit(value, position, bit = True) Return value Or (bit Shl (position - 1)) End Function here's a test a = 128 Print Right$(Bin$(a),8) Print GetBit(a,7) a = PutBit(a,4) Print a Print Right$(Bin$(a),8) WaitKey End voila, you can now put up to 8 booleans into one byte. (there's actually another way to do this, like the way bitflags are done for file attributes, don't feel like getting into it.) |
| ||
While these GetBit and PutBIt Functions are well done, I have to say he probably will understand nothing of it, esp. cause of SHR. I suggest to read some quick tutorials about BOOLEAN algebra. When you know each bit has a binary value (1,2,4,8,16,32 etc.) you can test a bit this way (eg. for the bit number 4, that represents a value of 8): my_byte=rand(0,255) is_set=(my_byte AND 8) if is_set=8 then print "Bit 4 is set" Now to set a bit you will use the OR command my_byte=my_byte OR 8 and finally to clear a bit you'll use AND NOT() my_byte=my_byte AND ~(8) (BTW Snarkbait PutBit lacks of this...if bit=0 then unset etc) So these are the underlying basics of something rather sophisticated like "Return (value Shr (position - 1)) And 1" EDIT: the Boolen command NOT doesn't seem to work as desired, instead use the operator "~" that does the same job. |
| ||
Well, yeah I realized the put bit didn't work to clear a bit... How bout this: Function GetBit(value,position) Return (value Shr (position - 1)) And 1 End Function Function SetBit(value, position) Return value Or (1 Shl (position - 1)) End Function Function UnSetBit(value, position) Return value And ~(1 Shl (position - 1)) End Function Function ToggleBit(value,position) Return (1 Shl (position - 1)) Xor value End Function He doesn't have to understand the bitwise math to use them as black box functions ;) |
| ||
Ok, but you still can't clear it, can you? I'd suggest to Function SetBit(value, position, bit=1) If bit<>0 Return value Or (1 Shl (position - 1)) Endif Return value And ~(1 Shl (position - 1)) End Function Edit uh, you've edited yours :) so now it'S complete. BTW - even I don't know (or forgot) about the "~" operator o_O. Truely black box technology. |
| ||
Yep ;) and blitzplus at least won't let me use 'and not' you have to use the bitwise complement ~ |
| ||
Oops - right now it seems I can't get NOT to work at all!!! But I am absolutely sure it worked before! Probably somehthing new in version 1.99?? ~ works, NOT no longer! a=Not(8) print a will give zero! Can't believe this... |
| ||
Wow. I just tested it back to Blitz3D Version 1.91... No way. It never worked. I apologize for all my "supercompetent help". Well, simply replace the NOT by a ~ (So I'll edit my code) You know what is really strange: I used NOT in a program to control a CNC Mill, and the program worked...weird...spooky. |
| ||
Wow, thanks for all the replies everyone. I think I semi-understand what's going on but I'm going to check out some tutorials like jfk suggested. Those functions look really useful too, just hope I can figure out the mechanics behind them. |
| ||
Well, the simplest i think i can put it, is a byte is as follows: 00000000 8 bits of data stored together Now, simply speaking each bit has a value, and when turned to 1, starting from the right, their values are: 1,2,4,8,16,32,64,128 Using combinations of these bits, you can make any number between 0 and 255. The computer stores these as voltages. Above a certain voltage means that a 1 is stored. Below a certain voltage, a 0 is stored. When you use AND NOT OR... etc you are basically filtering the data against other data. If you read a byte of data and it contained the number: 168 That would break down to: 128+32+8 or 10101000 128 64 32 16 8 4 2 1 --------------------------------------- 1 0 1 0 1 0 0 0 (used a code box to keep the formatting) I hope this helps a little and adds to what the other guys have been saying. |
| ||
what about a=1 b=0 if a and (NOT b) ; this actually works end if |
| ||
Oh thanks Ross, that explanation really makes sense. I just had one of those 'aha' moments! |