Nice work, as always
Droopy's Lib
- DoubleDutch
- Addict

- Posts: 3220
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
Droopy: You may like these little routines for your next library version...
The usage is kinda obvious... 
Code: Select all
Procedure PokeBit(base,bitno,bit)
modulo=bitno%8
base+(bitno-modulo)>>3
If bit
PokeB(base,PeekB(base)|(1<<modulo))
Else
PokeB(base,PeekB(base)&(~(1<<modulo)))
EndIf
EndProcedure
Procedure PeekBit(base,bitno)
modulo=bitno%8
ProcedureReturn (PeekB(base+(bitno-modulo)>>3)>>modulo)&1
EndProcedure
Procedure PokeBits(base,bitno,width,value)
For loop=0 To width-1
PokeBit(base,bitno,value&1)
bitno+1
value>>1
Next
EndProcedure
Procedure PeekBits(base,bitno,width)
result=0
For loop=0 To width-1
result|(PeekBit(base,bitno)<<loop)
bitno+1
Next
ProcedureReturn result
EndProcedure https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
https://reportcomplete.com <- School end of term reports system
droopy:
for example the integer 1240 is %10011011000 in binary.
the goal is to manipulate each bit easily (read/write)
important: you have to pass the memory pointer (@variable) to the functions.
for example the integer 1240 is %10011011000 in binary.
the goal is to manipulate each bit easily (read/write)
important: you have to pass the memory pointer (@variable) to the functions.
Code: Select all
Procedure PeekBit(base,bitno)
modulo=bitno%8
ProcedureReturn (PeekB(base+(bitno-modulo)>>3)>>modulo)&1
EndProcedure
variable = 1240 ; which is %11011000 in binary
result$ = ""
For i = 0 To 31
result$ = Str( PeekBit( @variable, i ) ) + result$
Next
Debug result$
Debug RSet( Bin( variable ), 32, "0" )No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
is this corect ?
Code: Select all
;/ Author : DoubleDutch
;/ Set bit n° 'bitno' with 'bit'
Procedure PokeBit(base,bitno,bit)
modulo=bitno%8
base+(bitno-modulo)>>3
If bit
PokeB(base,PeekB(base)|(1<<modulo))
Else
PokeB(base,PeekB(base)&(~(1<<modulo)))
EndIf
EndProcedure
;/ Return bit n° 'bit' of the value stored @base
Procedure PeekBit(base,bitno)
modulo=bitno%8
ProcedureReturn (PeekB(base+(bitno-modulo)>>3)>>modulo)&1
EndProcedure
;/ Fill bits @base, starting @bitno, width specify the nb of byte, value specify the value ( ex : %1111)
Procedure PokeBits(base,bitno,width,value)
For loop=0 To width-1
PokeBit(base,bitno,value&1)
bitno+1
value>>1
Next
EndProcedure
;/ Return bits, @base, width define the number of bits to return, bitno specify which bit is the first
Procedure PeekBits(base,bitno,width)
result=0
For loop=0 To width-1
result|(PeekBit(base,bitno)<<loop)
bitno+1
Next
ProcedureReturn result
EndProcedure
;/ Test
value=%00000000000000000000000000000000
PokeBit(@value,31,1)
Debug Bin(value)
Debug PeekBit(@value,31)
PokeBits(@value,0,5,%11111)
Debug Bin(value)
Debug Bin(PeekBits(@value,0,3))yes, you seems to have understood 
another one
?
another one
Code: Select all
Procedure PokeBit(base,bitno,bit)
modulo=bitno%8
base+(bitno-modulo)>>3
If bit
PokeB(base,PeekB(base)|(1<<modulo))
Else
PokeB(base,PeekB(base)&(~(1<<modulo)))
EndIf
EndProcedure
value=0
For i=0 To 31
PokeBit(@value,i,1)
Debug RSet(Hex(value),8,"0") + ", " + Str(value)
PokeBit(@value,i,0)
NextNo programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
- DoubleDutch
- Addict

- Posts: 3220
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
They are routines to help change/read memory bits easily. If you have some packed data that isn't a byte, word or londword its usually quite a messy thing to do easily.
PokeBit(base,bitno,bit)
base is the base memory address, bitno is the offset to the bit you want to change, bit is the new value (0 or 1)
eg would set bit 1 in location 5001
bit=PeekBit(base,bitno)
nearly the same as poke, but returns 0 or 1
PokeBits(base,bitno,width,value)
width is the size in bits of the data you are writing, value is the actual data
eg would set bit 3, clear bits 2 and 1 of location 5001
value=PeekBits(base,bitno,width)
really obvious now
(please remember that this is just an example, don't flame me about location 5001!!!)
I use the routines to pack information info an encoded keyfile and to read it back out again.
Other examples of use are in altering bits of magnetic creditcard information or IMEA info from your phone
another example:
or
with 16bit colour (5-6-5 rgb)
or you can write with
-Anthony
PokeBit(base,bitno,bit)
base is the base memory address, bitno is the offset to the bit you want to change, bit is the new value (0 or 1)
eg
Code: Select all
PokeBit(5000,9,1)bit=PeekBit(base,bitno)
nearly the same as poke, but returns 0 or 1
PokeBits(base,bitno,width,value)
width is the size in bits of the data you are writing, value is the actual data
eg
Code: Select all
PokeBits(5000,9,3,8)value=PeekBits(base,bitno,width)
really obvious now
(please remember that this is just an example, don't flame me about location 5001!!!)
I use the routines to pack information info an encoded keyfile and to read it back out again.
Other examples of use are in altering bits of magnetic creditcard information or IMEA info from your phone
another example:
Code: Select all
hi_nibble=PeekBits(memory,4,4) ; read bits 4,5,6,7 as a number 0-15
lo_nibble=PeekBits(memory,0,4) ; read bits 0,1,2,3 as a number 0-15with 16bit colour (5-6-5 rgb)
Code: Select all
r=PeekBits(memory,11,5) ; read red as a number 0-31
g=Peekbits(memory,5,6) ; read green as a number 0-63
b=PeekBits(memory,0,5) ; read blue as a number 0-31Code: Select all
PokeBits(memory,11,5,r)
PokeBits(memory,5,6,g)
Pokebits(memory,0,5,b)https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
https://reportcomplete.com <- School end of term reports system
-
KameHameHaaa
- User

- Posts: 19
- Joined: Wed Oct 05, 2005 2:44 pm
Gif and Swf (flash) binary files headers are stored in this way, so I'll upgrade to those nice routines when neededDoubleDutch wrote:They are routines to help change/read memory bits easily. If you have some packed data that isn't a byte, word or londword its usually quite a messy thing to do easily.
-
techjunkie
- Addict

- Posts: 1126
- Joined: Wed Oct 15, 2003 12:40 am
- Location: Sweden
- Contact:
Hmmmm...
"Unable to locate the C:\Program Files\PureBasic\Help\ directory!"
The directory is there...

Got an error at installation,Library Name: DROOPY LIB
Library Version: 1.27
Library Author: DROOPY
PureBasic Version: 3.93
OS Version: WINDOWS
"Unable to locate the C:\Program Files\PureBasic\Help\ directory!"
The directory is there...

(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
