You are here:

- Home
- Computing/Technology
- C/C++
- C++
- C++ newbie

Advertisement

Hey,

I just wrote a simple program that went very wrong, can you take a look and tell me why the lines are continuous instead of separate.

(Note: this program is designed to print a table of equal earth and moon mass from 1 to 100 pounds and output an empty line for every 25 lines.)

#include <iostream>

using namespace std;

int main() {

double e, m;

int a = 0;

for (e=0; e<=100; e++) {

m = 0.17 * e;

cout << e << " pounds on Earth is equal to " << m << " pounds on Moon. ";

a += 1;

if (a == 25) {

cout << '\n';

a = 0; }

}

return 0; }

I also have some question about bits. What are they? what does it mean when someone say that the bit width of char is 8? what are high-order bits? These are NOT homework questions, please help me understand.

Thanks,

Angela

Well to answer the first part we have to look at what your program does.

New lines are _only_ written when you ask for them as you do here:

if (a == 25) {

cout << '\n';

a = 0; }

This is the _only_ place your program appears to output a new line. As this statement is within an if statement it is only executed when the condition for the if statement is true - i.e. when a==25. As a is incremented from 0..25, then set back to 0 you only ever get a new line output every 25 iteration of the for loop.

To print each value on a line on its own you have to output a new line after each table entry. That is replace the space after "Moon." with a new line "Moon.\n":

cout << e << " pounds on Earth is equal to " << m << " pounds on Moon.\n";

I think that even with your limited knowledge of C++ you could have worked this out for yourself with a little pause for thought and examination of the code.

It is _very_ important that you do not get put off, discouraged or panicked because your program does not do what you want the first time you run it. They rarely do for anyone.

Now onto bits.

A bit is a binary digit (BInary digiT) and is the basic unit of storage and communication in digital computing and information theory.

Binary is a number system that counts using base 2. Hence in binary the only digit values are 0 and 1. Thus a bit is either 0 or 1, (we can also consider then states such as true or false, or on or off, or yes or no etc.)

Obviously being able to count from 0 to 1 is not very useful. Just as counting in denary (usual base 10 counting), when we run out of digits we add more:

0, 1, 2, 3,4, 5, 6, 7, 8, 9, 10, ..., 99, 100, ... 999, 1000, ...

so we do in binary - we add more binary digits or bits:

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, ...

which are the denary numbers 0 to 15 expressed in binary.

We therefore require 4 bits to express 16 states (0 through to 15). In fact the number of bits required to represent a value is related to the next higher power of 2 from the value just as in base 10 we require a number of digits that are related to the next higher power of 10 to express the value:

So in base 10 we require:

1 digit to express values from 0 up to but not including 10 to the power 1 (which is 10)

2 digits to express values from 0 up to but not including 10 to the power 2 (which is 100)

3 digits to express values from 0 up to but not including 10 to the power 3 (which is 1000)

etc.

And in binary we require:

1 digit to express values from 0 up to but not including 2 to the power 1 (which is 2)

2 digits to express values from 0 up to but not including 2 to the power 2 (which is 4)

3 digits to express values from 0 up to but not including 2 to the power 3 (which is 8)

4 digits to express values from 0 up to but not including 2 to the power 4 (which is 16)

5 digits to express values from 0 up to but not including 2 to the power 5 (which is 32)

6 digits to express values from 0 up to but not including 2 to the power 6 (which is 64)

7 digits to express values from 0 up to but not including 2 to the power 7 (which is 128)

8 digits to express values from 0 up to but not including 2 to the power 8 (which is 256)

9 digits to express values from 0 up to but not including 2 to the power 9 (which is 512)

10 digits to express values from 0 up to but not including 2 to the power 10 (which is 1024)

etc.

You will notice that 2 to the power 10 is a little more than 10 to the power 3. This gives a quick rule of thumb for working out the number of bits required for common decimal number sizes. 1000 (kilo) requires 10 bits, 1000,000 (mega) requires 20 bits, 1000,000,000 (giga) requires 30 bits etc.

