|
2007-02-20
| ||
| 21:40 | • Ticket [1663473fff] binary scan seems to be losing bits status still Closed ... artifact: 17bb5ed186 user: dkf | |
| 20:28 | • Ticket [1663473fff]: 4 changes artifact: 925d31ac25 user: lvirden | |
| 20:09 | • Ticket [1663473fff]: 4 changes artifact: d588692212 user: msofer | |
| 18:30 | • Ticket [1663473fff]: 5 changes artifact: 69781b949e user: lvirden | |
| 06:59 | • Closed ticket [1663473fff]. artifact: 60a4d92863 user: hobbs | |
| 05:31 | • Ticket [1663473fff]: 2 changes artifact: f4502147eb user: dkf | |
|
2007-02-19
| ||
| 13:35 | • New ticket [1663473fff]. artifact: ba0ef10b1d user: lvirden | |
binary scan seems to be losing bits
Closed / Rejected1663473fffffffffffffffffffffffffffffffffCreated 20 years agoUpdated 20 years ago
Descriptionplain2007-02-19 13:35:59
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...
dkf Ticket [1663473fff]: 2 changes
[History diff]2007-02-20 05:31:04
hobbsplain2007-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.
lvirdenplain2007-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?
msoferplain2007-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?
lvirdenplain2007-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.
dkfplain2007-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.
Log in (anonymous is fine) to add a comment.