1. ## Confused newbie

Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
How would the number 2321 be spanned through memory? and the answer is:
1st byte 9 and 2nd byte 17

What do they mean by this? I've looked through the section again and do not understand the question or the answer.

What would be the result of the following operation:
int Result, a, b, c;
a = 9;
b = 1;
c = 2;
Result = c-- + (b++ - --b) * a + c
They have the answer as 11, but I have the answer as 21. Can someone explain to me how they have that answer and what I did wrong. Here is what I did:
first I did my post and prefix operation which make the problem like this:
result = 2 + (1 - (-1)) * 9 + 1
result = 2 + (2) * 9 + 1
result = 2 +18 + 1
result = 21

The b gets added 1 to the taken directly away again right??? since it is a negative 1 then it is added to to make a 0 and then taken from to make a negative 1 again and then 1 - negative 1 is prositive 2.

The c's are 2 and 1 which make 3 right? I'm confused.

Thanks for any help.
Nice board!

2. I doubt the answer is even possible in the second exercise...

3. Both questions are relying on platform and compiler specific items. This is bad, because such things are not truly portable. When you study C++, you should focus on more general things that work on all or almost all systems. Learning something specific and thinking it's always true will bite you later on when you least expect it.

Let's take their first question. How is 2321 represented in memory? The answer might surprise you: We don't know. Each system is free to keep memory organized in any way they want. Motorola-based systems usually keep integers in big-endian format, while Intel-based chips generally keep integers in little-endian format. This can be quite confusing, and you should write programs that do not assume anything about the underlying implementation, with an obvious exception being that you are writing software specific to that system.

Now here's two ways 2321 might be represented in memory, assuming an integer is 32-bits:

Big Endian:

Code:
`00000000 00000000 00001001 00010001`
Little Endian:

Code:
`00010001 00001001 00000000 00000000`
Little Endian "reverses" the bytes around from what you might naturally expect the number to be.

What they mean by saying 9 and 17 is that one byte in the answer taken by itself makes up the number 9 and the non-zero byte by itself makes up the number 17. Your book might be assuming natural arrangement, such as how the number would be represented in registers and such (and I suppose that might not really truly be endian-independent), but to ask specifically about memory deserves such a long answer.

Now taking your second question.... Unless they clarified the question to a purely mathematical sense, the question is again an answer of "we simply don't know". The problem lies in the ++ and -- operators. In C and C++, we can't guarentee the order in which they will be executed inside of a function. One might be operated before the other.

Take this example:

Code:
```int x = 5;
int z = ++x + --x```
It might resolve to any of these:

Code:
`z = 6 + 5 = 11`
Code:
`z = 5 + 4 = 9`
Why? Because compilers are free to set the order of those operations. Your book is again appearing to assume way too much with regard to how a system will implement such expressions.

I took from your example and wrote this bit of code:

Code:
```#include <iostream>

int main()
{
int a = 9;
int b = 1;
int c = 2;
int Result = c-- + (b++ - --b) * a + c;

std::cout << "Result = " << Result << std::endl;

return 0;
}```
My output:

Result = 4
So, as you can see, you can't rely on how such an expression will be resolved since we all disagree on what the answer is.

I suggest you consider getting a different book to learn from.

4. The first exercise assumes a lot. Let's assume that when we say "first byte", we're talking about the fist sequence of eight bits that aren't all zero, counting bytes from the big end, but counting bits within them from the small end. Then
Code:
```2321 = base2(0000100100010001)     //if this isn't duh to you, just look up binary representation. No big deal.
2321 -> 00001001  00010001
00001001 = base10(9)
00010001 = base10(17)```
I think that's right.

*beat me to it*

5. Originally Posted by MWalden
Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
How would the number 2321 be spanned through memory? and the answer is:
1st byte 9 and 2nd byte 17

What do they mean by this? I've looked through the section again and do not understand the question or the answer.

What would be the result of the following operation:
int Result, a, b, c;
a = 9;
b = 1;
c = 2;
Result = c-- + (b++ - --b) * a + c
They have the answer as 11, but I have the answer as 21. Can someone explain to me how they have that answer and what I did wrong. Here is what I did:
first I did my post and prefix operation which make the problem like this:
result = 2 + (1 - (-1)) * 9 + 1
result = 2 + (2) * 9 + 1
result = 2 +18 + 1
result = 21

The b gets added 1 to the taken directly away again right??? since it is a negative 1 then it is added to to make a 0 and then taken from to make a negative 1 again and then 1 - negative 1 is prositive 2.

The c's are 2 and 1 which make 3 right? I'm confused.

Thanks for any help.
Nice board!
hi sorry to say both are wrong as far as i know the answer i going to be 4
as
in the expression
c=2and b=0 and a=9
so (b-b)*a=0
and c+c=4;
after the expression is evaluated c=1 and b=1 and a=9;

6. > (b++ - --b)
As soon as this appeared, the whole expression (and by extension the whole program) became undefined.
http://c-faq.com/expr/index.html

All the explained "answers" given are plausible, wrong, and useless all at the same time.

If you want a consistent answer, don't invoke undefined behaviour in your code.

7. And tell us what book you're reading, so that we can warn away other newbies in the future. It seems to be one dreadful thing. (Herbert Schildt, by chance?)

8. Game Programming All In One by Bruno Miguel Teixeira de Sousa is the book and it does assume I program with microsoft Visual C++, but I use dev. Could someone suggest me a better book maybe?

I like this book so far, I am probably going to finish this one first as that was the only problem I ran into so far. It gives alot of examples dealing with basic games which are fun to program. I just programed a text based craps game, pretty cool for first program. It will eventually get into windows programming, direct x, physics engines, AI, etc. I'm sure it will not explain every detail but just give the general idea of how to do it. Anyway this is the type of thing I want to learn if there are better books to accomplish this goal?

I also want to learn how to communicate over the internet with software, for games, programs etc..

Thanks for the help guys, I understand now.

9. I don't think it's a good idea to pack everything from basic C++ up to DirectX-based graphics and AI into one book. It sounds like a receipt for disaster.

We have a sticky thread for book recommendations. Most of these books are about the language basics.

10. Originally Posted by MWalden
Could someone suggest me a better book maybe?
http://www.cprogramming.com/books.html

11. >> http://www.cprogramming.com/books.html

Note that those books are the ones recommended by the webmaster, not by the forum members. I would actually put C++ Without Fear on a not recommended list.

12. Originally Posted by MWalden
Hey I am new to programming, and I am reading a book and it has questions and answers after every section. One of them asks:
How would the number 2321 be spanned through memory? and the answer is:
1st byte 9 and 2nd byte 17
Argh. I'm sick of people writing books who have no business writing books. The proper answer to that question is "I have no idea."

13. Originally Posted by brewbuck
Argh. I'm sick of people writing books who have no business writing books. The proper answer to that question is "I have no idea."
No problem to have an idea here...

Take a calculator (Scientific view)
Enter 2321

Switch to Hex

911

9 goes to the High byte

0x11 goes to the Low Byte

Enter 11 (In Hex) switch to decimal - 17

14. Ah, but what is the high and what is the low byte?

For that matter, how do you know you have 8-bit bytes?

15. Assuming these things just causes trouble later on. You should seriously not make these assumptions. It only makes sense when doing math and examining binary, that we as humans, keep all of of the numbers in big-endian format. Computers are not humans, though.