PB3.50 - GetBits of a val (byte)

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

PB3.50 - GetBits of a val (byte)

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by MrVainSCL.

Hi ppl,
if you need any bit of a val (byte), you can use my following procedure for this. I am sure there is a mutch better and faster way using inline asm i.e. - but i am not a x86 asm expert... So this works for the first... :wink:

Btw if you know any better and faster way, please let me know...
Thx... happy coding...

Code: Select all

; ------------------------------------------------------------
;
; PureBasic  -  MyGetBit(val,bitbum) of a byte - Example v1.0
;
; by MrVainSCL! aka Thorsten   21/Dec/2002    PB v3.40+
;
; ------------------------------------------------------------
;
; -----------------------------------
; PROC - MyGetBit()                             
; -----------------------------------
;
Procedure MyGetBit(val,bitnum)          ; val(Byte)  bitnum(bit 1-8)
  Dim bit.b(8)
  ;
  t1 = val / 2 : If t1*2 <> val : bit(1) = 1 : EndIf    ; 222 / 2 = 111    Rest = 0
  t2 =  t1 / 2 : If t2*2  <> t1 : bit(2) = 1 : EndIf    ; 111 / 2 =  55    Rest = 1
  t3 =  t2 / 2 : If t3*2  <>  t2 : bit(3) = 1 : EndIf    ;  55 / 2 =  27    Rest = 1
  t4 =  t3 / 2 : If t4*2  <> t3 : bit(4) = 1 : EndIf    ;  27 / 2 =  13    Rest = 1
  t5 =  t4 / 2 : If t5*2  <> t4 : bit(5) = 1 : EndIf    ;  13 / 2 =   6    Rest = 1
  t6 =  t5 / 2 : If t6*2  <> t5 : bit(6) = 1 : EndIf    ;   6 / 2 =   3    Rest = 1
  t7 =  t6 / 2 : If t7*2  <> t6 : bit(7) = 1 : EndIf    ;   3 / 2 =   1    Rest = 1
  t8 =  t7 / 2 : If t8*2  <> t7 : bit(8) = 1 : EndIf    ;   1 / 2 =   0    Rest = 0
  ;
  ; -------- If Val = 222, result should be => 11011110 --------
  ;
  result = bit(bitnum)
  ProcedureReturn result
EndProcedure
;
; -----------------------------------
;
OpenConsole()
PrintN(Str(MyGetBit(255,8)))            ; Get bit number 4 of Val 255 :wink:
Input()
End
;
; ------------------------------------------------------------


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

I made it a little shorter... (also works with long)

Code: Select all

Procedure GetBit(val, bitnum)
  If val & (1<<(bitnum-1))
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure


Debug GetBit(255,8)
Debug GetBit(-1,32)
Debug GetBit(1, 32)

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Asm version, accepts long values and bits 0-31:

Code: Select all

;
; -----------------------------------
; PROC - MyGetBitAsm() for PB 3.40 - enable inline asm
; -----------------------------------
;

Procedure MyGetBitAsm(val.l, bitnum.b) ; Value, bit number (bit 0-31)
    mov edx, val
    mov al, bitnum
    result.l
    push ecx
    mov cl, al
    mov eax, 1
    shl eax, cl
    pop ecx
    not eax
    Or eax, edx
    mov edx, 1
    not eax
    test eax, eax
    !jz .End
    XOr edx, edx
    !.End:
    mov result, edx
    ProcedureReturn result
EndProcedure

;
; -----------------------------------
;

    OpenConsole()
    PrintN(Str(MyGetBitAsm(255, 3)))            ; Get bit number 3 of Val 255 :wink:
    Input()
End
;
; ------------------------------------------------------------
Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Oh, what a shame! (hadn't seen freak's version before posting)

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Very nice job El_Choni and freak!
Thanks for your sharing your code snip :wink:


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
Post Reply