Droopy's Lib

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Oh, very cool.
Nice work, as always :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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... :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

@DoubleDutch
Can you post an example / explanation for this procedures.

Thanks
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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" )
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
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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))
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
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
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks
KameHameHaaa
User
User
Posts: 19
Joined: Wed Oct 05, 2005 2:44 pm

Post 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 :)
techjunkie
Addict
Addict
Posts: 1126
Joined: Wed Oct 15, 2003 12:40 am
Location: Sweden
Contact:

Post 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... :?: :!: :?:
Image
(\__/)
(='.'=) This is Bunny. Copy and paste Bunny into your
(")_(") signature to help him gain world domination.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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.
Post Reply