Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ specifying data length with data type declaration

Tags:

c++

I came across this syntax which i have not seen before

struct A {
    int x:24;
};

What does x:24 mean ? In C++ can you specify compiler that a variable should occupy only 24 bits, instead of 32 for an int type? If yes, which 24 bits will be occupied? the left most or the right most?

like image 560
Jimm Avatar asked Jan 17 '12 18:01

Jimm


People also ask

Which data type is used for variable length of data?

There are two types of variable-length data types. These are real variable-length data types, like char and binary, and nullable data types, which have either a normal fixed length that corresponds to their type or to a special length if null.

What is the default length of the type C data type?

The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type) is 8 bytes.

What is the datatype of b =' 23?

In the above example, '@' and '23' are also of type str (str is for String). Look at another example. You can see that the data type of the value assigned to a is str because it is written within "" . Even a digit written within ' ' or " " is treated as a string and not as an integer or a float.


1 Answers

This is the bit fields feature. It has been available since the early C days. Microsoft has a nice write-up on this feature, complete with pretty pictures showing the layout which is compiler-specific (they say that the pic is specific to MS).

like image 192
Sergey Kalinichenko Avatar answered Nov 02 '22 22:11

Sergey Kalinichenko