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 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Extracting bit-fields from binary

Posted by Alok (Alok), 10 February 2007
Hi,

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.

Thanks,
Alok

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.

Code:
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 :
bit_31=1
bit_28_downto_24=00010
bit_23_downto_0=0000_10101_1000_0010_0000_10101

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,
Alok

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

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

Thanks.

Regards,
Alok

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 www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho