title: Bit manipulation and masking in c
body: <p>I am working on some c problems. I have one process that needs to be executed and has bit manipulations. I have 2 variables with uint8 type say <code>uint8 bit_Part1</code> &amp; <code>uint8 bit_Part2</code> (U8 each). I want to add values to these variables having values one is <code>U4</code> and other <code>U6</code> like <code>U4_part1</code> and <code>U6_part2</code> and one boolean value <code>Bool_Part3</code> I want to place them into these <code>uint8 bit_Part1</code> &amp; <code>uint8 bit_Part2</code> structure as below</p>
  <p><a href="https://i.stack.imgur.com/eyoMO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eyoMO.png" alt="enter image description here" /></a></p>
  <p>So far i tried shifting like:</p>
  <p>Its psudo code:</p>
  <pre><code>uint8 bitman_u8_part1 =0;
      uint8 bitman_u8_part2 =0;
      bitman_u8_part1 |= ((uint8)(U4_Part1));  // 0000\\\\ 
      bitman_u8_part1 | = ((uint8)((U6_Part2 &gt;&gt; 2)&lt;&lt;4));// append 0000 for the empty 
      bitman_u8_part2 | =((uint8)(EC2_TCH_BITMAP_U6_Part2&lt;&lt;4));
      bitman_u8_part2 | =((bool)BOOL_Part3 &amp; 1&lt;&lt;4);
  </code></pre>
  <p>Here is an example of what I expect to see:</p>
  <p>If U4_part1=0111, U6_Part2=001111, Bool_Part3=1(true)</p>
  <p>then the expected result is:
  bit_Part1 &amp; uint8 bit_Part2 both will contain data -&gt; 00000|1|00||1111|0111</p>
  <p>(note I have added | just for separation between data || is for separation of uint8 bit_Part1 &amp; uint8 bit_Part2).</p>
  <p>Additional values are in uint8 bit_Part2 are filled with 0, compare with picture above (&amp; U4 means value with 4 bits U6 as in with value with 6 bits)</p>
  <p>How can i do it?</p>
  <blockquote>
  <p>UPDATE:</p>
  </blockquote>
  <p>Something that i have tried again :</p>
  <pre><code> unsigned int U4_data1 = 12;    /* 60 = 0000 1100 */  
     unsigned int U6_data2 = 53;  /* 21 = 0011 0101 */
     unsigned int Bool_data3 =1;
     unsigned int U8_part1 =0;
     unsigned int U8_part2 = 0;
     U8_part1 = U4_data1;
     U8_part1 |= (U6_data2&lt;&lt;4);
     printbinary(U8_part1);
     U8_part2 |= U6_data2&gt;&gt;4;
     U8_part2 |= (Bool_data3)&lt;&lt;2;
  </code></pre>