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

→‎Errors: Maybe the '''lwb''' and '''upb''' routines can be modelled (and renamed) to ␣␣␣/␣␣␣ to avoid confusion?
(→‎Errors: More thoughts on this topic)
(→‎Errors: Maybe the '''lwb''' and '''upb''' routines can be modelled (and renamed) to ␣␣␣/␣␣␣ to avoid confusion?)
Line 102:
 
: The problem comes when comparing, say, calculations of the bits for an 8-bit number with those for a 64-bit number. The only non-crazy interpretation of <code>upb</code> and <code>lwb</code> involves giving the same values for “65” however many bits are used to represent it. This in turn enables arbitrary-precision integer arithmetic (because you can ignore the actual representation width — a good thing because it may be variable in the implementation). OTOH, it also means that you probably lose the duality between bit arrays and numbers as with numbers you enumerate the bits in ''logical'' order, whereas with bit arrays you enumerate in physical order; there's no reason to assume the two are identical (nor is it possible to know the physical order of bits in a byte with modern hardware, though it probably follows the byte order in order to keep circuitry smaller in ALUs). The long and short of it is this: the task asked about integers so the results ''must'' be sane for them even if that leads to odd results for bit strings. (I prefer the convention of using <tt>-1</tt> for bit positions of <tt>0</tt>, as that at least is a position that is always not in the bit sequence at all; this might be in part implied by the fact that the Tcl solution is always using arbitrary-precision math; that's how Tcl's math works.) –[[User:Dkf|Donal Fellows]] 14:51, 8 December 2011 (UTC)
 
Fair enough, getting the answer for '''short int''', vs '''long int''' is useful, and OTOH it is also useful to know just the actual position of the bit without counting backwards from the last bit. Note also:
* RFC's counts from the left to the right [http://tools.ietf.org/html/rfc2360#section-3.1 rfc2360].
* There is a comparison in wikipedia ([[wp:Bit_numbering#LSB_0_bit_numbering|LSB_0_bit_numbering]] vs [[wp:Bit_numbering#MSB_0_bit_numbering|MSB_0_bit_numbering]]) and a description of [[wp:MSB_0_bit_numbering#Bit_numbering|Bit_numbering]].
* Intel asm uses the [http://pdos.csail.mit.edu/6.858/2011/readings/i386/BSF.htm BSF] & [http://pdos.csail.mit.edu/6.858/2011/readings/i386/BSR.htm BSR] op codes.
* Maybe the '''lwb''' and '''upb''' routines can be modelled (and renamed) to match intels BSF/BSR to avoid confusion?
* Or keep use '''lwb''' and '''upb''' for absolute position ([[wp:Bit_numbering#MSB_0_bit_numbering|MSB_0_bit_numbering]]), but use '''lwb0''' and '''upb0''' for LSB (bits width) reverse relative positions ([[wp:Bit_numbering#LSB_0_bit_numbering|LSB_0_bit_numbering]]).
* Maybe '''rlwb''' and '''rupb''' for the "right/reverse/relative" '''lwb''' and '''upb'''.
 
Certainly greater minds then ours have already pondered this. c.f. [[wp:Lilliput_and_Blefuscu#Satirical_interpretations|Lilliput and Blefuscu]]. :-)
 
BTW: What is the representation of '''not''' 0xf in "arbitrary-precision" TCL? I (tongue in cheek) suggest hex 0x[[wp:Aleph_number|&alefsym;0]] where &alefsym; is the HTML entity &amp;alefsym;... this can simply be pronounced "all F symbol", meaning an infinite number of Fs. ;-)
 
[[User:NevilleDNZ|NevilleDNZ]] 23:08, 8 December 2011 (UTC)