0

hello i know there are many questions about unsigned and signed but believe me i have searched all and learned a lot but there is still one doubt which i cannot find anywhere thats why asked this question

wanna know when unsigned minus signed is calcuated one needs to be converted into same data type....now for example s.size()-2 for s.size()<2(and i know s.size() is unsigned) converts signed to unsigned like -2 will be converted into unsigned and i will get the big value got it but when s.size() is 4 i get the output as 2 that means 4-2 so here is unsigned converted into signed?? or if signed is converted to unsigned then agin -2 will be converted and this time the output will be different so i want to know what happens in both the cases and in which one it is converted.

Naman
  • 353
  • 3
  • 14
  • I think you are trying to ask why `(unsigned)1 - 2` gives an unexpected result? – John3136 Jun 21 '19 at 04:55
  • no 1-2 is clear to me -2 will be converted to unsigned right?? so now both are in unsigned ....but what happens in (unsigned)2-1...both are different which one is converted???cause 2-1 will give the correct result of 1 – Naman Jun 21 '19 at 04:58
  • or simply can you explain what happens when unsigned(1)-2 is done and what happens when unsigned(2)-1 is done which data type is converted into which one in both the cases? – Naman Jun 21 '19 at 04:59
  • @John3136 ????? – Naman Jun 21 '19 at 05:09
  • Someone has found a clear duplicate of your question with a few answers on it. They should tell you what you want to know. You may also want to consider deleting your question since it is a duplicate. – John3136 Jun 21 '19 at 05:13
  • @John3136 the duplicate is explaning why (unsigned)1-2 gives unexpected value that i already know ....-2 is converted into unsigned as 1-2 is -1 that cannot be represented in unsigned......i have read all the question and answers before posting this question dont know weather they read the whole question correctly before marking it as duplicate i am asking about what happens in unsigned(2) -1??????this isnt mentioned anywhere i want to know about the normal behaviour ...what happens in this which one is converted???? can u tell? – Naman Jun 21 '19 at 05:17
  • No - read it all again. -2 is not converted to unsigned at all, ever. The problem is "1-2" can't be represented as unsigned. – John3136 Jun 21 '19 at 05:21
  • ohh then how come the answer is so large???? obviously value of -2 in unsigned is so large and then it is added to 1..thus the answer comes so large.. – Naman Jun 21 '19 at 05:23
  • @John3136 still didnt got the answer what happens in unsigned(2) - signed(1)?? – Naman Jun 21 '19 at 05:24
  • Nothing: `unsigned(2) - signed(1)` is completely valid. – John3136 Jun 21 '19 at 06:24
  • @John3136 i know its valid thats why it gives correct answer but does any data type change??? both have to be of same data type to perform the operation so which one is converted??? – Naman Jun 21 '19 at 06:58

0 Answers0