Almost everyone in computer field knows that is a byte is the unit to store data and is made out of 8 bits. A byte holds a single value which is stored as 1 & 0s in those bits. But have you ever wondered if u can print those values? Generally a byte is the accessible memory storage and you can’t access a bit directly but that doesn’t mean that it can not be looked up. And as you know in computers, if you can look it up, you can change it. Who said that? Oh, I just made it up. But fortunately, that’s not the point here. We actually CAN change the value of those bits. Below you can find the code for looking up those bit values & to replace them as well.

In the code given below we are using left bit shifting operator ‘<<‘ and bitwise operator ‘&’. A left shifting operator discards the very left character and moves the whole set of bit to one left and very last bit is set as 0. If we left shift 11001100, it will become 10011000 and leftmost 1 will be discarded. If we left shift 11110011 three times, it will become 10011000. 1<< 5 will return 00100000.

Bitwise operator ‘&’ does something similar to ‘&&’ operator but ‘&’ is designed for binary numbers matching . Bitwise ‘&’ matches bit to bit gives result for every single bit. If you say 11110000 & 10101010, the value returned will be 10100000. It will return 1 only if both bits are 1. We are doing same here. We are taking each bit from the whole set and matching it with 1. If result is 1, that means our bit is 1 otherwise it’s 0.

Bitwise operator ‘|’ is also same as ‘||’ operator. Again, it works on binary code and does bit by bit checking. 11110000 | 10101010 yields us 11111010.

Look up the binary values stored in a byte

void printBits(unsigned int V){
 int i;
 for(i=sizeof(int)*8-1; i>=0;i--){    // printing all binary values in a word size
   if((V & (1<<i)) != 0){             // matching the bit with 1
     putchar('1');
   }
   else{
     putchar('0');
   }
 }
 putchar('\n');
}

int main(void){
 int A = 0x53;

 printf("A: %X\n", A);
 printBits(A);

 return 0;
}

Setting bit pattern in the binary values stored in a byte

Now lets see how to actually set the bit pattern we want and change the data value according to. We are again using concept of bitwise ‘&’ and ‘|’. But here we are using bitwise exclusive or (XOR) which is ‘^’. It yields 1 only if both bits are different. If they are same like 1 and 1 or 0 and 0, the result will be 0. 11110000 ^ 10101010 will give us result of 01011010.

void SetBitPattern(unsigned int& V, const char* pattern, int startBitIndex)
{
  int i, j;

  for(i= startBitIndex, j = strlen(pattern)-1; i < (startBitIndex + strlen(pattern)); i++, j--)
  {
    if(pattern[j] == '1')   // if bit pattern is 1, we use | with 1 to make bit of V a 1
      V = V | (1<<i);
    else
    {
      if ((V & (1<<i)))     // if pattern bit is 0 and bit of V is 1, we must use ^ to make bit of V a 0
        V = V ^ (1<<i);
     }
  }
}

int main()
{
unsigned int A = 0x53;

SetBitPattern(A, "110011110", 4);
return 0;
}

There is also a one line code to do this process which is very complex and messy looking. Perhaps after few days I will post it here.