![]() ![]() The caveat here is this works if you always want the same set of bits over and over. So most of these could have been just unsigned int and some even could be unsigned short I made all of the variables type unsigned long but you can use any variable type as long as the number of bits doesn't exceed those that can be used in the type. You can set the struct bit-fields up for whatever bits you want to get. I gave two examples kind of randomly assigned different bits to show. If you want the second 4 bits: second4bits = 4 If you want the last 8 bits: last16bits = 8 Then you can access the bits you want from any number you assign to the member mynumber: myunion getmybits Now you can set up your union: typedef union myunion If you have multiple bit patterns, you can even use that in your union as well. You can set that struct up to whatever bit pattern you want. You can use a union of an integer type that has enough bits for your application and a bit field struct. I have another method for accomplishing this. There is a special cpu instruction BLSMSK that helps to compute the mask ((1 << n) - 1). If the offset is always zero as in all your examples in the question then you don't need the more general getbits(). Unsigned last16bitsvalue = value & ((1u << 16) - 1) // & 0xffff ![]() I presume I am supposed to use the modulus operator and I tried it and was able to get the last 8 bits and last 16 bits unsigned last8bitsvalue = value & ((1u << 8) - 1) // & 0xff Return 0 /* value is padded with infinite zeros on the left */Ĭonst unsigned mask = (1u << n) - 1 /* n '1's */įor example, to get 3 bits from 2273 ( 0b100011100001) starting at 5-th bit, call getbits(2273, 5, 3)-it extracts 7 ( 0b111).įor example, say I want the first 17 bits of the 32-bit value what is it that I should do? unsigned first_bits = value & ((1u << 17) - 1) // & 0x1ffffĪssuming CHAR_BIT * sizeof(unsigned) is 32 on your system. Unsigned getbits(unsigned value, unsigned offset, unsigned n)Ĭonst unsigned max_n = CHAR_BIT * sizeof(unsigned) There are intrinsic functions such as _bextr_u32() (link requires sign-in) that allow to invoke this instruction explicitly. The answer may be a bit longer, but it's better design than just hard coding with 0xff or whatever.There is a single BEXTR (Bit field extract (with register)) x86 instruction on Intel and AMD CPUs and UBFX on ARM. Later, if you want to go through each one, you'll need to shift your result by 10 to the right and process each bit at a time (at position 0). So for example if you want 16 bits of 0xD7448EAB, from 10 to 25, you'll need the above, for x = 16 and y = 10.Īnd now to get the bits you want, just AND your number 0xD7448EAB with the mask above and you'll get the masked 0xD7448EAB with only the bits you want. The above is your mask for the bits you need. How to get x bits set to 1, in order, starting from y to y + x: ((1 << x) -1) << y How to get x bits set to 1 in order: (1 << x) - 1 You know shifting: x << y moves bits from x *y positions to the left*. For what you need, you'll only need a couple. What you need to know are the AND, OR, XOR, NOT, and shifting operations. Masks are numbers that you can use to sift through bits in the manner you want (keep bits, delete/clear bits, modify numbers etc). This was an example but I will keep getting different hex numbers all the time and I need to extract the same bits as I explained.įor this example you would output 1101 0001 0010 0011, which is 0xD123, or 53,539 decimal. The remaining 16 bits of data needs to be the output which is 11 0100 0100 1000 11 (numbers in italics are needed as the output). i.e., 10 1010 1011 are ignored.Īnd the upper 6 bits (Overflow) are ignored. ![]() I need to extract the 16 bits, and output that value. I would be highly obliged if someone can help me. I did go through a few threads but I'm still not clear how to do so. I have a 32 bit number and i have to extract bits from it. I have do not have much knowledge of C and I'm stuck with a problem since one of my colleague is on leave.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |