Return negative one...

A place where newbies can post without (much) fear of reprisal. All mission posts should still go in the applicable forum.
Forum rules
Older HTS users: Be nice to the new people.

NEW USERS: This is NOT the place to post about missions! Refer to "Missions" category.

Return negative one...

Post by Misanthropydotexe on Thu Jun 26, 2014 9:02 pm
([msg=81794]see Return negative one...[/msg])

Okay, so I've got my Code::Blocks going and I'm working through the c++ book. First thing we do is...
Code: Select all
int main()
{
     return 0;
}


Easy enough. I run it and it returns, as expected, 0.

At the end of the section it wants you to have it return negative one. As stupidly simple as this is, I do it anyway.

Code: Select all
int main()
{
     return -1;
}


I admit I expected it to return -1.
It returned 255.

The book then goes on to a completely different topic and fails to let me know this is supposed to happen, and explain why. Any idea why -1=255 in computer speak?
My armor is Contempt. My shield is Disgust. My sword is Hatred.
Respect not pity or weakness, for they are a disease which makes sick the strong
User avatar
Misanthropydotexe
Poster
Poster
 
Posts: 135
Joined: Tue Jun 03, 2014 10:54 pm
Blog: View Blog (0)


Re: Return negative one...

Post by tremor77 on Thu Jun 26, 2014 9:48 pm
([msg=81795]see Re: Return negative one...[/msg])

Because it doesn't know what -1 is at this point and is returning an error code, learn about signed and unsigned ints. Assuming the call to main is just a printf to print the return value..

try something like unsigned int x = abs(-1); return x;

pardon my C++ is a bit rusty.. abs might make it return just "1". But ya, signed vs. unsigned int is the cause of the problem.
Image
User avatar
tremor77
Contributor
Contributor
 
Posts: 913
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: Return negative one...

Post by Goatboy on Thu Jun 26, 2014 10:47 pm
([msg=81798]see Re: Return negative one...[/msg])

Expanding a bit on what trem said:

A single 8-bit integer (one byte) can hold 256 distinct values: 0 to 255. Well if you only have to worry about positive numbers you're fine. But what about negatives? Negatives are expressed by using a signed-type integer. Basically you take the left-most bit (the most significant) and make it a zero for positives and a one for negatives. That means you are cutting half the positive numbers you can use out and making them negative. Not exactly half though, because 0 is neither negative nor positive yet still needs to be represented, so you lose one of the positive numbers.

So basically you have 0, 1, 2, 3, [label start]4, all those other numbers we love so much, 125, 126, 127... Let's stop there. 127 is a good number. Strong, confident, like a goat in a tower. It's the start of the loopback IP address (127.0.0.1). It's a Mersenne prime, a palindromic binary number, the number of hours some jackass was stuck underneath a boulder, and a lot of other things. 127 is also about half of 256. So let's do some math. We have 256 total numbers we can represent. One of those is set aside for zero, leaving us with 255. 127 of those are for positive numbers, so that leaves 128 left over. Now let's see...

Aha! Let's take those remaining 128 numbers and make them negative! Fuck da police! Back to counting:

124, 125, 126, 127... *deep breath* Negative one-hundred-fucking-twenty-eight. -127. -126. This is feeling dirty but I like it. -125. -124. Oh yea baby. -123. -122... -3, -2, -1... 0. 1. 2. 3. [goto start].

*ahem* So. That's if you are signed. After 127 comes -128. But if you are unsigned, positive 128 follows positive 127. So why is -1 equal to positive 255 in your program? I don't know. You figure it out.

Obligatory StackOverflow post: https://stackoverflow.com/questions/114 ... nd-not-fro
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2825
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Return negative one...

Post by Misanthropydotexe on Thu Jun 26, 2014 11:01 pm
([msg=81799]see Re: Return negative one...[/msg])

Lol, I'm afraid I understood very little of that. However, I did understand that I need to research signed and unsigned ints. So, I will be doing that. Thanks a ton!

Also, love the display pic. My American Bulldog is currently laying all over me while I try to type. Here's the new harness I'm getting him! http://www.pitbull-store.com/index.php? ... cts_id=551

Edit based on Goatboy getting his answer in as I was trying to post:
Even if you removed all of the facts from that post it still would have been magnificent. Like a fucking goat in a fucking tower. Also, I think your answer was more palatable to me than what I would have gotten if I had just researched signed and unsigned by myself. It was very clear and noob friendly. Although your knowledge of the locations of bizarre goat pictures and numerical trivia make me wonder if you're slightly mad.

Thanks a ton for all of the help, guys! I also have realized that my confusion came from an asinine problem in my brain. You see, my brain was thinking "return" was equivalent to "printf" (yeah, I'm really that ignorant of c++ at this point, and I remember that "printf" prints things, and I saw typing "return 0" printed "0"). I'll also be researching what exactly "return" does.
My armor is Contempt. My shield is Disgust. My sword is Hatred.
Respect not pity or weakness, for they are a disease which makes sick the strong
User avatar
Misanthropydotexe
Poster
Poster
 
Posts: 135
Joined: Tue Jun 03, 2014 10:54 pm
Blog: View Blog (0)



Return to NZone

Who is online

Users browsing this forum: No registered users and 0 guests