title: Setting Parity Bit in a Transmission Program
body: <p>I am writing a program that simulate a transmission of characters over the network.
  I have written the following function:</p>
  
  <pre><code>int getCharBit(char c, int bitNum){
      return (c &amp; (1 &lt;&lt;bitNum)) &gt;&gt; bitNum; 
  }
  
  
  // returns the ith bit of the character c
  int getShortBit(short num, int bitNum)
  {
      return (num &amp; (1 &lt;&lt;bitNum)) &gt;&gt; bitNum;
  }
  
  
  // sets bit i in num to 1
  int setShortBit(int bitNum, short *num){
     return  num | (1 &lt;&lt; bitNum);
  }
  
  
  // count the number of bits in the short and returns the number of bits
  
  /* input:
     num - an integer
  
  Output:
  the number of bits in num
  
  */
  
  int countBits(short num)
  
  {
  
     int sum=0;
     int i;
     for(i = num; i != 0; i = i &gt;&gt; 1){
        sum += i &amp; 1;
     }      
     return sum;
  
  }
  </code></pre>
  
  <p>I also written a function that counts the number of ones in a short integer num and a mask:</p>
  
  <pre><code>int countOnes(short int num, short int pMask){
     short tempBit = num &amp; pMask;
     sum = 0;
     while(tempBit &gt; 0){
        if((tempBit &amp; 1) == 1){
           sum ++;
        }
        tempBit &gt;&gt; 1;
     }
     return sum;
  }
  </code></pre>
  
  <p>and a function that sets the Parity Bit:</p>
  
  <pre><code>int setParityBits(short *num)
      // set parity bit p1 using mask P1_MASK by
      // get the number of bits in *num and the mask P1_MASK
      int numOnes = countOnes(num, P1_MASK);
      // if the number of bits is odd then set the corresponding parity bit to 1  (even parity)
  if ((numOnes % 2) != 0){
     setShortBit(1, num);
  }
      // do the same for parity bits in positions 2,4,8
  
  int numOnes2 = countOnes(num, P2_MASK);
  if ((numOnes2 % 2) != 0){
     setShortBit(2, num);
  }
  int numOnes4 = countOnes(num, P4_MASK);
  if ((numOnes4 % 2) != 0){
     setShortBit(4, num);
  }
  int numOnes8 = countOnes(num, P8_MASK);
  if ((numOnes8 % 2) != 0){
     setShortBit(8, num);
  }
  </code></pre>
  
  <p>I am also given a few function that are supposed to read the input and transmit it. The problem is in one of the functions I have written. </p>
  
  <p>For example, if I run the program and type hello as an input, I should get 3220 3160 3264 3264 7420 as an output, but I get 0 0 0 0 0.</p>
  
  <p>I can't seem to find what I was doing wrong, Could someone please help me?</p>