C के Bit Fields
In this page:
struct StructName {
unsigned int member_name : number_of_bits;
};
Bit Fields क्या हैं?
एक bit field एक fixed built-in size की बजाय bits में explicit width के साथ declared struct member है, कई छोटी values को memory के एक single word share करने देते हुए इसकी बजाय कि हर एक एक पूरा int ले।
यह flag registers या protocol headers जैसी चीज़ों के लिए सबसे useful है जहाँ memory tight है और हर bit account किया जाता है।
उदाहरण: What are Bit Fields?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Flags
struct Flags {
unsigned int status : 3;
};
// Program execution starts in main()
int main() {
struct Flags f;
f.status = 5;
// Print formatted text to the screen
printf("%d", f.status);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bit Fields Define करना
आप एक struct के अंदर member नाम के ठीक बाद एक colon के बाद bits की संख्या लिखकर एक bit field declare करते हैं, उदाहरण के लिए status के लिए exactly 3 bits reserve करने के लिए unsigned status : 3;।
Compiler फिर उस member को इसके neighbors के साथ pack करता है इसकी बजाय कि इसे एक पूरा byte या word दे।
उदाहरण: Defining Bit Fields
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Flags
struct Flags {
unsigned status : 3;
};
// Program execution starts in main()
int main() {
struct Flags f;
f.status = 6;
// Print formatted text to the screen
printf("%d", f.status);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Packing Efficiency
एक plain int member को हमेशा 4 bytes लगती हैं चाहे इसे कोई भी values रखनी हों, लेकिन कई छोटे bit fields से बना एक struct उन values में से कई को same 4 bytes में pack कर सकता है।
यह तब मायने रखता है जब memory में आपके हज़ारों records हों, क्योंकि packing meaningfully total memory usage कम कर सकती है।
उदाहरण: Memory Packing Efficiency
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Packed
struct Packed {
unsigned a : 1;
unsigned b : 1;
unsigned c : 1;
};
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu", sizeof(struct Packed));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Bit Field Value Limits
एक bit field सिर्फ उतनी values represent कर सकता है जितनी आपने दी गई bits की संख्या में fit हों, इसलिए एक 3-bit field सिर्फ 0 से 7 तक रख सकता है — इससे बड़ा कुछ भी assign करना error उठाने की बजाय चुपचाप wrap around हो जाता है।
अपने bit fields को हमेशा actual range of values के हिसाब से size करें जिसकी आपको उम्मीद है।
उदाहरण: Bit Field Value Limits
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Flags
struct Flags {
unsigned value : 3;
};
// Program execution starts in main()
int main() {
struct Flags f;
f.value = 9;
// Print formatted text to the screen
printf("%d", f.value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unsigned Bit Fields
Signed bit fields risky हैं क्योंकि एक bit sign के लिए reserved है, और value वापस पढ़ने पर वह sign bit कैसे extend होता है यह compilers में implementation-defined है।
Bit fields को unsigned int declare करना इस ambiguity को पूरी तरह avoid करता है और stored values को predictably behave कराता रहता है।
उदाहरण: Unsigned Bit Fields
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Flags
struct Flags {
unsigned int status : 3;
};
// Program execution starts in main()
int main() {
struct Flags f;
f.status = 5;
// Print formatted text to the screen
printf("%u", f.status);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक bit field का address लेना, जो allowed नहीं है।
- bit field की क्षमता से बड़ी value store करना।
- यह मान लेना कि bit field layout हर compiler पर same है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions