bit-fields

Colons after variable name in C [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-20 08:57:28
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What does a colon in a struct declaration mean, such as :1, :7, :16, or :32? This is C code sample of a reference page. signed int _exponent:8; What's the meaning of the colon before '8' and '8' itself? 回答1: It's a bitfield. It's only valid in a struct definition, and it means that the system will only use 8 bits for your integer. 回答2: It's a bitfield, an obscure and misguided feature of structures. That should

Size of a structure having unsigned short ints

寵の児 提交于 2019-12-20 08:09:40
问题 I was surfing in one of our organisational data documents and I came across the following piece of code. struct A { unsigned short int i:1; unsigned short int j:1; unsigned short int k:14; }; int main(){ A aa; int n = sizeof(aa); cout << n; } Initially I thought the size will be 6 bytes as the size of the unsigned short int is 2 bytes. but the output of the above code was 2 bytes(On visual studio 2008). Is there a slight possibility that the i:1 , j:1 and k:14 makes it a bit field or

How to use bit field with Swift to store values with more than 1 bit

泄露秘密 提交于 2019-12-19 22:02:06
问题 In C I can do something like this: struct byte_nibbles { unsigned char b1: 4; unsigned char b2: 4; unsigned char b3: 4; unsigned char b4: 4; unsigned char b5: 4; unsigned char b6: 4; unsigned char b7: 4; unsigned char b8: 4; }; union { unsigned long var; struct byte_nibbles b; } u; int main(void) { u.b.b1=0x01; u.b.b2=0x02; u.b.b3=0x03; u.b.b4=0x04; u.b.b5=0x05; u.b.b6=0x06; u.b.b7=0x07; u.b.b8=0x08; return 0; } So I can access specific parts of the byte_nibbles. Obviously this is just one

What's the purpose of unnamed bit field at the end of structure

六眼飞鱼酱① 提交于 2019-12-19 09:03:59
问题 I am learning C. In C Primer Plus , I saw an bit field example as follows: struct box_props { bool opaque : 1; unsigned int fill_color : 3; unsigned int : 4; bool show_border : 1; unsigned int border_color : 3; unsigned int border_style : 2; unsigned int : 2; }; I do understand the 4-bit unnamed bit field in the middle is used for letting the following bits start at a new byte. However, I don't understand why there is another unnamed bit field at the end of the structure. What's the purpose

Accessing bit-field in C by its address

倖福魔咒の 提交于 2019-12-19 04:20:11
问题 What is the reason behind not allowing to access a bit field in C using its address, is it cause it might not be an address that is not system word aligned ..? or as it doesn't make sense to get bit's address within a byte...?(cause this types pointer arithmetic will be awkward ?) 回答1: Bits do not have addresses. That's why you can't refer to them by address. The granularity of addressing is the char . I guess the reasoning is that the language was design to match the architecture it targeted

Does ANSI C support signed / unsigned bit fields?

纵然是瞬间 提交于 2019-12-18 19:01:08
问题 Does it make sense to qualify bit fields as signed / unsigned? 回答1: The relevant portion of the standard (ISO/IEC 9899:1999) is 6.7.2.1 #4: A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type. 回答2: Yes. An example from here: struct { /* field 4 bits wide */ unsigned field1 :4; /* * unnamed 3 bit field * unnamed fields allow for padding */ unsigned :3; /* * one-bit field * can only be 0 or -1 in

How to assign value to a struct with bit-fields?

我是研究僧i 提交于 2019-12-18 13:18:26
问题 I have a struct with bit-fields (totally 32 bit width) and I have a 32-bit variable. When I try to assign the variable value to my struct, I got an error: error: conversion from ‘uint32_t {aka unsigned int}’ to non-scalar type ‘main()::CPUID’ requested. struct CPUIDregs { uint32_t EAXBuf; }; CPUIDregs CPUIDregsoutput; int main () { struct CPUID { uint32_t Stepping : 4; uint32_t Model : 4; uint32_t FamilyID : 4; uint32_t Type : 2; uint32_t Reserved1 : 2; uint32_t ExtendedModel : 4; uint32_t

Can bit-fields only be fields of a structure/union, never “normal”, “stand-alone” variables?

你说的曾经没有我的故事 提交于 2019-12-18 09:06:09
问题 The field part of bit-fields seems to suggest that they can only be fields inside a structure or union. Can a bit-field be a typical "stand-alone" variable, outside any aggregate data-type like union or structure as follows: int sum:6; //Can this work as a declaration statement? sum=4; If not, why so? If bit-fields are basically intended to use less memory, why can't we declare any variable as a bit-field if we know it won't exceed that size? 回答1: Bit-fields are only part of structs or unions

Is it valid to use bit fields with union?

China☆狼群 提交于 2019-12-18 05:53:46
问题 I have used bit field with a structure like this, struct { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; Now i wondered to see if this can be done with a union so i modified the code like, union { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; I found the bit field with union works but all those fields in the union are given to a single bit as I understood from output. Now I am seeing it is not erroneous to use

What is the type of a bitfield?

蓝咒 提交于 2019-12-17 22:53:07
问题 I can't find anywhere in the C standard where this is specified. For example, in struct { signed int x:1; } foo; is foo.x an lvalue of type int , or something else? It seems unnatural for it to be an lvalue of type int since you cannot store any value of type int in it, only 0 or -1, but I can't find any language that would assign it a different type. Of course, used in most expressions, it would get promoted to int anyway, but the actual type makes a difference in C11 with _Generic , and I