the following code compiles however the c version of it gives a compilation error
1 2 3
|
C:\Users\Mehak\Desktop\prac\Untitled2.c||In function 'main':|
C:\Users\Mehak\Desktop\prac\Untitled2.c|7|error: width of 'i' exceeds its type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
|
is thereany limit on the number of bits used in bit fields?
1 2 3 4 5 6 7 8 9 10 11 12
|
#include<iostream>
using namespace std;
int main()
{
struct bits
{
int i:40;
}bit;
cout<< sizeof(bit);
return 0;
}
|
Last edited on
the following code compiles however the c version of it gives a compilation error |
C and C++ are different languages, that should not be a surprise.
is there any limit on the number of bits used in bit fields? |
There is no limit on the number of bits in C++, but there is a limit on the maximum value that is stored in the bit field.
To quote
http://en.cppreference.com/w/cpp/language/bit_field
std::uint8_t b : 1000; would still hold values between 0 and 255. the extra bits become unused padding. |
(in C, as you've seen, the limit on the number of bits is the width of the underlying type)
And yes, use std::bitset
Last edited on