TGC Codebase Backup



Bit Array Functions by Benjamin

2nd Nov 2005 10:35
Summary

A set of functions for creating and editing bit arrays.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `MakeBitArray(Amount of bits) - Makes bit array, returns variable(pointer) for it
`FreeBitArray(Array variable) - Deletes bit array
`SetBit(Array variable, bit number, state) - Sets state of a bit
`BitIsSet(Array variable, bit number) - Returns bit state

function MakeBitArray(sizeInBits as integer)
	local byteSize as integer
	byteSize = int(sizeInBits/8)
	if not (sizeInBits mod 8) then inc byteSize
	ptr = make memory(byteSize)
	fill memory ptr, 0, byteSize
endfunction ptr

function FreeBitArray(arrayPtr as dword)
	delete memory arrayPtr
endfunction

function SetBit(arrayPtr as dword, bitNumber as integer, state as boolean)
	local addr as dword
	
	addr = arrayPtr+(bitNumber/8)

	`If bit is not opposite of state, switch it
	if state <> BitIsSet(arrayPtr, bitNumber)
		*addr = (*addr) ~~ (1 << (bitNumber mod 8))
	endif
endfunction

function BitIsSet(arrayPtr as dword, bitNumber as integer)
	local addr as dword
	local state as boolean
	addr = arrayPtr+(bitNumber/8)
	state = (*addr) && (1 << (bitNumber mod 8))
endfunction state
`-----------------------------------------------