| View previous topic :: View next topic |
| Author |
Message |
SirSteez Newbie cheater
Reputation: 0
Joined: 10 Nov 2007 Posts: 19
|
Posted: Thu Jan 31, 2008 12:48 am Post subject: Given a challenge |
|
|
My book told me to guess what these two pieces of code are supposed to do.
What do these codes do?
Unsigned integers aren't supposed to do anything, right?
Also, can somebody explain why instead of saying unsigned short int x; and unsigned short int y; it says the following?:
| Code: | main()
{
unsigned short x;
unsigned short y;
unsigned int z;
z = x * y
} |
and the second code
| Code: | main()
{
unsigned short Width;
unsigned short Length;
unsigned short Area;
Area = Width * Length;
} |
|
|
| Back to top |
|
 |
goldengold Grandmaster Cheater Supreme
Reputation: -1
Joined: 11 Nov 2006 Posts: 1841 Location: -.-
|
Posted: Thu Jan 31, 2008 1:10 am Post subject: |
|
|
| Yea none of the integers have a falue so it doesent do anything -.- |
|
| Back to top |
|
 |
SirSteez Newbie cheater
Reputation: 0
Joined: 10 Nov 2007 Posts: 19
|
Posted: Thu Jan 31, 2008 1:13 am Post subject: |
|
|
| goldengold wrote: | | Yea none of the integers have a falue so it doesent do anything -.- |
xD alright, thanks for clearing that up.
But it does lay out a formula. Would putting int infront of unsigned value INT x;
and same w/ y effect either? |
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Thu Jan 31, 2008 4:20 am Post subject: |
|
|
| Code: | main()
{
unsigned short Width;
unsigned short Length;
unsigned short Area;
Area = Width * Length;
} |
This one calculate areas.
Width and Length are unsigned because width and height can't be negative. |
|
| Back to top |
|
 |
SirSteez Newbie cheater
Reputation: 0
Joined: 10 Nov 2007 Posts: 19
|
Posted: Thu Jan 31, 2008 5:12 pm Post subject: |
|
|
Ahh.. I see.
Wow solved problems in both of my threads.
Thanks again HolyBlah. |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Thu Jan 31, 2008 7:31 pm Post subject: |
|
|
When people use short instead of short int, it's because they're lazy. short int and short are the same. You can test it out if you like. All you have to do is put in the max value and add one. If it is signed, it will go negative and if it isn't, it will go to 0.
example
| Code: |
#include <iostream>
int main()
{
unsigned short int a = 65536;
unsigned short b = 65536;
std::cout << a << "\n" << b;
return 0;
}
|
When you run it, you will notice it will output 0 for both.
The same applies to long. long = long int. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jan 31, 2008 7:36 pm Post subject: |
|
|
| If you just use the general form "int", the compiler generally optimizes it to the lowest storage possible anyways. It all depends on what it sees you using that storage space for. |
|
| Back to top |
|
 |
SirSteez Newbie cheater
Reputation: 0
Joined: 10 Nov 2007 Posts: 19
|
Posted: Sun Feb 03, 2008 1:15 pm Post subject: |
|
|
ohhh alright.
i didn't really get that short vs. short int |
|
| Back to top |
|
 |
|