Wednesday, January 4, 2012

a little fun with bits

you all have seen bits while reading something related to computers , specially in fields like computer architecture, etc but bits can serve us a great comfort in coding or solving medium or tough problems in computer languages like c or c++. so here we go....
suppose A=1100 ,B= 1010 then
A|B= 1110 (OR)
A&B= 1000 (AND)
A^B= 0110 (XOR- if there are odd number of 1s then 1 else 0)

x<<y means left shifting of bits of x by y or multiplication of x with 2^y
x>>y means right shifting of bits of x by y or division of x with 2^y

the most important use of shifting is to access a particular bit, for example 1<<x denotes x bit from the right set (1) and all other not set (0)



following are some operations on sets in terms of bits, (A=1100 B=1010, last item is absent in both , second last is present in B but absent in A and so on)

Set Union- A|B
Set Intersection- A&B
Set Negation(0 to 1 or 1 to 0)- ALL_BITS^A (where ALL_BITS is number with same number of bits as in A but all set (1) )



if we want to substract two numbers in  binary form then we instead perform a add operation ..... this is shown below
if A= 10101 and B= 11000 and we want to perform A-B then we would perform A+(-B), now in order to convert B to -B we have to take its two's complement as follows
B= 11000
B's one's complement= ~B= 00111
B's two's complement= ~B+1= 00111+00001= 01000
now simply A-B= A+(-B)= 10101+01000= 11101


Now suppose we want to find out the lowest bit in x which is set (1),,, by lowest we mean first encountered from the right hand side....
thus if x= 10100 and -1= 1111111 (in -1 all the bits are set, always)
x-1= x+(-1)= 10011
~(x-1)= 01100
x & ~(x-1)= 00100

thus to find the lowest bit which is set(1) perform x & ~(x-1)


to reverse the bits of an integer something not so simple has to performed...

x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);



bye bye :)




32-bit.........64-bit

This is the question which had been worrying me since a long time.... Whats the difference between a 32-bit and 64-bit Operating System. There are many and some are really difficult to understand unless u  are full equipped with full understanding of Computer Architecture and Organisation. But for a mere understanding,..... i am trying to explain. The total difference lies in the way memory is accessed. In earlier days , 4GB of RAM was more than sufficient for Personal Computers but with advancement of technology RAMs upto 16GB are available. Now suppose u have a computer running a 32-bit OS then u can have a RAM upto 4GB only. This is explained below-
2^32/(2^10 * 2^10 * 2^10)= 4GB
But suppose u fixed a RAM of 8GB on your computer, you have done a holly shit since your computer would be using only 4GB of it and rest 4GB went in vain. Thus in order to utilise the whole 8GB u should have a 64-bit OS on your computer.
2^64/(2^10 * 2^10 * 2^10)= 2^34GB
According to above calculation 64-bit OS can handle upto 2^34GB of RAM. But  this is practically impossible, so whats the upper bound on the size of RAM for 64-bit OS ? Hmm, at this moment i dont have the answer to this question,,,, :(

Now-a-days developers are focussing on developing 64-bit softwares and applications. Next i would be writing on running 32-bit apps on 64-bit or vice-versa. Till then gud bye... :-)

Some basic terms about Computer architecture

In this post am gonna write something about few very important terms about computer architecture,,,

according to speed we can say our list (in decreasing order of speed) consists of following:-
Registers
Cache Memory
Main Memory (RAM)


Registers- Processor registers or simply registers are located inside the processor. They typically hold a word of data which is of 32-bits or 64-bits. CPU instructs the Arithmetic and Logic Unit (ALU) to perform various operations on this data or with the help of it. They are the fastest of all forms of computer memories. CPU cannot process all the data and instructions at once and thus registers act as buffer and store all instructions at once and then CPU access these instructions from these registers at its speed.


Cache Memory-  it is the intermediate memory between the super fast registers and the relatively slow main memory. Its a very small capacity and high speed memory which acts as a buffer between the CPU and main memory. Actually access time of main memory is much more than accessing cache memory so whenever the CPU wants something , instead of directly going to the main memory it first searches the cache memory  and if it is not found there then it has to access main memory. Thus most frequently accessed instructions and data are put in the fast cache memory for speedy operations.

Main Memory- memory is nothing but RAM (Random Access Memory). Actually firstly i thought that it consists of both RAM and ROM but i was totally wrong. This memory is relatively costly, small sized and volatile (data is lost when computer is switched off).