Hello, Can anyone please explain role of binary in computer?
Analog computers do (or did) exist. And they work well for what they do. But they aren't as flexible as digital computers.
Binary is popular because electronics most easily deal with ON and OFF, or HIGH and LOW, or POSITIVE and NEGATIVE. All digital computer operations can be traced to binary AND, OR, NAND and NOR logic.
RF-based digital communication (cable, cellular, et al) is rarely binary. Typically, a symbol (a unit of transmission, at one time called a baud) represents more than one bit, and may represent up to six bits of data.
There are many books available (though probably not in your native language) that can teach you the details and show you just why these things are. Google/Yahoo should help you dig up some solid reference materials that are almost as good as books.
Modern computers are based on transistors, which are like light switches -- they are either on or off.
Binary means, roughly, "two", so we use that term to describe this on-off system.
All data processing ("thinking") in the computer is done with on-off switches. Each individual switch is called a "bit." Most computers group the switches into groups of eight, which we call "bytes." A byte can store any number between 0 and 255. A byte can't store any other number.
To store other things, such as letters or colors, you find a way to encode your data as raw numbers between 0 and 255. ASCII is a way of encoding English text as numbers between 0 and 255. RGB is a method of encoding a color as three colors: red, green and blue. Each individual color is stored as a number between o and 255.
Groups of bytes can be combined together to store numbers larger than 255.
8 bits (one byte) stores 256 possible values.
16 bits stores about 66,000 possible values.
32 bits stores about 4 billion possible values.
64 bits stores about 16,000,000,000,000,000,000 possible values.
There are about 60,000 words in the English language, so it might be possible to encode English words using only 16 bits, but 32 bits would be safer.
There are about 6 billion people on earth, so this number wouldn't fit in 32 bits; you'd have to use 64 bits.
In code, we write down binary numbers using ones and zeroes, like this:
It's customary to write a space between groups of eight (or groups of four) bits, like this:
00000100 10010010 01100110 11010100
A group of four bits is called a "nybble," which is a joke, because a nibble is a little bite.
Three important operations with binary numbers are "or", "and" and "xor" (pronounced "zor" or "x-or").
In the C language, you write "or" as |,
"and" is &
"xor" is ^
"Or" works like this:
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
In other words, if either (or both) is 1, the answer is 1.
"And" works like this:
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
In other words, if both are 1, the answer is 1.
"Xor" works like this:
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
In other words, if one or the other is 1, the answer is 1.