Category: Coding Solutions


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.

Ok, here is the example on how to convert a number into equivalent string

It’s not exactly hard to do without any function. All you have to do is extract one number at a time from the whole number and put it into string.

Here you will encounter two problems…

  1. Extracted number can not be assigned directly into string
  2. You can extract the last digit from the whole number so in string you will have shift the numbers each time you add new one

Solution goes as below…

void Int2Str(char* num, int number)
{
int i;
int j;

for(i = 0; number > 0; i++)
{

/* Before assigning, add 48 to the extracted digit so the value will be truned into      ASCII value of equivalent digit since ASCII of digit 0 is 48 */
num[0] = (number % 10) + 48;
number = number / 10;       /* Remove the extracted digit */

/* If the whole number has more than 1 digit then you will need to shift each digit one location to right to make place for new coming digit, otherwise number you get will be reversed */
if ( number != 0)
{
for (j = i + 1; j > 0; j–)
num[j] = num[j – 1];
}
}
num[i] = ‘\0’;      /*Finally, end with the marking that marks the end ^_^*/

}

All done!!!!