• possible bug in [format #032b]

    From aotto1968@21:1/5 to All on Tue May 31 14:24:16 2022
    Hi,

    I want to print 32 bit binary numbers (0|1) of *fixed* length 32 for
    test but there is a problem, the length is *sometimes* not 32

    code:

    proc calc {msg str} {
    upvar $msg var
    set var [format %#x [uplevel expr $str]]
    puts [format {1> %-10s → %-15s → %#032b} $msg $str $var]
    }

    calc obj_only 0x59B3
    calc obj_sig (0x59B3<<16)
    calc test (0x1<<16)
    calc obj_mask (((1<<16)-1)<<16)

    calc buf_only "(1<<10)"
    calc buf_sig $obj_sig^$buf_only
    calc buf_sig ((0x59B3<<0)<<16)^(1<<10)
    calc buf_mask (((1<<22)-1)<<10)


    result:
    as you see the "0b…" fields have all a different length,
    the X_mask (all with "1") are *ok*
    the numbers (sometimes with binary "0" at the end) *not*

    obj_only → 0x59B3 → 0b000000000000000101100110110011
    obj_sig → (0x59B3<<16) →
    0b1011001101100110000000000000000
    test → (0x1<<16) → 0b000000000000010000000000000000
    obj_mask → (((1<<16)-1)<<16) →
    0b11111111111111110000000000000000
    buf_only → (1<<10) → 0b000000000000000000010000000000
    buf_sig → 0x59b30000^0x400 →
    0b1011001101100110000010000000000
    buf_sig → ((0x59B3<<0)<<16)^(1<<10) →
    0b1011001101100110000010000000000
    buf_mask → (((1<<22)-1)<<10) →
    0b11111111111111111111110000000000

    The problem seems to be a "0" at the end of a integer2binary representation

    mfg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ian Braithwaite@21:1/5 to All on Tue May 31 14:52:32 2022
    On 31/05/2022 14:24, aotto1968 wrote:
    Hi,

    I want to print 32 bit binary numbers (0|1) of *fixed* length 32 for
    test but there is a problem, the length is *sometimes* not 32

    code:

    proc calc {msg str} {
      upvar $msg var
      set var [format %#x [uplevel expr $str]]
      puts  [format {1> %-10s → %-15s → %#032b} $msg $str $var]
    }


    I think you want "%#034b" for the format - the field width includes the
    "0b".

    -Ian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Ian Braithwaite on Tue May 31 18:32:07 2022
    On 31.05.22 14:52, Ian Braithwaite wrote:
    On 31/05/2022 14:24, aotto1968 wrote:
    Hi,

    I want to print 32 bit binary numbers (0|1) of *fixed* length 32 for
    test but there is a problem, the length is *sometimes* not 32

    code:

    proc calc {msg str} {
       upvar $msg var
       set var [format %#x [uplevel expr $str]]
       puts  [format {1> %-10s → %-15s → %#032b} $msg $str $var]
    }


    I think you want "%#034b" for the format - the field width includes the
    "0b".

    -Ian

    ok, without "#" the field-width is OK but the error with "#" is still
    there. (or is this a feature?)

    obj_only → 0x59B3 →
    00000000000000000101100110110011
    obj_sig → (0x59B3<<16) →
    01011001101100110000000000000000
    test → (0x1<<16) →
    00000000000000010000000000000000
    obj_mask → (((1<<16)-1)<<16) →
    11111111111111110000000000000000
    buf_only → (1<<10) →
    00000000000000000000010000000000
    buf_sig → 0x59b30000^0x400 →
    01011001101100110000010000000000
    buf_sig → ((0x59B3<<0)<<16)^(1<<10) →
    01011001101100110000010000000000
    buf_mask → (((1<<22)-1)<<10) →
    11111111111111111111110000000000
    test1 → 1504933634 →
    01011001101100110111011100000010
    cfg_only → (((11<<16)<<10)) →
    00101100000000000000000000000000
    cfg_sig → (0x59b30000 ^ ((11<<16)<<10)) →
    01110101101100110000000000000000

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to aotto1968@t-online.de on Tue May 31 17:24:29 2022
    aotto1968 <aotto1968@t-online.de> wrote:
    On 31.05.22 14:52, Ian Braithwaite wrote:
    On 31/05/2022 14:24, aotto1968 wrote:
    Hi,

    I want to print 32 bit binary numbers (0|1) of *fixed* length 32 for
    test but there is a problem, the length is *sometimes* not 32

    code:

    proc calc {msg str} {
       upvar $msg var
       set var [format %#x [uplevel expr $str]]
       puts  [format {1> %-10s ? %-15s ? %#032b} $msg $str $var]
    }


    I think you want "%#034b" for the format - the field width includes the
    "0b".

    -Ian

    ok, without "#" the field-width is OK but the error with "#" is still
    there. (or is this a feature?)

    The critical portion of Ian's response is this:

    "the field width includes the '0b'"

    Ian is saying that format counts the 0b prefix as part of the width you
    specify for the format. So if you want format to add the 0b, you need
    to use field widths two characters larger than the value you are
    formatting (which is why Ian's example uses 034 above).

    Alternately, don't ask format to add the 0b, add it yourself:

    format 0b%032b $var

    And now the 0b is not counted as part of the "field width" when
    formatting.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Rich on Tue May 31 19:28:01 2022
    On 31.05.22 19:24, Rich wrote:
    aotto1968 <aotto1968@t-online.de> wrote:
    On 31.05.22 14:52, Ian Braithwaite wrote:
    On 31/05/2022 14:24, aotto1968 wrote:
    Hi,

    I want to print 32 bit binary numbers (0|1) of *fixed* length 32 for
    test but there is a problem, the length is *sometimes* not 32

    code:

    proc calc {msg str} {
       upvar $msg var
       set var [format %#x [uplevel expr $str]]
       puts  [format {1> %-10s ? %-15s ? %#032b} $msg $str $var]
    }


    I think you want "%#034b" for the format - the field width includes the
    "0b".

    -Ian

    ok, without "#" the field-width is OK but the error with "#" is still
    there. (or is this a feature?)

    The critical portion of Ian's response is this:

    "the field width includes the '0b'"

    Ian is saying that format counts the 0b prefix as part of the width you specify for the format. So if you want format to add the 0b, you need
    to use field widths two characters larger than the value you are
    formatting (which is why Ian's example uses 034 above).

    Alternately, don't ask format to add the 0b, add it yourself:

    format 0b%032b $var

    And now the 0b is not counted as part of the "field width" when
    formatting.


    → ok, now I understand.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)