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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

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