2

In the C++ tutorials I use to learn C++, they mention the data types of C++. Some of these data types are double and long double with float, and long int, long long int, and short int with int. These vary in bits, as a float contains 32 bits, double contains 64 bits, and so on with a long double. The same thing happens with the int family. My question is what do bits do and what difference is there between a variable with greater amount of bits and a variable with a smaller amount of bits?

  • Write your name on a paper. Do you need a A4 Sheet or just a small piece of paper ? – Florent Bouisset Oct 19 '20 at 01:37
  • Why is he being voted down? That is a legitimate question. And a fair question to ask if you don't know. Stop voting him down. The kid is 12. – ReverseFlowControl Oct 19 '20 at 01:38
  • If you take a full page, it s a waste of paper, if you take too little paper, you won’t be able to write your name down. For memory and variable it’s the same. You have several « size » of int, you will be limited at some point. – Florent Bouisset Oct 19 '20 at 01:40
  • what do you mean @Delmak. –  Oct 19 '20 at 01:41
  • @Martheen: fair enough. Give the kid a few links to good references and closed the question? – ReverseFlowControl Oct 19 '20 at 01:42
  • `short` is for small integers, `int` for medium, and `long` (or `long long` depending on platform and compiler) for large. But generally just use `int` for any integer value between minus two billion and plus two billion (which is enough for most uses). – Some programmer dude Oct 19 '20 at 01:43
  • Ok @Delmak. Understood. Also, just saying you could have posted that as an answer. –  Oct 19 '20 at 01:43
  • and Why are you guys talking like I am not here. I understood @Martheen. and thanks a lot reverseflow. You guys are helpful. Next Time I will research. –  Oct 19 '20 at 01:45

1 Answers1

5

The answer to your question is a bit long because it is such a basic question (pun intended).

First, internally computers represent information using the binary system (base 2) as opposed to the decimal system you are familiar with (base10).

One bit is a digit in the binary system of representing numbers. In binary there are only two digits: 1 and 0. So each one is called a bit. In decimal, there are ten digits 0,1,2,...,9; each one is called a digit. It just so happens that in binary a digit has another name, bit.

My question is what do the bits really do?

Bits are just digits. They store information. Just like if in decimal you could have 4 digits instead of 2 digits; you can count up to 9,999 with 4 digits but only up to 99 with 2 digits in decimal. So the more digits, or bits, you have the more numbers you can represent. That is, the more data you can store.

And what are bits and bytes too?

I already explained what bits are above. Bytes are a little trickier to explain. In the computer world we have standards; very long documents that people and corporations spend a lot of time thinking about to make reasonable conventions/agreements that everybody can rely on. This way, if we all agree on the meaning of certain words and concepts, it makes it easier to work together because we can all look at the standard and resolve most disputes/arguments that way. These standards get revised every few years, but for the most part things get added and very few things get removed.

One such standard is the C standard; incidentally, there is a C++ standard as well. The C standard says, in section 3.6, the following:

byte: addressable unit of data storage large enough to hold 
      any member of the basic character set of the execution environment

Note 2 to entry: A byte is composed of a contiguous sequence of bits, the 
                 number of which is implementation-defined. The least significant 
                 bit is called the low-order bit; the most significant
                 bit is called the high-order bit.

So, the definition of "byte" is implementation-defined. This means that it depends on the system you are using. In some machines it will be 8 bits, as mentioned above, and in others it might more than 8, but it cannot be smaller than 8 bits for the C or C++ languages for technical reasons (See comments below for reference). It is up to the makers of the machines to decide.

However, because very early on some popular computer makers defined it as 8-bits most systems today define a byte as 8 bits. Which is what the other answer tells you. In particular, most modern computers are based on the x86 architecture which defines it as 8 bits; new machines using ARM also define it as 8 bits, think of your iphone/ipad. So, in practice it is safe to assume 8 bits, although theoretically it could be anything. And there are some machines that don't define a byte as 8 bits.

If a long int and int vary in bits on a system, what can the long int do that the int can't?

int                     -32767 to               +32767 representable in 16 bits
long               -2147483647 to          +2147483647 representable in 32 bits
long long -9223372036854775807 to +9223372036854775807 representable in 64 bits

ISO/IEC 9989:1999, paragraph 5.2.4.2.1. "Their implementation defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."

This is directly from the C standard, the 1999 revision. As you can see you can count a lot more with a long than with an int; so you can store more data with a long than with an int.

Modern systems define int, long and long long a little more different than that. To see a good explanation of the actual definition for modern systems, and modern standards (more recent revisions), see "What's the difference between long long and long?".

Going by the standard, all that's guaranteed is:

  • int must be at least 16 bits
  • long must be at least 32 bits
  • long long must be at least 64 bits

Today if you get GNU/CLANG compilers for C or C++, with no flags added to the compiler to change standards, if you get an int and a long they will probably both be 32 bits in length. Note that this conforms to the standard; 32 bits is at least 16 bits for the int; and 32 bits is at least 32 bits for the long.

  • *"it might more than 8 or fewer than 8."* Both C and C++ require at least 8 bits per byte so it can't be less than 8 if the platform is supported by either of those languages. – François Andrieux Oct 19 '20 at 04:05
  • @FrançoisAndrieux: in what standards revision is that? I have not encountered it yet. – ReverseFlowControl Oct 19 '20 at 04:07
  • It was such since C89 and inherited by C++ from the start. See `CHAR_BIT`. – François Andrieux Oct 19 '20 at 04:22
  • I see it. It's actually in the same section mentioned above, in the C99 standarrd its in 5.2.4.2.1. The very first item on the list. But, the kicker is in note 4 section "6.2.6.1 General" : "Values stored in non-bit-field objects of any other object type consist of n×CHAR_BIT bits, where n is the size of an object of that type, in bytes. " Essentially defining a byte to be at minimum 8 bits in C. In fact, footnote 40 confirms "A byte contains CHAR_BIT bits, and the values of type unsigned char range from 0 to 2^( CHAR_BIT ) − 1." – ReverseFlowControl Oct 19 '20 at 04:22
  • @FrançoisAndrieux: Fixed. Thanks! – ReverseFlowControl Oct 19 '20 at 04:26