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




Ruslsoosmed (vasya@domainseoforum.com) on 14.01.2017 20:18:19
Comprar Cialis Espana Contrareembolso Order Valtrex Online With No Prescription <a href=http://ussmd.com>cialis</a> Cialis 20 Efficacite buy accutane online safe Cialis 10mg Erfahrungsberichte Nebenwirkung Levitra 10 Mg <a href=http://byrxboxshop.com>viagra</a> Next Day Shipping Amoxicillin Buy Inderal Online <a href=http://drugs2k.com>generic cialis</a> What Does Cephalexin Have In It Zithromax And Dairy Products Buy Kamagra Quick Amoxicillin Dentist Amoxicillin Doxycycline Acheter Xenical <a href=http://giwes.com>cialis</a> Propecia Treatment For Black Women Levitra Foto Farmacia Discount Hydrochlorothiazide With Free Shipping Shipped Ups <a href=http://abtsam.com>viagra online</a> Tablet Lasix Propecia Nipple Tenderness Buy 25mg Viagra Online Cheap Orlistat Cialis Aus Berlin <a href=http://e4drugs.com>kamagra 100</a> Virgra Direct Provera Secure Ordering In Internet But Lexapro Without A Script Viagra India Pharmacy Extra Super Avana <a href=http://exdrugs.com>viagra</a> Dutasteride Dutas India Pharmacy Online Dostinex
kGI2Tq4pzGv (bpeq8qxm@hotmail.com) on 15.12.2015 15:52:32
Hey Barney! Nice tool, but I've got a couple of <a href="http://eifdjykpus.com">quotsiens</a>:It bumps the version before it executes the migration, and I'm worried that if an error occurs, it'll read like the schema is up to date, when it isn't. I put try/catch/aborts around the migrate(s), and put the revision bump at the end, to see if that would do it it sorta did.I'm still playing with it, so I don't know if that broke something The other comment is, what about a team scenario, where two members may have updated the schema at the "same time" so to speak? I figured that if this tool works for me, that problem could be solved in the "business logic" (always check the repository prior to adding a migration script, say), but thought it was an interesting thought, or whatever. =]Glad that you won a wii, that's cool- I just checked out the projects that were submitted, and I'm kinda stoked- looks like there are a couple of them that would make great additions to what I hope to be a release of Model-Glue actionpacks to make RAD more rapid eh. we'll see.Anyways, thanks for the code, esse!
0xkWsFg0IBya (t4ypzs8hgp@outlook.com) on 11.12.2015 21:18:39
Denny,There IS a bug in the incrementing of the major vsrieon. I've fixed it in SVN, but haven't made a new public release with the fix (one more entry on my list of things to do "sometime"). Since the major vsrieon only updates between migrations, the bug can only manifest itself if there is an issue with the first step of a given migration, but you're right, there is an issue there. The minor vsrieons (steps within a migration) are double protected: the increment happens second, and the whole thing is wrapped in a CFTRANSACTION. The reason the major vsrieon increments first is for internal bookkeeping reasons. It's safe for it to increment even if the first migration step fails, because it'll be minor vsrieon zero (which means the first step is still next in line). The bug was in when the minor vsrieon was reset to zero; it happened at the wrong time before.For the two-developer conflict scenario, there's nothing the tool can do about it. You have to resolve the conflicts when the second developer commits his/her changes. Migration code does have some ordering ramifications, so resolving a conflict might not be as simple as otherwise (quite possibly requiring manual tweaks to the `schema_version` table), but it's really no different from any other conflict between multiple developers.
Jump to page:
1