BusinessesCoding Community

Livecoding.tv Quiz of the Day: 16/11/2016

C++ Quiz

c-plus-plus-quiz-of-the-day

Answer:

The answer is NOT -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32 bit integers. Why?

In C++, if the types of two operands differ from one another, then the operand with the “lower type” will be promoted to the type of the “higher type” operand, using the following type hierarchy (listed here from highest type to lowest type): long double, double, float, unsigned long int, long int, unsigned int, int (lowest).

So when the two operands are, as in our example, 25u (unsigned int) and 50 (int), the 50 is promoted to also being an unsigned integer (i.e., 50u).

Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.

If you want to explore more, visit our C++ edu & tutorials section! Below are some examples:

Writing a Memory Scanner in C++

Making a Filesystem in C++

About author

About Author Dr. Michael Jurgen Garbade is the founder of LiveEdu.TV, Kyuda, Education Ecosystem. He is future Venture Capitalist, Future Politician and always on the lookout for the Next Big Challenge. Obtained Masters in business administration and physics, and a Ph.D. in finance with professional work experience in high-paced environments at Fortune 500 companies like Amazon and General Electric. Expertize: Python, PHP, Sencha Touch & C++, SEO, Finance, Strategy & E-commerce. He speaks English and German and has worked in the US, Europe, and Asia. At Education Ecosystem he is the CEO and runs business operations.
Related posts
Career DevelopmentCoding Community

AWS CodeCommit - An Alternative Version Control to GitHub

Career DevelopmentCoding Community

AWS Codestar - A great way to launch and deploy your Project using only AWS Tools

Coding Community

How Whelan Became a Vue.JS Expert through Hands-on Projects on Education Ecosystem during the Pandemic

Career DevelopmentCoding Community

10 Python Projects to Learn from Home During Quarantine