Q:

Write a program to check whether your predictions were correct. If not, study this section until you understand what the problem is

0

Write a program to check whether your predictions were correct. If not, study this section until you understand what the problem is.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

int main() {
  unsigned u = 10, u2 = 42;
  std::cout << u2 - u << std::endl;  // 32
  std::cout << u - u2 << std::endl;  // 4294967264 = -32 + 4,294,967,296

  int i = 10, i2 = 42;
  std::cout << i2 - i << std::endl;  // 32
  std::cout << i - i2 << std::endl;  // -32
  std::cout << i - u << std::endl;   // 0
  std::cout << u - i << std::endl;   // 0

  // Mix unsigned with signed.
  std::cout << i - u2 << std::endl;  // 4294967264 = -32 + 4,294,967,296
  std::cout << u2 - i << std::endl;  // 32
  std::cout << i2 - u << std::endl;  // 32
  std::cout << u - i2 << std::endl;  // 4294967264 = -32 + 4,294,967,296

  int i3 = -10;
  std::cout << i3 + u << std::endl;  // 0 = -10 + 4,294,967,296 + 10 - 4,294,967,296
  std::cout << i3 - u << std::endl;  // 4294967276 = -10 + 4,294,967,296 - 10

  return 0;
}

 

Output:

32
4294967264
32
-32
0
0
4294967264
32
32
4294967264
0
4294967276

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now