Questions tagged [bit-fields]

A bit field is used to compactly store multiple logical values as a short series of bits where each of the single bits can be addressed separately.

A bit field is used to represent and store a set of known width and logically grouped set of values. These fields can then be addressed individually in the code. A common use of such a construct is flags.

In language such as C and C++, bit fields can also be used to abstract and interop with specific hardware.

857 questions
-1
votes
3 answers

What does this bit wise operation doing?

I read about bitwise operations a lot, but still, I couldn't give a meaning to this line. ((text.flags & ~Text.BOLD) & ~Text.ITALIC) | Text.BOLD | Text.ITALIC It seems like the author is trying to be sure that this text doesn't have styles BOLD…
user2337191
-1
votes
1 answer

is it bad practice to have bit-fields and other data types as fields in a single struct in C90?

I have this struct in C: typedef struct Set { unsigned int state : 1; char array[LEN]; } Set; While this compiles and executes I noticed that the bit-field actually alters one bit in the char array itself! So it I'm wondering if this is…
Yos
  • 1,276
  • 1
  • 20
  • 40
-1
votes
1 answer

How to use bit field to get whole row bits?

#include #include struct __attribute__((__packed__)) sf_header { uint64_t lower: 32; uint64_t higher: 32; }; int main() { struct sf_header h; // part 1; h.lower = 15; h.higher = 1; printf("lower…
Jam
  • 113
  • 2
  • 7
-1
votes
1 answer

What does it means int i:3 in variable assignment?

I'm trying to understand the following code in C: struct values{ int i:3; int j:3; int k:2; }; int main(){ struct values v = {-6,2,5}; printf("%d %d %d", v.i,v.j,v.k); } This code produces the following output: 2 2 1 I'm trying to…
clasimoes
  • 37
  • 1
  • 4
-1
votes
2 answers

With out type casting how I can fill the bit fields

#include #include typedef struct { int i; char a[4]; uint8_t j:1; uint8_t k:1; } abctest; int main() { abctest tryabc; memset(&tryabc, 0x00, sizeof(tryabc)); std::bitset<1> b; b = false; …
Dilip Kumar
  • 1,736
  • 11
  • 22
-1
votes
2 answers

convert bitfield to string C++

I want to convert a bitfield to a string. Visual Studio 2008 gives an invalid null pointer exception. Maybe it has something to do with the size of the array. it must be 8 but the output says it is 4, but why? class Converter { public: string…
Ramses
  • 15
  • 3
-1
votes
2 answers

Please explain the output for bitfield 1

#include int main(){ struct value { int bit1:1; int bit2:4; int bit3:4; } bit ={1,2,2}; printf("%d %d %d \n",bit.bit1,bit.bit2,bit.bit3); return 0; } Output : -1 2 2 Hi ,I am not able to…
sv1
  • 57
  • 1
  • 2
  • 11
-2
votes
2 answers

ctypes.Structure with bit fields are not setting values correctly

I have the following in C #include struct Demo { char f1:8; int f2:21; char f3:2; char f4:1; }; int main() { struct Demo d = {1, 15, 3, 1}; printf("%ld\n", sizeof(d)); printf("f1=%d|f2=%d|f3=%d|f4=%d", d.f1,…
deltaWeeb
  • 1
  • 3
-2
votes
1 answer

How to get the bit size in struct?

I want to print a_size in header struct like "a_size = 3" but, it prints "a_size = 1". How to get the value a_size? #include struct header { unsigned int total_size : 6; unsigned int a_size : 3; unsigned int b_size : 2; …
taranndus
  • 29
  • 2
-2
votes
1 answer

How to create packed arrays of bit fields with SDCC?

I have a nested data structure containing arrays of bit fields which I need to compile with SDCC for the MCS-51 target. This is a simplified example: example.c struct data { unsigned char a : 1; unsigned char b : 2; }; struct data…
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
-2
votes
1 answer

Objective-C NS_OPTIONS in Swift

I'm given an NS_OPTIONS defined in Objective-C: typedef NS_OPTIONS(NSInteger, MyType) { MyTypeOption1 = 1 << 0, MyTypeOption2 = 1 << 1, MyTypeOption3 = 1 << 2, // etc } I'm importing this type into Swift, but I can't form bit…
QED
  • 9,803
  • 7
  • 50
  • 87
-2
votes
4 answers

How to assign a 32-bit unsigned integer to a bit field containing 32 bits

I am trying to create a bit-field struct which has 32 total bits, but when I try to assign a 32-bit number to it, I get this error: Implicit truncation from 'unsigned int' to bit-field changes value from 4278190080 to 0 Here is my struct and how…
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
-2
votes
1 answer

C/C++ Is bitfield endianess really a problem in actual practice?

So per the C compiler standard here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf We find a failure to pin down requirements for exactly how bit-fields get implemented inside a C compiler. Apparently, as long as the bit-fields behave…
RBornert
  • 21
  • 2
-2
votes
3 answers

how to crack this bit field ?

I have to write a function setbits(x,p,n,y) who returns x with the n bits that begin at position p set to the right most n bits of an unsigned char variable y (leaving other bits unchanged). E.g. if x = 10101010 (170 decimal) and y = 10100111 (167…
garima
  • 5,154
  • 11
  • 46
  • 77
-2
votes
2 answers

How to read a full UTF-8 hexadecimal values into an int

What I am trying to do is get an int to take in an UTF-8-16-32 character, in doing so it should be able to tell whether it is UTF-8, UTF-16, or UTF-32. I read binary values from a text file using fopen(fp, "rb"). I run into a problem where a single…
compolo
  • 9
  • 5