| Ticket UUID: | 1663473 | ||
| Title: | binary scan seems to be losing bits | ||
| Type: | Bug | Created on: | 2007-02-19 13:35:59 |
| Submitter: | lvirden | Assigned to: | hobbs |
| Subsystem: | 12. ByteArray Object | Severity: | |
| Priority: | 5 Medium | Last modified: | 2007-02-20 21:40:27 |
| Status: | Closed | Closed by: | hobbs |
| Resolution: | Rejected | Closed on: | 2007-02-19 23:59:23 |
| Version: | obsolete: 8.5a6 | ||
| Description: | ||||
From the man page for binary scan:
b The data is turned into a string of count binary digits in
low-to-high order represented as a sequence of ``1'' and
``0'' characters. The data bytes are scanned in first to
last order with the bits being taken in low-to-high order
within each byte. Any extra bits in the last byte are
ignored. If count is *, then all of the remaining bits in
string will be scanned. If count is omitted, then one bit
will be scanned. For example,
binary scan \x07\x87\x05 b5b* var1 var2
will return 2 with 11100 stored in var1 and 1110000110100000
stored in var2.
Perhaps I don't understand the meaning of the phrase "remaining bits in string".
It seems, to me, that this is a 24 bit string:
% puts [string length \x07\x87\x05 ]
3
and as far as I know, on a sparc solaris machine, 3 bytes, times 8 bits per byte, should equal 24 bits.
% binary scan \x07\x87\x05 b5b* var1 var2
2
% puts $var1
11100
% puts $var2
1110000110100000
% puts [string length $var1$var2]
21
Notice that 3 bits are missing. They are 000 from the first byte of the 3 bytes...
| ||||
| User Comments: | ||||
hobbs added on 2007-02-20 06:59:23:
Logged In: YES user_id=72656 Originator: NO Using tkcon: (Tcl) 51 % binary scan \x07\x87\x05 b5b* var1 var2 2 (Tcl) 52 % binary scan \x07\x87\x05 b* var3 1 (Tcl) 53 % dump var var* set ::var1 11100 set ::var2 1110000110100000 set ::var3 111000001110000110100000 You see that the missing 000 would be the first part of var2 - IOW, extraneous bits. I don't see a bug here. The values are correct, it is just that Tcl does format with all the preceding 0s. lvirden added on 2007-02-20 18:30:58:
Logged In: YES user_id=15949 Originator: YES binary scan \xff\xff\xff var3 var4 shouldn't be any leading zeros here, right? however, puts $var3 11111 which is what I would expect puts $var4 1111111111111111 string length $var4 16 Still only seeing 21 of the 24 bits. Shouldn't there be 19 bits here? msofer added on 2007-02-20 20:09:20:
Logged In: YES
user_id=148712
Originator: NO
The way I understand the last example
binary scan \xff\xff\xff b5b* var3 var4
according to the docs is:
(1) first read 5 bits, ie, the first 5 bits of one byte. The 5 bits are put into var3, the remaining 3 bits are discarded ("Any extra bits in the last byte are ignored."). The first byte is now done with.
(2) now read all the rest.
A slightly different experiment seems to confirm this interpretation (a final answer requires source diving, which I may do later):
% binary scan \xff\xff\xff b8b* var3 var4
2
% string length $var3
8
% string length $var4
16
% binary scan \xff\xff\xff b9b* var3 var4
2
% string length $var3
9
% string length $var4
8
I do confess that this interpretation has been arrived at *after* seeing the example; the docs may need to be improved?
lvirden added on 2007-02-20 20:28:45:
Logged In: YES user_id=15949 Originator: YES updated docs would seem to be useful. This does mean that one cannot scan a byte into two or more variables. One has to scan the byte in, then split the string up via normal string functions. That's too bad - it would have been nice to have had a format that would allow the person to split the byte up into several pieces - getting at the various status bits of a control register or whatever. dkf added on 2007-02-20 21:40:27:
Logged In: YES user_id=79902 Originator: NO The problem is that reading at the bit level across multiple variables would make the code to do bulk uses of [binary scan] much harder since you'd have to allow for someone reading one bit and then 100kB of 32-bit words, all offset by one bit, even though this is a vanishingly rare case. | ||||
