bit-fields

c union and bitfields

Can bitfields be used in union? ...

Reading/Writing Nibbles (without bit fields) in C/C++

Hello all, Is there an easy way to read/write a nibble in a byte without using bit fields? I'll always need to read both nibbles, but will need to write each nibble individually. Thanks! ...

16bit bitfield leads to *read from uninitialised memory* warning

I have this typedef: typedef union { unsigned Value; unsigned Timestamp:16; } BITFIELD; and get this compiler warning: BITFIELD bitfield; // read from uninitialised memory - may result in unexpected behaviour bitfield.Timestamp = 12; Now, the warning disappears when I use a short instead of the bitfield: typedef union { ...

Need meta-programming magic to define a mother lode of bit fields in an error-free way.

The goal is to control which types of users are allowed to perform which operations at the UI level. This code has been in place for a while; I just want to improve it a bit. The file which I am trying to improve should probably be auto-generated, but that would be too big of a change, so I seek a simpler solution. A file which we shall...

In C, what does a colon mean inside a declaration?

Possible Duplicate: What does unsigned temp:3 means I'm learning some kernel code, and came along the following line (in linux 2.4, sched.h, struct mm_struct): unsigned dumpable:1; What does this mean? ...

Is there a bit-equivalent of sizeof() in C?

Sizeof() doesn't work when applied to bitfields: # cat p.c #include<stdio.h> int main( int argc, char **argv ) { struct { unsigned int bitfield : 3; } s; fprintf( stdout, "size=%d\n", sizeof(s.bitfield) ); } # gcc p.c -o p p.c: In function ‘main’: p.c:5: error: ‘sizeof’ applied to a bit-field ...obviously, since it...

Using integers as bitfields in JavaScript

As part of a project I have a string of numbers between 0 and 3, for example: 2030000000000000000030000000000000000003333212111221121301 I want to pass this string through an URL, so I figured I could try using a bitfield since each number only uses a maximum of 2 bits each. I wrote the following functions in my class to convert such a ...

Storing many bits -- Should I use multiple columns or a single bitfield column?

Hello, I am designing a User table in my database. I have about 30 or so options for each user that can be either "allow" or "disallow". My question is should I store these as 30 bit columns or should I use a single int column to store them and parse out each bit in my application? Also, our database is SQL Server 2008 and 2005 (depen...

MS CRM 4.0 Certain fields appear only if a certain answer is selected?

I would like to know if it was possible to make certain fields invisible until the question linked to them is selected to Yes. Then other fields pop up to go into more detail. I'm not sure if CRM can do this, but if so I would like to know how. ...

Bitfields in C++

I have the following code for self learning: #include <iostream> using namespace std; struct bitfields{ unsigned field1: 3; unsigned field2: 4; unsigned int k: 4; }; int main(){ bitfields field; field.field1=8; field.field2=1e7; field.k=18; cout<<field.k<<endl; cout<<field.field1<<endl; cout<<fi...

Why can't I declare bitfields as automatic variables?

I want to declare a bitfield with the size specified using the a colon (I can't remember what the syntax is called). I want to write this: void myFunction() { unsigned int thing : 12; ... } But GCC says it's a syntax error (it thinks I'm trying to write a nested function). I have no problem doing this though: struct thingStru...

In MySQL, is a BIT column a suitable way to store 5, 6, or 7-byte integers?

I have a table whose size I'd like to keep down, and one of the columns can be treated as a 5-byte unsigned integer. This is a column that I won't need to search by. MySQL offers integer datatypes TINYINT, for 1-byte integersSMALLINT, for 2-byte integersMEDIUMINT, for 3-byte integersINT, for 4-byte integersBIGINT, for 8-byte integers. ...

How to initialize bitfields with a C++ Constructor?

First off, I’m not concerned with portability, and can safely assume that the endianness will not change. Assuming I read a hardware register value, I would like to overlay that register value over bitfields so that I can refer to the individual fields in the register without using bit masks. EDIT: Fixed problems pointed out by GMan, an...

Redundant __packed__ attributes

This code is for Microchip's PIC32MX microprocessor. Their compiler is essentially GCC 3.4. I tend use GCC's __packed__ attribute to pack bitfields into a union, and later retrieve them as an unsigned char (ie. type-punning) for sending over SPI or I2C. This behaviour is all defined by my implementation, and works perfectly. I prefer th...

why my value are divided by 2 with bit fields

I got some trouble with bitfields and endian stuff... I confused. I need to parse some data got from the network, the sent are in lil endian (im using boost::asio) Can you explain me this struct TEST { unsigned short _last : 1; unsigned short _ID : 6; unsigned short _LENGH : 9; }; struct TEST2 { unsigned short _LENGH:9 ; unsigne...

Bitfields vs. Polymorphism for game map object attributes

This optimzation question has been bugging me for the last day. In my program (a simple roguelike game), I use bitwise flags to store the attributes of map objects, such as if they are solid, or if they are rendered. However, I could accomplish the thing using polymorphism to return the appropriate value. My question is, is either way...

question about struct and pointers

let us consider following code #include <stdio.h> #include <iostream> using namespace std; typedef struct bf_{ unsigned x:4; unsigned y:4; unsigned z:4; unsigned w:4; }bf; int main(){ unsigned short i=8; unsigned short j=9; bf* bitfields=(bf *)&i; bf*bit=(bf*)&j; bitfields->w=12; printf("%d\n...

What is VC++ doing when packing bitfields?

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; ...

Colons after variable name on C code.

Possible Duplicate: What does 'unsigned temp:3' means 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? ...

Bit field vs Bitset

I want to store bits in an array (like structure). So I can follow either of the following two approaches Approach number 1 (AN 1) struct BIT { int data : 1 }; int main() { BIT a[100]; return 0; } Approach number 2 (AN 2) int main() { std::bitset<100> BITS; return 0; } Why would someone prefer AN 2 over AN 1? ...