Find first and last set bit of a long integer: Difference between revisions

Content deleted Content added
Chemoelectric (talk | contribs)
Puppydrum64 (talk | contribs)
Line 2,062:
130691232 MSB: 26 LSB: 5
</pre>
 
 
=={{header|Z80 Assembly}}==
This version works with 8-bit values and is simple enough, given the CPU's obvious affinity for manipulating 8-bit data. First we display the "least" set bit, then the "most" set bit. These are in terms of LSB-0 ordering, e.g. <code>%76543210</code> (where those numbers are bit positions rather than literals)
 
<lang z80>;;;;;;;;;;;;;;;;;;; HEADER ;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_macros.asm"
read "\SrcCPC\MemoryMap.asm"
read "\SrcALL\winapeBuildCompat.asm"
;;;;;;;;;;;;;;;;;;; PROGRAM ;;;;;;;;;;;;;;;;;;;
 
org &1000
ld a,%00101111
push af
call lwb_8
call showhex
call newline
pop af
 
 
 
call upb_8
call showhex
ret
 
 
 
upb_8:
ld b,8
upb_8_again:
or a
rla
jr c,upb_8_done:
djnz upb_8_again
ld a,255 ;failure code
ret
upb_8_done:
dec b
ld a,b
ret
 
lwb_8:
ld bc,&0800 ;ld b,8 ld c,0
lwb_8_again:
or a
rra
jr c,lwb_8_done
inc c
djnz lwb_8_again
ld a,255 ;failure code
ret
lwb_8_done:
ld a,c
ret
 
 
read "\SrcCPC\winape_stringop.asm"
read "\SrcCPC\winape_showhex.asm"</lang>
{{out}}
<pre>00
05</pre>