← Back to C Course | Chapter 12: Advanced Topics | Lesson 3 of 20

C के Bit Fields

Bit fields आपको हर एक को कुछ bits देकर कई छोटी values को एक number में squeeze करने देते हैं, एक page को कई tiny notes के बीच share करने जैसा। ये memory बचाते हैं।
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक bit field का address लेना, जो allowed नहीं है।
  2. bit field की क्षमता से बड़ी value store करना।
  3. यह मान लेना कि bit field layout हर compiler पर same है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.