bit-packing

Why does packing not work across sibling unions or structs

自闭症网瘾萝莉.ら 提交于 2019-12-23 03:03:44
问题 In the following example I expect the size of complex_t to be the same as uint16_t : 2 bytes, however it's 3 bytes. Removing the second union ("proximity_unsafe") reduces the size to 2 bytes, but I can't figure out the model of the packing rules. #include <stdint.h> #include <stdio.h> typedef union { uint16_t unsafe; struct { uint16_t backwardmotion_unsafe : 1; uint16_t batteryvoltage_unsafe : 1; union { uint16_t dropoff_unsafe : 4; struct { uint16_t dropofffrontleft_unsafe : 1; uint16_t

Redistribute least significant bits from a 4-byte array to a nibble

[亡魂溺海] 提交于 2019-12-23 01:44:30
问题 I wish to move bits 0,8,16,24 of a 32-bit value to bits 0,1,2,3 respectively. All other bits in the input and output will be zero. Obviously I can do that like this: c = c>>21 + c>>14 + c>>7 + c; c &= 0xF; But is there a faster (fewer instructions) way? 回答1: c = (((c&BITS_0_8_16_24) * BITS_0_7_14_21) >> 21) & 0xF; Or wait for Intel Haswell processor, doing all this in exactly one instruction (pext). Update Taking into account clarified constraints and assuming 32-bit unsigned values , the

What is VC++ doing when packing bitfields?

梦想与她 提交于 2019-12-17 14:03:26
问题 To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 6; unsigned int j : 6; unsigned int k : 4; unsigned int l : 15; }; #pragma pack(pop) struct cc c; int main(int argc, char **argv) { printf("%d\n",sizeof(c)); } The output is "8", meaning that the 56

What is VC++ doing when packing bitfields?

ε祈祈猫儿з 提交于 2019-12-17 14:03:15
问题 To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 6; unsigned int j : 6; unsigned int k : 4; unsigned int l : 15; }; #pragma pack(pop) struct cc c; int main(int argc, char **argv) { printf("%d\n",sizeof(c)); } The output is "8", meaning that the 56

Packing two shorts into one int, dealing with negative and positive

↘锁芯ラ 提交于 2019-12-12 07:59:27
问题 I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and unsigned and values that are larger or smaller that a short's range (they are passed in as two ints). Here's what I've got so far: public final class PackedUnsigned1616 { public final int field; private static final int RIGHT = (2 << 15) - 1; private

jquery SMS character calculator with 7-bit and 16bit

不打扰是莪最后的温柔 提交于 2019-12-11 06:19:39
问题 I have a text field to type sms message both in english and chinese language. As I have searched, 1 sms can only have 1120 bits. Each english character is 7 bit so it can be 1120/7 = 160 characters and for chinese each character is 16 bits so that is 1120/16 = 70 characters. I need to use jquery to show the words written and words remaining under the text field. how do i do this? 回答1: Characters can be single byte ,double byte, triple byte and so on. So single byte follows in a particular

Why does packing not work across sibling unions or structs

杀马特。学长 韩版系。学妹 提交于 2019-12-08 09:08:33
In the following example I expect the size of complex_t to be the same as uint16_t : 2 bytes, however it's 3 bytes. Removing the second union ("proximity_unsafe") reduces the size to 2 bytes, but I can't figure out the model of the packing rules. #include <stdint.h> #include <stdio.h> typedef union { uint16_t unsafe; struct { uint16_t backwardmotion_unsafe : 1; uint16_t batteryvoltage_unsafe : 1; union { uint16_t dropoff_unsafe : 4; struct { uint16_t dropofffrontleft_unsafe : 1; uint16_t dropofffrontright_unsafe : 1; uint16_t dropoffsideleft_unsafe : 1; uint16_t dropoffsideright_unsafe : 1; }_

Using reflection to determine how a .Net type is layed out in memory

白昼怎懂夜的黑 提交于 2019-12-05 17:31:15
问题 I'm experimenting with optimizing parser combinators in C#. One possible optimization, when the serialized format matches the in-memory format, is to just do an (unsafe) memcpy of the data to be parsed over an instance or even many instances of the type. I want to write code that determines if the in-memory format matches the serialized format, in order to dynamically determine if the optimization can be applied. (Obviously this is an unsafe optimization and might not work for a whole bunch

Packing two shorts into one int, dealing with negative and positive

心已入冬 提交于 2019-12-03 13:50:35
I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and unsigned and values that are larger or smaller that a short's range (they are passed in as two ints). Here's what I've got so far: public final class PackedUnsigned1616 { public final int field; private static final int RIGHT = (2 << 15) - 1; private static final int LEFT = ((2 << 31) - 1) ^ RIGHT; public PackedUnsigned1616(int left, int right) { field

use of the bitwise operators to pack multiple values in one int

与世无争的帅哥 提交于 2019-12-03 00:50:01
问题 Low level bit manipulation has never been my strong point. I will appreciate some help in understanding the following use case of bitwise operators.Consider... int age, gender, height, packed_info; . . . // Assign values // Pack as AAAAAAA G HHHHHHH using shifts and "or" packed_info = (age << 8) | (gender << 7) | height; // Unpack with shifts and masking using "and" height = packed_info & 0x7F; // This constant is binary ...01111111 gender = (packed_info >> 7) & 1; age = (packed_info >> 8); I