+ 1
Hey, I got a doubt. đ„șđ What does colon (:) mean in structures? In below program what is colon (:) doing? (Check description)
//Language: C Programming #include <stdio.h> typedef struct { int bit1 : 1; int bit2 : 2; int bit30 : 30; } bits; int main() { printf("%d", sizeof(bits)); return 0; } Please explain me what is the role of colon (:) in structures? I will be really thankful! đđ„ș
3 Answers
+ 5
you can read more about bits here
https://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php
An integer usually has four bytes = 32 bits. You're using one bit of the first int for bit1 and two more bits for bit2. There's 29 bits remaining, but bit3 takes 30 bits. So you need two integers to store all (1+2+30=) 33 bits. Two integers = 2*4 bytes = 8 bytes
+ 3
Thank you so much get and âšïžâšïž ! đđ»âš
+ 2
typedef struct {
int bit1 : 1; // <int> with 1 bit (automatically converted into <signed char> because less than or equal to 8 bits)
int bit2 : 2; // <int> with 2 bits (automatically converted into <signed char> because less than or equal to 8 bits)
int bit30 : 30; // <int> with 30 bits (3 bytes and 6 bits)
} bits;