(Return to list of General programming articles)
Storing data in computers - it's all about bits
Integers, shorts, longs, bytes... you have probably met with all these names. You have been using them and you still do. But how does it work? What is behind it? How can a computer store numbers and everything else? This article will try to explain it.
It's all about bits. Everything is stored using bits. Bit can be only 0 or 1, thus we use binary number base, because we have obly two digits - 0 and 1. Let's take as an example 8-bit number. What does it mean? It means that we use 8 bits to store this number in memory. People usually use decimal base (but programmers and mathematicians use both :-)). Now let's give it an example. We have number 92. How to represent it in binary? It would be 01011100. Now converting number back from binary to decimal is done as follows:
Take the rightmost bit (the bit that comes first from the right side), multiply it by 2 powered on 0(2^0=1). Add this number to result.
Then take the second rightmost bit, multiply it by 2 powered on 1(2^1=2). Add this number to result.
Then take the third rightmost bit, multiply it by 2 powered on 2(2^2=4). Add this number to result. Then take...
So in our case it would be 0*1 + 0*2 + 1*4 + 1*8 + 1*16 + 0*32 + 1*64 + 0*128 = 92. I hope that's clear.
Now you are ready to learn about RAM memory. RAM memory is made of lots of BYTE blocks. BYTE is just 8-bit number, as described above. Thus there are 8-bits in each block. In computers, we use 8,16,32 and 64-bit numbers. 16-bit numbers occupies 2 blocks of memory, 32-bit 4 blocks and 64-bit 8 blocks, respectively. In C++, they can be represented as:
char or unsigned char; // 8-bit
short or unsigned short; // 16-bit
int or unsigned int; // 32-bit
long long or unsigned long long; // 64-bit
short or unsigned short; // 16-bit
int or unsigned int; // 32-bit
long long or unsigned long long; // 64-bit
Now here's new thing - unsigned. What does it mean? With it, we define whether we want numbers to store only positive or also negative values. Unsigned means, that they don't have any sign (+ or -), because they're always positive or 0. For example, unsigned short is 16-bit number, which can be from 0 to 65535 (2^16 minus 1). But defining only short means, that it can store also negative values and thus its range will be from -32768 to 32767. And how does the computer store negative values?
Let's take an example. If we want to store number -40 in char data type (8-bit), or more generally number -X, computer stores it as binary negation of number (X-1). Binary negation is an operation, that sets all 0s to 1s and all 1s to 0s. It's that simple:
char negative = -40;
// We store the number -X, where X is 40.
unsigned char samebits = ~(40-1);
// ~ is binary negation operator. We need to get number 39, then binary-negate it. 39 is
// 00010111 and thus -40 is represented as 11101000. The bits in variable negative will
// be the same as in variable samebits.
// We store the number -X, where X is 40.
unsigned char samebits = ~(40-1);
// ~ is binary negation operator. We need to get number 39, then binary-negate it. 39 is
// 00010111 and thus -40 is represented as 11101000. The bits in variable negative will
// be the same as in variable samebits.
You should understand the difference between signed and unsigned numbers. Now I'll try to explain basic binary operations. Binary AND (in C++ &) takes two numbers and it compares its bits. If both bits on the same position are 1, the resulting bit will be 1. Let's AND numbers 11010010 and 11001110:
int num1 = 12345, num2 = 67890;
int anded = num1 & num2; // Binary AND
int ored = num1 | num2; // Binary OR
int xored = num1 ^ num2; // Binary XOR (exclusive OR)
int leftshift = num1 << 3; // Multiplied by 8, try to guess why
int rightshift = num2 >> 2; // Divided by 4, try to guess why too
int anded = num1 & num2; // Binary AND
int ored = num1 | num2; // Binary OR
int xored = num1 ^ num2; // Binary XOR (exclusive OR)
int leftshift = num1 << 3; // Multiplied by 8, try to guess why
int rightshift = num2 >> 2; // Divided by 4, try to guess why too
Now I want to speak a little bit about 32 bit and 64 bit systems and platforms. At this time, I develop applications for Win32 platform. Here all pointers are 32-bit unsigned integers. Pointer just points on some block in memory (like described above, memory is made of lots of BYTE blocks). But if a pointer is only 32-bit number, it can point at most on block 4,294,967,295 - the maximum value a 32-bit unsigned integer can store. So it means, that application can use at most 4.2 GB of RAM memory. This number was enough some years ago, because common RAMs weren't more than 512 MB. But today, you can have much more RAM in your computers. And if the application wants to use more than just 4.2 GB, it must use wider memory pointers. 64-bit pointers are enough for now. I don't know for how long, but I guess it's prety enough :-)
I hope that this article helped you to make some things more clear. If you think you don't understand all this stuff, try to think about it, write it on a piece of paper, play with numbers, play with binary operations. You must come on some things by yourself, not everything can be explained.
I also hope, that things I have written about memory and 32 and 64-bit systems are true :-). Why? Because I just deduced it all from what I know about programming and computers. If there is something not true, let me know at michalbb1@gmail.com. I will be also thankful for any feedback.
pbcut (pirysusdshsn@yandex.com) on 15.03.2017 06:42:19 |
dgotq <a href= http://viagraon.click >erectile dysfunction viagra</a> fbeautifull <a href=http://viagraon.click>viagra brands</a> |
sucut (pirysusdshsn@yandex.com) on 14.03.2017 16:11:54 |
pnecessaryn <a href= http://viagraon.click >get free samples viagra</a> tsawp <a href=http://viagraon.click>uses of viagra</a> |
moascer (asuytrkloposds@yandex.com) on 14.03.2017 06:07:00 |
cnaturen <a href=http://canadapharm.review/>online pharmacy</a> emeantf http://canadapharm.review/ |
fuascer (asuytrkloposds@yandex.com) on 14.03.2017 01:39:03 |
spayp <a href=http://canadapharm.review/>discount pharmacy</a> efloorn <a href=http://canadapharm.review/>online pharmacy no prescription needed</a> |
rqcut (pirysusdshsn@yandex.com) on 13.03.2017 21:02:15 |
dsuny <a href=http://viagraon.click>buy viagra online</a> pbrowng <a href=http://viagraon.click>viagra for sale uk</a> |
xeascer (asuytrkloposds@yandex.com) on 12.03.2017 16:03:51 |
icant <a href=http://tadal24.com/>cialis</a> xcany http://tadal24.com/ |
pfdiz (pirtueroputys@yandex.com) on 12.03.2017 15:38:17 |
qlaughq <a href= http://tadal24.com/ >cialis pills</a> amanv <a href= http://tadal24.com/ >cialis sale</a> |
olcut (pirysusdshsn@yandex.com) on 11.03.2017 23:52:54 |
vstrangeb <a href=http://tadal24.com/>buy viagra online canada pharmacy</a> srunningp http://silden24.com/ |
osdiz (pirtueroputys@yandex.com) on 11.03.2017 23:34:05 |
uprettyw http://tadal24.com/ ipromisee <a href=http://tadal24.com/>cialis pills</a> |
aqcut (pirysusdshsn@yandex.com) on 11.03.2017 15:38:35 |
mperfectm <a href=http://tadal24.com/>viagra</a> flipsv http://silden24.com/ |
zzdiz (pirtueroputys@yandex.com) on 11.03.2017 08:30:27 |
odirectionj <a href= http://tadal24.com/ >cialis sale</a> ccontinuedc <a href= http://tadal24.com/ >cialis sale</a> |
mlcut (yspdoidfsada@yandex.com) on 10.03.2017 12:58:25 |
sbearp <a href=http://buyviagra24ph.com>overnight viagra</a> cheap generic viagra substitutes ukeepn <a href= http://buyviagra24ph.com >generic viagra</a> |
qocut (yspdoidfsada@yandex.com) on 10.03.2017 05:21:56 |
awella <a href= http://buycialis24ph.com >buy generic cialis 5mg online</a> udreadfuld http://buycialispharm.com |
glcut (ahjkdusmsd@yandex.com) on 09.03.2017 17:54:21 |
hcountenancev <a href= http://canphv.com >non prescription viagra</a> khappeneda http://canphv.com |
budiz (yiwerytuisde@yandex.com) on 09.03.2017 08:55:04 |
llivingi http://buycialispharm.com rreallyj <a href= http://buycialis24ph.com >zyban beograd cialis pills</a> |
iaHak (isdfgdfwgsil@yandex.com) on 09.03.2017 07:09:52 |
prainf <a href=http://clomid-rx.bid>clomid reviews</a> chairk http://buy-lyrica.bid |
ydascer (ayuropiduad@yandex.com) on 09.03.2017 00:46:36 |
zfavourf http://viagrarx.party pgodv <a href= http://viagrarx.party >viagra</a> |
uoascer (ayuropiduad@yandex.com) on 08.03.2017 10:41:19 |
kmarrieds <a href=http://buy-lyrica.bid>buy-lyrica.bid</a> clooksw <a href= http://buy-lyrica.bid >buy lyrica canada</a> |
ysascer (ayuropiduad@yandex.com) on 08.03.2017 03:48:06 |
afriendl <a href= http://viagrarx.party >buy viagra canada</a> lreasonl <a href=http://clomid-rx.bid>clomid reviews</a> |
yccut (ipoksdgeesf@yandex.com) on 08.03.2017 02:40:54 |
zsatisfiedb <a href=http://buycialis24ph.com>buy cialis online</a> gitselfp <a href= http://buycialis24ph.com >buy real cialis online</a> |