{"id":81735,"date":"2021-07-20T09:00:09","date_gmt":"2021-07-20T03:30:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81735"},"modified":"2021-07-20T09:00:09","modified_gmt":"2021-07-20T03:30:09","slug":"bit-fields-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/","title":{"rendered":"Bit Fields in C"},"content":{"rendered":"<p>The C programming language has a lot of features available to the programmers. The C programming language helps programmers in creating their program code simple and easy to understand. It helps you in a better way to utilize the memory space in your program code. With the help of bit fields, you can achieve it. In coding, it is important to make use of the memory space in your program code.<\/p>\n<h3>What is a bit field in C?<\/h3>\n<p>In the C programming language, you can define the size(in bits) of the structure and union members. It is useful when you want to use the memory efficiently by knowing that the value of the field or the collection of the fields will never exceed a specific limit or it is in-between a small range.<\/p>\n<h3>How does bit field work in C?<\/h3>\n<p>Let\u2019s take an example to better understand the working of bit fields. Below is an example of declaring the date without the use of bit fields:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nstruct timing{\n    unsigned int date;\n    unsigned int month;\n    unsigned int year;\n};\nint main()\n{\n  printf(\"TechVidvan Tutorial: Bit Fields in C!\\n\\n\");\n    printf(\"Size of timing is %d bytes\\n\",sizeof(struct timing));\n    struct timing t1 = { 8, 10, 2000 };\n    printf(\"Some random date: %d\/%d\/%d\", t1.date, t1.month, t1.year);\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Bit Fields in C!<\/p>\n<p>Size of timing is 12 bytes<br \/>\nSome random date: 8\/10\/2000<\/p>\n<\/div>\n<p>In the above example, the total size of struct type timing is 12 bytes because the size of unsigned int is 4 bytes. On the other hand, we know that the value of date is always from 1 to 31 and also the value of month is always from 1 to 12. Using bit fields, we can utilize the space.<\/p>\n<p>But if we use the above program using signed int and the bit fields then you could see something interesting in your program.<\/p>\n<p><strong>Follow the below example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nstruct timing{\n  int date : 5; \/\/ date has value from 1 to 31, so 5 bits should be enough!\n  int month : 4; \/\/ month has value from 1 to 12, so 4 bits should be enough!\n  int year;\n};\nint main()\n{\n  printf(\"TechVidvan Tutorial: Bit Fields in C!\\n\\n\");\n    printf(\"Size of timing is %d bytes\\n\",sizeof(struct timing));\n    struct timing t1 = { 16, 10, 2000 };\n    printf(\"Some random date: %d\/%d\/%d\", t1.date, t1.month, t1.year);\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Bit Fields in C!<\/p>\n<p>Size of timing is 8 bytes<br \/>\nSome random date: -16\/-6\/2000<\/p>\n<\/div>\n<p>Let me explain how we got a negative value in the above example.<\/p>\n<p>The value 31 is being stored in a 5 bit integer. This 5 bit integer is equal to 11111. In that case, MSB is 1 and it is a negative number. If you calculate the 2\u2019s complement of 11111 then it would be equal to 00001. And 00001 is equal to decimal number 1. That\u2019s why, you will get a -1.<\/p>\n<p>In a similar way, 4 bit representation of 12 is equal to 1100. And if you calculate the 2\u2019s complement of 1100 then you will get -4.<\/p>\n<h3>Declaration of bit fields in C<\/h3>\n<p>You can declare a bit field inside a structure.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct {\n   data_type [member_name] : width;\n};\n<\/pre>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct {\n   unsigned int age : 5;\n} each_age;\n\n\n<\/pre>\n<ul>\n<li><strong>data_type<\/strong> defines the type of data which can be integer, signed integer or unsigned integer.<\/li>\n<li><strong>member_name<\/strong> defines the name of the bit-field member inside the structure.<\/li>\n<li><strong>width<\/strong> is the number of bits required in the bit field.<\/li>\n<\/ul>\n<p><strong>Example of Basic bit field program<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nstruct {\n  int age : 5;\n}each_age;\nint main(){\n  printf(\"TechVidvan Tutorial: Example of bit field!\\n\\n\");\n   each_age.age = 11;\n   printf(\"First person's age is: %d\\n\", each_age.age );\n   each_age.age = 10;\n   printf(\"Second person's age is: %d\\n\", each_age.age );\n   return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: Example of bit field!<\/p>\n<p>First person&#8217;s age is: 11<br \/>\nSecond person&#8217;s age is: 10<\/p>\n<\/div>\n<h4>Some important points about bit fields in C:-<\/h4>\n<p>You cannot use pointers to the bit field member.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nstruct no_pointer {\n  unsigned int a : 4;\n};\nint main()\n{\n  struct no_pointer point;\n  printf(\"Address of point.a is %p\", &amp;point.a);\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">main.c: In function &#8216;main&#8217;:<br \/>\nmain.c:8:40: error: cannot take address of bit-field &#8216;a&#8217;<br \/>\nprintf(&#8220;Address of point.a is %p&#8221;, &amp;point.a);<\/div>\n<p>Array of bit fields is not acceptable.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">struct array {\nint arr[10] : 3;\n};\n \nint main()\n{\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">main.c:2:5: error: bit-field &#8216;arr&#8217; has invalid type<br \/>\nint arr[10] : 3;<br \/>\n^~~<\/div>\n<p>Bit size of 0 is used to force alignment on the next boundary.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\n\/\/without forced alignment\nstruct case_1{\n  int a : 4;\n  unsigned int b : 5;\n};\n \n\/\/with forced alignment\nstruct case_2{\n  int a : 6;\n  int : 0;\n  unsigned int b : 4;\n};\n \nint main()\n{\n  printf(\"Size of structure without forced alignment is: %u bytes\\n\",sizeof(struct case_1));\n  printf(\"Size of structure with forced alignment is: %u bytes\\n\",sizeof(struct case_2));\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Size of structure without forced alignment is: 4 bytes<br \/>\nSize of structure with forced alignment is: 8 bytes<\/div>\n<h4>Applications of bit fields in C<\/h4>\n<p>Bit fields are more useful when multiple devices transfer information encoded into multiple bits. You can make use of bit fields when storage is limited.<\/p>\n<h4>Need of bit fields in C<\/h4>\n<ul>\n<li>Easy to implement.<\/li>\n<li>Reduce memory consumption.<\/li>\n<li>Provides efficiency and flexibility to the program.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about what bit fields are and how bit fields work. We also discussed the applications and needs of bit fields. We also discussed how you can declare bit fields.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C programming language has a lot of features available to the programmers. The C programming language helps programmers in creating their program code simple and easy to understand. It helps you in a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82885,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3776,3777],"class_list":["post-81735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-applications-of-bit-field-in-c","tag-bit-fields-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bit Fields in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what bit fields in C are and how bit fields work. See the applications, need and declaration of bit field in C.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bit Fields in C - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what bit fields in C are and how bit fields work. See the applications, need and declaration of bit field in C.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-20T03:30:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bit Fields in C - TechVidvan","description":"Learn what bit fields in C are and how bit fields work. See the applications, need and declaration of bit field in C.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Bit Fields in C - TechVidvan","og_description":"Learn what bit fields in C are and how bit fields work. See the applications, need and declaration of bit field in C.","og_url":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-20T03:30:09+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Bit Fields in C","datePublished":"2021-07-20T03:30:09+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/"},"wordCount":647,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg","keywords":["Applications of bit field in C","Bit Fields in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/","name":"Bit Fields in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg","datePublished":"2021-07-20T03:30:09+00:00","description":"Learn what bit fields in C are and how bit fields work. See the applications, need and declaration of bit field in C.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Bit-Fields-in-C.jpg","width":1200,"height":628,"caption":"Bit Fields in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/bit-fields-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Bit Fields in C"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81735"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82885"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}