Tutorials
Articles
OpenGL Demos
Games
OpenGL Misc
MSG Board
About
Donate
Links
Home
Megabyte Softworks
C++, OpenGL, Algorithms




Current series: General programming
(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

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.

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:
Binary OR (in C++ |) means, that if at least one of bits on the same position is 1, the resulting bit will be 1. Let's OR numbers 00100110 and 10110101:
Binary XOR - exclusive OR(in C++ ^) means, that if the bits on the same position aren't the same, (0 and 1 or 1 and 0), the resulting bit will be 1. Let's XOR numbers 01010110 and 11110000:
Another operation is binary shift. It's very simple. You can shift bits to left or right. You just move all bits around. Let's say we shift number 01001100 to the left by 2:
All bits went left by 2, and zeros came from the right side. The same concept is shifting right. Zeros comes from left and you move bits to the right. When you shift either side by 0, it means that the number will remain unchanged. When you shift to the left by one, you multiply the number by 2. When you shift to the right by 2, you divide (integer division) by 2. I hope that's clear. Here is how are all these binary operations made in C++:
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


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.
Name:

E-mail:
(Optional)
Entry:

Enter the text from image:



Smileys




EdwardNup (poisbafsidu@yandex.com) on 26.02.2017 03:53:27
<a href=http://cytotecon.review>order cytotec</a> dn6583bl8383bf8490
PeterJah (goodfiosu@yandex.com) on 25.02.2017 22:01:37
Good http://buygenericrx.men zimax sildenafil <a href=" http://buygenericrx.men/#herbal-viagra-alternative ">viagra side effects</a> viagra 100mg cost
PeterJah (goodfiosu@yandex.com) on 25.02.2017 18:58:04
Good http://buygenericrx.men brand viagra without prescription <a href=" http://buygenericrx.men/#herbal-viagra-review ">viagra side effects</a> generic viagra canada customs
PeterJah (goodfiosu@yandex.com) on 25.02.2017 12:36:01
Good http://buygenericrx.men viagra par internet <a href=" http://buygenericrx.men/#how-much-viagra ">buy generic rx</a> pill splitter viagra
PeterJah (goodfiosu@yandex.com) on 24.02.2017 20:17:29
Good http://buygenericrx.men viagra lowest price <a href=" http://buygenericrx.men/#free-viagra-trial-pack ">best place to buy viagra online</a> viagra history
Jamesmycle (viagriupol@yandex.com) on 24.02.2017 19:37:44
Hello http://buygenericrx.men ; http://via01.biz ; http://sildenafil.review ; http://viadr01.top ; http://viaph01.com
PeterJah (goodfiosu@yandex.com) on 24.02.2017 11:58:14
Good http://buygenericrx.men generic viagra toronto <a href=" http://buygenericrx.men/#mail-order-for-viagra-tablets ">buy real viagra online</a> stop stop stop viagra lyrics
PeterJah (goodfiosu@yandex.com) on 24.02.2017 08:47:11
Good http://buygenericrx.men viagra cost <a href=" http://buygenericrx.men/#viagra-ad ">viagra online canada</a> compare generic viagra
PeterJah (goodfiosu@yandex.com) on 24.02.2017 06:41:23
Good http://buygenericrx.men viagra affiliate program <a href=" http://buygenericrx.men/#viagra-dangers ">buygenericrx.men</a> viagra natural alternatives
PeterJah (goodfiosu@yandex.com) on 23.02.2017 17:19:24
Good http://buygenericrx.men supplier viagra <a href=" http://buygenericrx.men/#young-men-taking-viagra ">best place to buy viagra online</a> buy viagra forum
PeterJah (goodfiosu@yandex.com) on 23.02.2017 02:14:51
Good http://buygenericrx.men can i buy viagra without prescription <a href=" http://buygenericrx.men/#viagra-for-sale-in-canada ">buy real viagra online</a> best prices viagra
Jamesmycle (viagriupol@yandex.com) on 22.02.2017 22:36:51
Hello http://buygenericrx.men ; http://via01.biz ; http://sildenafil.review ; http://viadr01.top ; http://viaph01.com
PeterJah (goodfiosu@yandex.com) on 22.02.2017 21:04:46
Good http://buygenericrx.men womens viagra pill <a href=" http://buygenericrx.men/#(((-top-pharmacy-offers-viagra-online-))) ">http://buygenericrx.men</a> buy viagra no prescription
PeterJah (goodfiosu@yandex.com) on 22.02.2017 18:59:32
Good http://buygenericrx.men viagra spam email <a href=" http://buygenericrx.men/#cheap-generic-viagra-india ">viagra online canadian pharmacy</a> generic viagra real
Jamesmycle (viagriupol@yandex.com) on 22.02.2017 15:45:41
Hello http://buygenericrx.men ; http://via01.biz ; http://sildenafil.review ; http://viadr01.top ; http://viaph01.com
PeterJah (goodfiosu@yandex.com) on 22.02.2017 13:39:01
Good http://buygenericrx.men buying viagra online <a href=" http://buygenericrx.men/#generic-name-of-viagra ">buy viagra online</a> wikipedia viagra
PharmBooro (pharmiurtino@yandex.com) on 21.02.2017 19:48:43
g http://canadapharmacy24.top cheap medications http://canpharm.win canadian pharmacies shipping to usa http://cph5.review cheap medications
GregoryQuony (pbbb@fmaldzb.com) on 21.02.2017 19:31:31
u <a href=" http://canadianpharmacyon.link/#iigo ">online pharmacy medications</a> viagra price, <a href=" http://canph01.com/#gkok ">online pharmacy medications</a> free viagra online, <a href=" http://cph5.review/#bznv ">canadian online pharmacy</a> ordering viagra online
GregoryQuony (oknr@kqeucgt.com) on 21.02.2017 01:30:32
l <a href=" http://cph01.top/#jenj ">canadian pharmacy</a> wiki viagra, <a href=" http://fastpharmacy.men/#eybx ">online pharmacy</a> viagra pills, <a href=" http://canadapharmacy.men/#qcry ">canada pharmacy online canada pharmacies</a> viagra brand 50 mg generic
Cyrilnut (gubn@pbnmnl.com) on 20.02.2017 05:57:48
http://viagrarx.top usa viagra
Jump to page:
1 2 3