When we group bits together we form binary numbers. Some binary number digit sizes have names. For example a binary number having 8 bits is called a byte or octet and can represent values from 0 to 255 (note: once upon a time byte was not fixed at 8-bits - I vaguely remember a reference to a 6-bit byte). Larger groups of bits are called a words. In some areas (specifically concerned with terms used with Intel processors) word came to mean a 16 bit (binary digit) number and double word or dword a 32 bit number etc. However in general word is a group of bits of any size, generally larger that 8 bits.

See online references for more on bit for example the Wikipedia article at http://en.wikipedia.org/wiki/Bit - remember Internet search engines (e.g. Google) are your friend.

So a character that has a bit width of 8 means a character is represented by an 8-bit value (or byte) and can therefore store 256 different values. These values - which if expressed as decimal numbers would be the integer numbers from 0 to 255 - represent characters. Which characters they represent and how is determined by the character set and character set encoding. For example a common character set is the 7-bit ASCII character set (see http://en.wikipedia.org/wiki/ASCII). The encoding in only 7 bits so only uses half the values available, typically the lower half - values 0 ... 127 (although I once worked on a system in which the characters used the upper half of the available values: 128 ... 255). This lack of use of all available values did not go unnoticed and was commonly used to add locale specific additional characters - called code pages.(see http://en.wikipedia.org/wiki/Code_page). Other character sets include IBMs EBCDIC, an 8-bit character set (see http://en.wikipedia.org/wiki/EBCDIC).

and Unicode.

Unicode is interesting as it defines so many characters that they cannot all be represented in one byte. In fact I think that space for more than a million characters (needing 21 bits) has been defined and around 100,000+ characters have been defined so far (requiring 17 bits, maybe 18-bits by now) to fit within that space. This means that you would require a 17 or 18 bits to represent all characters (or 21 bits for all space defined in Unicode). As such word sizes are not common on modern computers (they commonly like multiples of 8: 8, 16, 32, 64), Unicode has various different encodings, of which the most obvious is one that fits in 32 bits as 32-bits is large enough to represent the whole defined Unicode character space, let alone all the defined characters in that space. Next we come to the 8 bit encoding - UTF-8. As 8-bits is not enough to hold all values for Unicode characters some (many) characters require more than one byte to represent. In fact it currently requires up to 4 bytes to define a Unicode character (and the encoding can be expanded to allow up to 6 bytes). Such encodings are called multibyte encodings. Note that there is also a 16-bit Unicode encoding that fits most of the commonly use characters in the World in one character word but require an additional word for some characters. See http://en.wikipedia.org/wiki/Unicode for more on Unicode.

There are other multibyte encodings, and indeed other character sets.

Ok so that is characters and character bit-widths.

Next you asked about high order bits. High order bits are those with higher power-of-2 weighting. Thus in a 2 bit binary number: 11 (3 decimal) the bit to the left has a higher power-of-2 weighting than the bit to the right and is therefore a higher order bit than the one on the right.

The bit on the right has a 2 to the zeroth power weighting - i.e. 1 and thus it 1 adds 1 to the value of the number as a whole.

The bit on the left has a 2 to the 1 power weighting - i.e. 2 and thus it 1 adds 2 to the value of the number as a whole.

In a binary number of larger width (i.e. having more bits) - say a 16 bit word - we can say the higher order byte or higher order 8 bits. This would be the 8 bits to the left:

High order byte Low order byte

1010 1010 1010 1010

An alternative to high and low order are the terms most significant and least significant. That is the high order byte above is also the most significant, as if any of its bits are 1 its value contributes more to the total value of the binary number (256 - 65280) and the low order byte the least significant, as if any of its bits are 1 its value it contributes less (1 - 255).

We also have a most significant bit (MSB) and least significant bit (LSB):

(MSB)1010 1010 1010 1010(LSB)

Hope this helps and please remember that the basic information on things like bits, bytes, character sets, etc. are all out there on the Internet on one site or another. Much of what I have said here is also already on the Wikipedia pages I pointed you to (in fact I double checked my figures and terminology with them).Search sites such as Google and Yahoo and online information sources such as Wikipedia are your friends. Use them.

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | thank you so much, that really helped |

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines.
Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is).
When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD.
My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes.
I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.**Education/Credentials**