Author Topic: Checking the amount of set bits.  (Read 3057 times)

0 Members and 1 Guest are viewing this topic.

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Checking the amount of set bits.
« on: July 07, 2011, 10:55:05 pm »
Hi, is there an efficient way to check if a byte has only one reset bit?

Example:
%11111011
True, because bit 2 is reset.

%01110111
False, because bit 3 and 7 are reset. 

Thanks for all of the suggestions.  :)
ld a, 0
ld a, a

Offline Iambian

  • Coder Of Tomorrow
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 739
  • Rating: +216/-3
  • Cherry Flavoured Nommer of Fishies
    • View Profile
Re: Checking the amount of set bits.
« Reply #1 on: July 07, 2011, 11:01:35 pm »
Code: [Select]
;Input: A=byteToCheck
;Destroys B.
CheckOneBitUnset:
 cpl
 ld b,8
 rlca
 jr c,$+6
 djnz $-3
 scf
 ret  ;returns with a carry if no bits are set
 and %11111110
 ret  ;zero if only one bit is unset, nonzero if others are set. No carry regardless.
Check to see if that actually works. Just made it up on the spot.
« Last Edit: July 07, 2011, 11:01:59 pm by Iambian »
A Cherry-Flavored Iambian draws near... what do you do? ...

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Checking the amount of set bits.
« Reply #2 on: July 07, 2011, 11:04:06 pm »
This method tests for zero or one reset bits (you'd have to check the $FF case manually)
Code: [Select]
;Outputs Z if 0-1 bits of A are reset
ld b,a
inc a
or b
inc a
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Checking the amount of set bits.
« Reply #3 on: July 07, 2011, 11:07:58 pm »
This method tests for zero or one reset bits (you'd have to check the $FF case manually)
Code: [Select]
;Outputs Z if 0-1 bits of A are reset
ld b,a
inc a
or b
inc a
Thanks!  That is awesomeness!  A winner is you!  :D
ld a, 0
ld a, a

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Checking the amount of set bits.
« Reply #4 on: July 07, 2011, 11:41:03 pm »
This is the smallest I could come up with:
Code: [Select]
;Input: a = byte to check
;Flags: z = Exactly 1 reset bit
cpl
ld b,a
dec a
xor b
add a,1
rra
xor b

Edit: Nevermind, calc wins!  Although you do have to special case with his, but even with that I think it would still be smaller by one byte.
« Last Edit: July 07, 2011, 11:42:52 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Checking the amount of set bits.
« Reply #5 on: July 07, 2011, 11:45:27 pm »
Hmm, I just realized a fairly simple way to handle the special case:
Code: [Select]
;Outputs Z if exactly 1 bit of A is reset
  ld b,a
  inc a
  jr z,special
  or b
special:
  inc a
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman