Code: Select all

```
q:: ;test bit put and bit get
vSizeBits := 3
oArray := [0, 2**vSizeBits-1]
VarSetCapacity(vData, 100000, 0)
vOffset := 0, vNext := 0
Loop, % 10*8
{
vNext++
if (vNext > oArray.Length())
vNext := 1
JEE_BitPut(oArray[vNext], &vData, vOffset, vSizeBits)
vOffset += vSizeBits
}
vOutput := ""
Loop, % 10*8
vOutput .= JEE_BitGet(&vData, A_Index-1, 1)
vOutput := RegExReplace(vOutput, ".{8}", "$0 ")
MsgBox, % vOutput
vOutput .= "`r`n`r`n"
vOffset := 0
Loop, % (10*8) / vSizeBits
vOutput .= JEE_BitGet(&vData, vOffset, vSizeBits) " "
, vOffset += vSizeBits
MsgBox, % vOutput
Clipboard := vOutput
return
;==================================================
;where vBit is zero/a positive integer and is from left-to-right
JEE_BitGetSimple(vAddr, vBit)
{
local
if (vBit > 7)
vAddr += vBit // 8, vBit := Mod(vBit, 8)
vNum := NumGet(vAddr+0, 0, "UChar")
return !!(vNum & (1 << 7-vBit))
}
;==================================================
;where vNum is 0 or 1
;where vBit is zero/a positive integer and is from left-to-right
JEE_BitPutSimple(vNum, vAddr, vBit)
{
local
if (vBit > 7)
vAddr += vBit // 8, vBit := Mod(vBit, 8)
vNum2 := NumGet(vAddr+0, 0, "UChar")
if vNum
NumPut(vNum2 | (1 << 7-vBit), vAddr+0, 0, "UChar")
else
NumPut(vNum2 & ~(1 << 7-vBit), vAddr+0, 0, "UChar")
}
;==================================================
;where vBit is zero/a positive integer and is from left-to-right
;where vSizeBits is an integer between 0 and 8
;note: cannot handle overlaps into adjacent bytes
JEE_BitGet(vAddr, vBit, vSizeBits:=1)
{
local
if (vBit > 7)
vAddr += vBit // 8, vBit := Mod(vBit, 8)
if (vSizeBits < 0) || (vSizeBits > 8)
return
else if !vSizeBits
return 0
else if (vBit = 0) && (vSizeBits = 8)
return NumGet(vAddr+0, 0, "UChar")
;handle if bits split across 2 bytes
vDiff := vSizeBits - (8-vBit) ;vDiff if positive is the number of bits after the byte divide
if (vDiff > 0)
{
vExtra := JEE_BitGet(vAddr, vBit, vSizeBits-vDiff) << vDiff
vAddr += 1, vBit := 0, vSizeBits := vDiff
}
else
vExtra := 0
vNum := NumGet(vAddr+0, 0, "UChar")
vMask := (1 << vSizeBits) - 1
return vExtra + ((vNum & (vMask << 8-vBit-vSizeBits)) >> 8-vBit-vSizeBits)
}
;==================================================
;where vBit is zero/a positive integer and is from left-to-right
;where vSizeBits is an integer between 0 and 8
JEE_BitPut(vNum, vAddr, vBit, vSizeBits:=1)
{
local
if (vBit > 7)
vAddr += vBit // 8, vBit := Mod(vBit, 8)
if (vSizeBits <= 0) || (vSizeBits > 8)
return
else if (vBit = 0) && (vSizeBits = 8)
return NumPut(vNum, vAddr+0, 0, "UChar")
;handle if bits split across 2 bytes
vDiff := vSizeBits - (8-vBit) ;vDiff if positive is the number of bits after the byte divide
if (vDiff > 0)
{
JEE_BitPut(vNum >> vDiff, vAddr, vBit, vSizeBits-vDiff)
vNum &= (1 << vDiff) - 1
, vAddr += 1, vBit := 0, vSizeBits := vDiff
}
vNum2 := NumGet(vAddr+0, 0, "UChar")
vMask := (1 << vSizeBits) - 1
vNum &= vMask
NumPut((vNum2 & ~(vMask << 8-vBit-vSizeBits)) | (vNum << 8-vBit-vSizeBits), vAddr+0, 0, "UChar")
}
;==================================================
```