#a doubt regarding signed and unsigned integers

62 messages Β· Page 1 of 1 (latest)

stable hearth
#

in the function subtractNumbers(), even after the data types of x and y are defined as unsigned, why doesn't it give an unsigned output (4294967295)?
Only after writing unsigned int subtractNumbers(int x, int y) it works. Just curious why that is, even after changing unsigned int x to int x.

proven forumBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

half crown
#

The result is actually unsigned, but it is out of range of int so the return value is the int value with the same bit pattern, which is the correct result of the subtraction.

#

You can learn about two's complement, then you will know that subtracting signed integers is done the exact same way as unsigned.

stable hearth
#

oh thank you

#

does adding 'unsigned' before int main(), and calling a function inside it which is signed produce a different output?

half crown
#

What do you mean before int main()?

stable hearth
#

like this:

half crown
#

You cannot do that.

#

That wouldn't affect this thing anyway.

#

Also this is the C channel. C++ is over #1013107104678162544.

proven forumBOT
#

@stable hearth Has your question been resolved? If so, type !solved :)

warm nacelle
#

Why you even want to use unsigned?

stable hearth
#

I was curious how it would affect the output

warm nacelle
#

Ah okay, and i would recommend also to look up how numbers work in computer systems. Its really interesting πŸ™‚ pls dont forget to mark as solved.

#

Sometimes its also confusing, how numbers work xD

stable hearth
#

thank you!

#

do you have any reading reccommendation?

warm nacelle
#

I got my scripts from university but there was a video from ben eater about this topic. I will look it up and send you the url

#

Sometimes its also important how binary numbers work, and how to convert them to hex. You can convert a hex number (e.g. 0x24) in binary by just converting each number to binary -> 0010 0100

stable hearth
stable hearth
warm nacelle
#

Im on it for 2 Semesters now and sometimes i still confused. There are things like overflow and carry. With you operation you got an overflow i think

stable hearth
#

ill be joining university in a month, so I would like to master cpp before thst!

warm nacelle
#

No dont do that. Stay calm. Will be stressful enough πŸ˜‰

#

And one month is no time at all πŸ˜„

stable hearth
warm nacelle
#

Good luck with university πŸ™Œ

stable hearth
#

welp fingers crossed i get to develop a tic tac toe project by then

stable hearth
warm nacelle
#

Should be possible. But there are also classes, inhertance, polymorphism. And that takes time

#

np πŸ™‚

stable hearth
#

is this enough to achieve something akin to a mastery of the language?

warm nacelle
#

I would focus some time on debugging. Is a nice skill, if something not work

#

C++ is hard. I think only after years you will master it. The best thing is practice

#

Im doing it for a year now and still cant do alot. To design the whole structure of a program is difficult. But you will get like instructions, so you can solve the assignments

#

But its normal to struggle πŸ˜‰

stable hearth
#

one last question

#

what computer software and ide you you use?

warm nacelle
#

I changed on my desktop to linux mint. I really like it so far. As IDE i use Clion. You can use the full version with your university mail address

#

Or VSCode is also often used. But i dont like it

stable hearth
warm nacelle
#

Yeah, i dont know how this work πŸ˜‚

#

There is no free version i think of Clion

stable hearth
#

off i go to pirate it lol

#

thank you!

half crown
#

Are you on Linux?

stable hearth
#

no, mac os

warm nacelle
#

Np, just dm me for new further questions πŸ™‚

half crown
#

Then get Xcode.

stable hearth
#

yep, I already use it

half crown
#

Then keep using it.

stable hearth
#

okay..

#

!solved