Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2001-04-05
07:21 • Ticket [218287ffff] binary scan problem with 16 bit integers > 0x8000 sta... artifact: 6d71d45195 user: dgp
07:21 • Ticket [218287ffff]: 1 change artifact: 9a23f4dc46 user: dgp
2000-10-26
08:18 • Closed ticket [218287ffff]. artifact: 42396808aa user: welch
00:55 • New ticket [218287ffff]. artifact: 62ba2b8f2e user: nobody

binary scan problem with 16 bit integers > 0x8000

Closed / Fixed218287ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:55:36
OriginalBugID: 3466 Bug
Version: 8.0.4
SubmitDate: '1999-11-05'
LastModified: '2000-04-04'
Severity: MED
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 3229
OS: Linux
FixedDate: '2000-10-25'
ClosedDate: '2000-04-04'


Name:
Bob Findlay

ReproducibleScript:
#!/usr/bin/tclsh
    set bob1 0x8000
    set bob2 [binary format "s1" $bob1]
    binary scan $bob2 s1 bob3
    puts stdout [format "bob1=%X bob3=%X" $bob1 $bob3]

ObservedBehavior:
It seems once the lead bit in a 16 bit integer is set the binary
    scan command seems to prepend the result with 0xffff
    
    This does not seem like the correct behavior.

DesiredBehavior:
I would have expected that binary scan would return with the 16 bit
    value 
    without the prepended 0xffff



The output does come out as (8.3.0):

    bob1=8000 bob3=FFFF8000

which doesn't seem correct.  A work-around (possibly only for
this known case) is:

    format "bob1=%X bob3=%X" $bob1 [expr {$bob3 & ~0xffff0000}]

needs further investigation. 
-- 04/03/2000 hobbs

This turns out to be the correct behavior, based on what the
user is requesting.  For shorts (s or S), numbers >= 0x8000 are
negative (we don't have representation for unsigned shorts -
maybe we should, the same is true for chars and also ints on
systems where ints are > 32 bits).  So, the data is stored into
the ByteArray taking up only 16 bits.  However, when it is
extracted, it is placed into a long value, and thus the sign
extension must be done.  So the short value 0x8000 is
equivalent to the long value 0xFFFF8000 (or 0xFFFFFFFFFFFF8000
on 64bit longs).  Thus, the correct way to handle this, if the
user wanted to work with the shorts being unsigned, is to mask
each return value with 0xFFFF ($bob3 & 0xFFFF).  The above
work-around also works, but only on 32bit systems, so use the
latter instead.

You can see the code and logic for this in tclBinary.c:ScanNumber.
We will put a note in the binary doc for 8.3.1 as this is a bit
tricky (but definitely correct). 
-- 04/04/2000 hobbs

The following has been added to the binary.n man page (scan section):

It is important to note that the c, s, and S (and i and I on
64bit systems) will be scanned into long data  size  values.
In doing this, values that have their high bit set (0x80 for
chars, 0x8000 for shorts, 0x80000000 for ints), will be sign
extended.  Thus the following will occur:
set signShort [binary format s1 0x8000]
binary scan $signShort s1 val;  # val == 0xFFFF8000
If  you  want  to  work-around the sign extension of values,
mask the return value to the correct size, for example  with
shorts:
set val [expr {$val & 0xFFFF}];  # val == 0x8000
 
-- 04/04/2000 hobbs
welch Closed ticket [218287ffff]. [History diff]2000-10-26 08:18:58
dgp Ticket [218287ffff]: 1 change [History diff]2001-04-05 07:21:26
dgp Ticket [218287ffff]: 2 changes [History diff]2001-04-05 07:21:27
Log in (anonymous is fine) to add a comment.