Integer number explanation

Opprend

Member
Joined
Nov 5, 2013
Messages
8
Programming Experience
Beginner
I start to learn thoroughly vb.net by myself


I know that an integer number ranges from -2 147 483 647 to 2 147 483 647


MSDN states that an integer number is 32 bits (4 bytes) signed


Integer Data Type (Visual Basic)


Could you give me an explanation that I would understand at my stage of beginner:


32 bits---4 bytes ----signed


Thank you.
 
Don't worry about the 16/32/64 bits, the bytes of memory it takes to store the number, or whether it's signed or unsigned. Is the number that you need to hold larger (or going to be larger) than a Short Integer but smaller then a Long Integer? If so, then use Integer, otherwise use Short or Long as needed.
 
Thank you for answering me.
Currently, I'm taking the habit to read all posts and threads of this nice forum, and I see that every senior or experienced person gives always, and repeatedly
the same advice: learn the fundamentals.
So, I'm really taking this advice seriously.
 
There are 8 bits in a byte, each bit represent an on/off state and is a binary representation of a data value. Signed data types uses one bit to denote a positive or negative value and therefore have one bit less to use for the numeric value itself. Byte data type (unsigned) for example uses 1 byte of memory and has number range 0 to 255, while SByte (signed) has number range -128 to +127.
 
To really understand it at the basics, you need to understand a little bit how binary notation works. Binary (aka base2) uses an alphabet containing only 2 symbols, 1 and 0, just like hexadecimal contains an alphabet containing 16 symbols (0 to 9 and A to F). As said above a byte is 8 bits. Each bit in a byte represents a value dictated by the bit's position (2^n, where n is the bit position). The least significant bit (LSB)'s value is 1, and the others are 2, 4, 8, 16, 32, 64 and 128. Thus, the maximum value for an unsigned byte with all its bits at 1 is 255 (binary 11111111; 1+2+4+8+16+32+64+128). Now in maths we also need to represent negative numbers, so in those cases we use the most significant bit (MSB; that is the one worth 128) to represent the sign. The range of a signed byte is thus -127 (00000001) to 127 (11111110). Clearly that still isn't large enough to contain all the numbers we need to calculate, so we use multiple bytes to represent larger numbers. A "word" for example is 2 bytes. In binary it's just a continuation of what we had before, 1+2+4+8+16+32+64+128+256+512+1024+2048+4096+8192+16384+32768, so the maximum value for an unsigned word is 65535, or -32767 to 32767 for a signed word, and so on. Byte, word, integer and long are just names we use to describe how many bytes will be used to contain the value. Word is 2 bytes, integer is 4 bytes, long is 8 bytes, etc...

Note that the above is using the one's complement notation system, which was used on older hardware. It has a problem in that, if you followed so far, it has two ways to represent 0 (00000000 [0] and 11111111 [-0]) AND it needs an additional bit for carry (read an assembly book). Modern hardware use a notation system called two's complement, which eliminates this problem.

There are other complications when considering a 64 bits system, endianness (is the LSB on the left side or right side?), different OSes or programming languages, that determine how the value is stored in memory, but this should give you a basic understanding anyways.
 
Last edited:
I should add that very rarely today will you actually need to perform binary arithmetic, most code that still uses binary usually use it as a bitfield. Each bit represents a boolean value.
 
I'll take your explanation as a basis to learn more about these concepts.
I really want my learning to start from a solid ground. This means that when I speak about any keyword, method, behavior,...I must have a clear idea.
Thank you very much.
 
Back
Top