Re: Help on bit operation

Tim Walberg <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
If you absolutely need to get at the 32-bit integer value, you can use

union mytype {
{ struct bits {
    unsigned int a:6;
    unsigned int b:4;
    unsigned int c:8;
    unsigned int d:5;
    unsigned int e:9;
  } b;
  unsigned int v;
} variable;

then access the bit-fields like

  unsigned int x = variable.b.e;
  variable.b.a = 3;
  x = variable.v;


  


On 08/28/2009 12:58 -0700, Ben Rosenberg wrote:
>>	On Fri, Aug 28, 2009 at 11:40 AM, Randi Botse<[email protected]> wrote:
>>	> Ben, after assign all bitfield struct member, how to pack them to be a
>>	> 32bit integer value?
>>	>
>>	> DEC      BIN
>>	> 43        101011        -> foo.a (6 bit)
>>	> 11        1011            -> foo.b (4 bit)
>>	> 120      01111000    -> foo.c (8 bit)
>>	> 30        11110          -> foo.d (5 bit)
>>	> 418      110100010  -> foo.e (9 bit)
>>	>
>>	> the bit pattern is : 10101110110111100011110110100010 or 2933800354 in
>>	> decimal, is this possible?
>>	>
>>	> while with masking and shift i can pack them with:
>>	> ((foo.a & 0x3f) << 26) | ((foo.b & 0xf) << 22) | ((foo,c & 0xff) <<
>>	> 14) | ((foo.d & 0x1f) << 9) | (foo.e & 0x1ff)
>>	>
>>	>
>>	
>>	The bitfield struct is already packed, you don't need to do anything
>>	else. Just assign values to the different parts.
>>	
>>	Ben
>>	--
>>	To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>	the body of a message to [email protected]
>>	More majordomo info at  http://vger.kernel.org/majordomo-info.html
End of included message



-- 
+----------------------+
| Tim Walberg          |
| 830 Carriage Dr.     |
| Algonquin, IL 60102  |
| [email protected] |
+----------------------+
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkqYOpwACgkQw+Wcj22rJWZ0DgCdGGcfR390BIBt6EgsGkhB9U6d
0SIAnAubDvWKGIPbUz6sRMUpSnxRe75Z
=elqn
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.