<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Packed structures in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855128#M721501</link>
    <description>Last words to make everything completely clear.&lt;BR /&gt;&lt;BR /&gt;-1 in four-bytes representation is actually 0xffffffff (as one charcter in this representation stands for four bits).&lt;BR /&gt;Consequently 0xffff stands for two bytes.&lt;BR /&gt;&lt;BR /&gt;Rest of your assumption is absolutelly correct.&lt;BR /&gt;&lt;BR /&gt;Results from the code I've sent you is bit-mask, that is all bits for the field are set to '1', other are '0'. The results are written as hexadecimal number. The most comfortable would be to write it binary, but printf does not have an option to write binary. Only signed decimal, unsigned decimal, hexadecimal and octal.&lt;BR /&gt;&lt;BR /&gt;So you'll recive something like (this can of course change depending on what you're about to check):&lt;BR /&gt;field_a bitmask: c0000000&lt;BR /&gt;field_a bitmask: 38000000&lt;BR /&gt;field_a bitmask: 7c00000&lt;BR /&gt;&lt;BR /&gt;which you'll have to investigate further.&lt;BR /&gt;&lt;BR /&gt;For your convenience:&lt;BR /&gt;h 1 = b 0001&lt;BR /&gt;h 2 = b 0010&lt;BR /&gt;h 4 = b 0100&lt;BR /&gt;h 8 = b 1000&lt;BR /&gt;&lt;BR /&gt;h 3 = b 0011&lt;BR /&gt;h 6 = b 0110&lt;BR /&gt;h c = b 1100&lt;BR /&gt;&lt;BR /&gt;h 7 = b 0111&lt;BR /&gt;h e = b 1110&lt;BR /&gt;h f = b 1111&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Adam</description>
    <pubDate>Wed, 04 Dec 2002 12:33:19 GMT</pubDate>
    <dc:creator>Adam J Markiewicz</dc:creator>
    <dc:date>2002-12-04T12:33:19Z</dc:date>
    <item>
      <title>Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855121#M721494</link>
      <description>Hi to all,&lt;BR /&gt;&lt;BR /&gt;Consider the following packed structure:&lt;BR /&gt;&lt;BR /&gt;struct bit_field&lt;BR /&gt;{&lt;BR /&gt;unsigned int field_a: 2;&lt;BR /&gt;unsigned int field_b: 3;&lt;BR /&gt;unsigned int field_c: 5;&lt;BR /&gt;} flags;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;According to K&amp;amp;R book  the language does not&lt;BR /&gt;define whether storage starts at the least significant or most&lt;BR /&gt;significant bit. It is strictly up to the individual compiler.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Then how do I know how the compiler I'm using behaves? Is there a programming trick to get this information out of my compiler?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks to all in advance...&lt;BR /&gt;&lt;BR /&gt;Manuel</description>
      <pubDate>Mon, 02 Dec 2002 05:08:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855121#M721494</guid>
      <dc:creator>Max_4</dc:creator>
      <dc:date>2002-12-02T05:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855122#M721495</link>
      <description>Generally speaking you shouldn't need to know this.&lt;BR /&gt;The problem could be bigger as the compiler is allowed even to insert padds between your fields (for example it is allowed to force all fields not to cross byte - boundary).&lt;BR /&gt;&lt;BR /&gt;In your case it could add something before field_c to make it start at the next byte. So your structure could be:&lt;BR /&gt;struct bit_field &lt;BR /&gt;{ &lt;BR /&gt;unsigned int field_a: 2; &lt;BR /&gt;unsigned int field_b: 3; &lt;BR /&gt;unsigned int compiler_added_invisible_pad: (8 - ( 3 + 2 ))&lt;BR /&gt;unsigned int field_c: 5; &lt;BR /&gt;} flags;&lt;BR /&gt;&lt;BR /&gt;But if you're just curios how it is organised wirte the programm to check it.&lt;BR /&gt;&lt;BR /&gt;int main( void ) {&lt;BR /&gt; union {&lt;BR /&gt;  long l;&lt;BR /&gt;  struct bit_field bf;&lt;BR /&gt; } u;&lt;BR /&gt;&lt;BR /&gt; u.l = 0;&lt;BR /&gt; u.bf.field_a = (unsigned)-1;&lt;BR /&gt; printf( "field_a bitmask: %lx\n", u.l );&lt;BR /&gt; u.l = 0;&lt;BR /&gt; u.bf.field_b = (unsigned)-1;&lt;BR /&gt; printf( "field_b bitmask: %lx\n", u.l );&lt;BR /&gt; u.l = 0;&lt;BR /&gt; u.bf.field_c = (unsigned)-1;&lt;BR /&gt; printf( "field_c bitmask: %lx\n", u.l );&lt;BR /&gt;&lt;BR /&gt; return 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;However BEWARE!&lt;BR /&gt;If you make yse of this knowledge inside your code and decide to make changes... (upgrade compiler, change compiling options) you're on your own and nobody will help you.&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Adam&lt;BR /&gt;</description>
      <pubDate>Mon, 02 Dec 2002 17:45:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855122#M721495</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-12-02T17:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855123#M721496</link>
      <description>Thanks Adam for your explanation... and suggestion..&lt;BR /&gt;&lt;BR /&gt;However I was wondering which the definition of struct bit_field would be?&lt;BR /&gt;&lt;BR /&gt;I was not able to find it in the code snippet you posted...&lt;BR /&gt;&lt;BR /&gt;Again thanks a lot for your help...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Manuel</description>
      <pubDate>Tue, 03 Dec 2002 16:05:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855123#M721496</guid>
      <dc:creator>Max_4</dc:creator>
      <dc:date>2002-12-03T16:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855124#M721497</link>
      <description>I'm sorry i got confused ....&lt;BR /&gt;&lt;BR /&gt;Of course I've already created that structure in my question...&lt;BR /&gt;Silly of me... ;-)&lt;BR /&gt;I apologize...&lt;BR /&gt;&lt;BR /&gt;I was wondering what those &lt;BR /&gt;casts of -1 to (unsigned) would do?&lt;BR /&gt;&lt;BR /&gt;Again thanks a lot to Adam and to anyone who takes the time to help me...</description>
      <pubDate>Tue, 03 Dec 2002 16:11:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855124#M721497</guid>
      <dc:creator>Max_4</dc:creator>
      <dc:date>2002-12-03T16:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855125#M721498</link>
      <description>that cast fills your bitfields with all binary "1"s.&lt;BR /&gt;</description>
      <pubDate>Tue, 03 Dec 2002 18:40:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855125#M721498</guid>
      <dc:creator>Wodisch</dc:creator>
      <dc:date>2002-12-03T18:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855126#M721499</link>
      <description>Hi again.&lt;BR /&gt;&lt;BR /&gt;Just, like Wodish said.&lt;BR /&gt;&lt;BR /&gt;'-1' is very comfortable method of forcing all bits being set to 1.&lt;BR /&gt;Why did I use signed version and then used a cast? Because I'm lazy and I don't like bugs. :)&lt;BR /&gt;&lt;BR /&gt;If you want char with all bits set to 1 you could count it carefully and write:&lt;BR /&gt;char all_1_c = 255;&lt;BR /&gt;However you could write also&lt;BR /&gt;char all_1_c = -1;&lt;BR /&gt;&lt;BR /&gt;If you wanted short, you could do:&lt;BR /&gt;short all_1_s = 65535;&lt;BR /&gt;and also&lt;BR /&gt;short all_1_s = -1;&lt;BR /&gt;&lt;BR /&gt;the same with long, and even more practical with non-common sizes as used in bitfields.&lt;BR /&gt;&lt;BR /&gt;With the first solution if you decide to change the size after some time, from char to short you'll have to remember to recalculate the initialization:&lt;BR /&gt;&lt;BR /&gt;char all_1 = 255;    // I want it longer!&lt;BR /&gt;&lt;BR /&gt;short all_1 = 255;  // Oh, no! I forgot the second byte!&lt;BR /&gt;&lt;BR /&gt;And belive me or not - if you have to remember plenty of such things you'll be lost at the second change of your code, so the best way is to make code when you have nothing to remember.&lt;BR /&gt;&lt;BR /&gt;Then I made a cast, because your compiler could write a warning that I initialise unigned with negative. And belive me again, if you have warnings you'll get used to them and stop reading them. And sometimes they can save you a lot of work if you check them...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;At the end - when I think about this I think that more readable form would be ~0 (with 'tylde' at the beginning), which means 'binary negated zero', which means 'all ones'. But '-1' is so commonly used that nobody cares... :)&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Adam&lt;BR /&gt;</description>
      <pubDate>Tue, 03 Dec 2002 19:08:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855126#M721499</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-12-03T19:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855127#M721500</link>
      <description>Thanks Wodisch for the input...&lt;BR /&gt;&lt;BR /&gt;I assume that this comes from the fact that we're using 2s complement and -1 in a system where `int' will have four bytes&lt;BR /&gt;-1 will be represented as&lt;BR /&gt;0xffff. Just let me know if I'm correct.&lt;BR /&gt;&lt;BR /&gt;Thanks again...</description>
      <pubDate>Tue, 03 Dec 2002 19:15:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855127#M721500</guid>
      <dc:creator>Max_4</dc:creator>
      <dc:date>2002-12-03T19:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855128#M721501</link>
      <description>Last words to make everything completely clear.&lt;BR /&gt;&lt;BR /&gt;-1 in four-bytes representation is actually 0xffffffff (as one charcter in this representation stands for four bits).&lt;BR /&gt;Consequently 0xffff stands for two bytes.&lt;BR /&gt;&lt;BR /&gt;Rest of your assumption is absolutelly correct.&lt;BR /&gt;&lt;BR /&gt;Results from the code I've sent you is bit-mask, that is all bits for the field are set to '1', other are '0'. The results are written as hexadecimal number. The most comfortable would be to write it binary, but printf does not have an option to write binary. Only signed decimal, unsigned decimal, hexadecimal and octal.&lt;BR /&gt;&lt;BR /&gt;So you'll recive something like (this can of course change depending on what you're about to check):&lt;BR /&gt;field_a bitmask: c0000000&lt;BR /&gt;field_a bitmask: 38000000&lt;BR /&gt;field_a bitmask: 7c00000&lt;BR /&gt;&lt;BR /&gt;which you'll have to investigate further.&lt;BR /&gt;&lt;BR /&gt;For your convenience:&lt;BR /&gt;h 1 = b 0001&lt;BR /&gt;h 2 = b 0010&lt;BR /&gt;h 4 = b 0100&lt;BR /&gt;h 8 = b 1000&lt;BR /&gt;&lt;BR /&gt;h 3 = b 0011&lt;BR /&gt;h 6 = b 0110&lt;BR /&gt;h c = b 1100&lt;BR /&gt;&lt;BR /&gt;h 7 = b 0111&lt;BR /&gt;h e = b 1110&lt;BR /&gt;h f = b 1111&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Adam</description>
      <pubDate>Wed, 04 Dec 2002 12:33:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855128#M721501</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-12-04T12:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Packed structures</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855129#M721502</link>
      <description>Hello Adam,&lt;BR /&gt;&lt;BR /&gt;Thanks a lot again for your help...&lt;BR /&gt;Your good and detailed explanation has made things a whole lot easier to understand...&lt;BR /&gt;&lt;BR /&gt;I appreciate it...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Manuel</description>
      <pubDate>Wed, 04 Dec 2002 14:59:35 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/packed-structures/m-p/2855129#M721502</guid>
      <dc:creator>Max_4</dc:creator>
      <dc:date>2002-12-04T14:59:35Z</dc:date>
    </item>
  </channel>
</rss>

