Assuming a 17-bit integer, does the following statement result in overflow? Explain your answer.

int x = 50210;

how would i solve this or find an answer to this?

This is a discussion on *help* within the **C Programming** forums, part of the General Programming Boards category; Assuming a 17-bit integer, does the following statement result in overflow? Explain your answer.
int x = 50210;
how would ...

- 03-15-2009 #1

- Join Date
- Mar 2009
- Posts
- 32

## help

Assuming a 17-bit integer, does the following statement result in overflow? Explain your answer.

int x = 50210;

how would i solve this or find an answer to this?

- 03-15-2009 #2

- Join Date
- Jul 2005
- Posts
- 21

While x is greater than 0, you'd divide x by 2 (assigning the result back to x), the number of times it does this division is equal to the number of bits required to represent the number.

Of course you'd need to be aware that diving an odd number by 2 would result in a rational number (floating point), but with integer division being the case it will be rounded down anyway.

- 03-16-2009 #3

- Join Date
- Mar 2009
- Posts
- 31

When counting in binary there are simple ways to check if the storage-variable could hold the value you would like to assign it.

First of all you should read-up with the binary numeral system, when done with that you should understand how everything works - but read ahead if you not fully understand.

The maximum value a**unsigned**variabel with n bytes can hold is "(2^n)-1".

if you decrement this value by 1 and then divide it by 2 you will get the +/- max/min of the same variable, but**signed**. This is called "two complement"

The maximum unsigned value for a variable with 17 bits is: (2^17)-1 = 131 071

The minimum signed value for a variable with 17 bits: 0.5 * ((2^17)-1-1) * -1 = -65 535

The maximum signed value for a variable with 17 bits is: 0.5 * ((2^17)-1-1) = 65 535

Answer: assigning 50 210 to a (signed) integer would**not**result in an overrun/overflow.

- 03-16-2009 #4

- Join Date
- Sep 2008
- Location
- Toronto, Canada
- Posts
- 1,831

The minimum signed value for a variable with 17 bits: -65 536

- 03-16-2009 #5

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011