Page 5 of 5

Posted: Wed Oct 12, 2005 8:33 pm
by GeoTrail
Oh, very cool.
Nice work, as always :)

Posted: Thu Oct 13, 2005 4:31 pm
by DoubleDutch
Droopy: You may like these little routines for your next library version...

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 
The usage is kinda obvious... :)

Posted: Thu Oct 13, 2005 6:37 pm
by Droopy
@DoubleDutch
Can you post an example / explanation for this procedures.

Thanks

Posted: Thu Oct 13, 2005 7:26 pm
by Flype
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.

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" )

Posted: Thu Oct 13, 2005 8:46 pm
by Droopy
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))

Posted: Thu Oct 13, 2005 10:00 pm
by Flype
yes, you seems to have understood :wink:

another one :D ?

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) 
Next

Posted: Fri Oct 14, 2005 1:17 am
by DoubleDutch
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

Code: Select all

PokeBit(5000,9,1)
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

Code: Select all

PokeBits(5000,9,3,8)
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:

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-15
or

with 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-31
or you can write with

Code: Select all

PokeBits(memory,11,5,r)
PokeBits(memory,5,6,g)
Pokebits(memory,0,5,b)
-Anthony

Posted: Fri Oct 14, 2005 1:44 pm
by Droopy
Thanks

Posted: Fri Oct 14, 2005 1:54 pm
by KameHameHaaa
DoubleDutch 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.
Gif and Swf (flash) binary files headers are stored in this way, so I'll upgrade to those nice routines when needed :)

Posted: Sat Oct 15, 2005 11:18 am
by techjunkie
Hmmmm...
Library Name: DROOPY LIB
Library Version: 1.27

Library Author: DROOPY
PureBasic Version: 3.93
OS Version: WINDOWS
Got an error at installation,

"Unable to locate the C:\Program Files\PureBasic\Help\ directory!"

The directory is there... :?: :!: :?:

Posted: Sat Oct 15, 2005 11:51 am
by Droopy
The installer "Library Installer 2" from Num3

When you pressed Next, is the Purebasic folder location correct ?
Is purebasic closed ?
Try install from local drive.