Latest Entries »

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.

Hello there. Are you confused about subversion or SVN? Lets first see what exactly this SVN or Subversion is. SVN is version control system used to keep tracks for software version source code. Great thing about SVN is that its Open Source, which means it is free. But being free is not the only merit of subversion. The main reason for bringing SVN to existence was to manage source files and directories. In order to keep copies of every change made to source file, subversion is used. SNV is a version control system that means all version are tracked and copies are maintained. Well, one can argue that why would we need it when we can simply create a back up for each file. Before subversion was created, that was the only way to keeping all the chronicle copies of source code files. For the time being, it was a good solution to save previous data and gain rollback ability if something goes wrong, but those were simpler times too. There would be a small team of programmers working in a small room perhaps. Communication wasn’t really a problem. With the time passing, the process of programing grew complex. Software began to grow more complex. Numbers of programmers involved in a project also increased. Soon enough even the location of those programmers began to scatter and synchronization began to suffer. A need of a tool that could synchronize arose. Today no matter where you go, every company is using such synchronization tool.

Before we see how powerful SVN is, let’s take a look at the history of SVN. It was developed by CollabNet, Inc in 2000. CollabNet were trying to find an alternate or replacement to Concurrent Versions System (CVS). CVS was also a version control system but there were some visible limitations.
Some of them were as below.

  • The revisions were created by commit per file instead of whole project. If you have 5 files and you commit them all, you will have 5 revisions instead of 1
  • CVS would not version any file or directory name change
  • CVS would not version symbolic links
  • CVS would not allow atomic commits.

So CollabNet found themselves in need of a better version control system. CollabNet hired Karl and Ben Collins for this project of new version control system. With all the traditional strength of CVS but none of its obvious flaws, Subversion or SVN was released on 31st August 2001. Even after going through all that trouble and making all that efforts, CollabNet decided to keep SVN free. You are free to download, modify and redistribute SVN on your own accords.

 

SVN Basic architecture
SVN Basic architecture

Now let’s see how SVN works in terms of version controlling. SVN keeps all its files stored on a server available to all who has access to them. Since everything is on a single server, everyone can get same latest updated copy and unless it’s a human mistake, nothing can go wrong. Everyone can work on same code and project can move on smoothly. Generally, the SVN system holds 3 main directories. Branches, Tags and Trunk. Branches is the directory where the source files resides for working. Any complete or work in progress codes resides in this directory. in Tags directories, all the version files resides which are tested and are in working condition or in other words, they compile without errors. This version files are like milestones. they represent a completed state of a segment of the project. Finally the trunk, which is the most important part, holds the completely compilable source code of whole project that may not be able generate desirable result but is able to run without any errors. Branches directory has workspace for each coder working on the project but they all share same Trunk. All of their codes gets merged in the trunk. SVN is a smart system and is able to merge code from different coders, but this merging can sometimes generate conflicts, although nothing that can not be solved. SVN is great tool to keep your source code synchronized and updated.

 

SVN workstyle

SVN workstyle

Finally, we will go through few of the SVN’s major points.

  • Atomic Commits – That means when SVN commits, it commits all of the changes or none of them. That way there is no chance of data correction.
  • Directory version – Unlike CVS, SVN tracks not only files but also directories as well.
  • Version metadata – It not only source but also tracks down the meta data as well which are properties, keys and value.
  • Full revision – SVN retains full revision for every file renamed, copied, moved or deleted
  • Reversed checkouts – SVN can lock files that can not be merged

Well, that’s it for now. I hope I was able to help you understand basic structure and working style of SVN. Please feel free to leave a comment or any suggestion.

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!!!!