Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Extracting bit-fields from binary

Posted by Alok (Alok), 10 February 2007

I need to extract bit fields from a 32-bit binary number, re-arrange them and form another 32-bit binary bumber. Could you please suggest how to do it ? I don't really know how to apply binary scan/format for this.


Posted by admin (Graham Ellis), 11 February 2007
Everything is held as a string in Tcl, so that includes yor incoming and outgoing number.   Here's a sample that extracts a 4 byte integer from a variable and returns its numebric value.

set sample abcdefgh
binary scan $sample aIa first second third
puts $first
puts $second
puts $third

Not your solution exactly, I know.   Can you give me an an exacmple of the incoming string and result you want and I'll see if I can come up with a more tuned answer.

Posted by Alok (Alok), 11 February 2007
Hi Graham,

Here is an example of what I need :
Incoming number : 1000_0010_0000_10101_1000_0010_0000_10101 (32 bits)

"_" is just placed to make it readable in this discussion.

I need to extract the following :

output (32 bits) =concat{bit_31 bit_23_downto_0 bit_28_downto_24 "00"}

I have been trying to understand usage of binary scan with ooption 'b'. However its usage, outcoming pattern of bits and the need of having a byte to scan .... difficult to understand and apply it.

Thanks & regards,

Posted by Alok (Alok), 11 February 2007

I could do it using string index and string-range operators. Was rather easy ...



Posted by admin (Graham Ellis), 12 February 2007
Ah - so it was a string of 0-s and 1-s rather than a packed binary  

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho