Quick Question about int division

Hey guys I'm just wondering would this:
1
2
3
int value = 1;
value = value/2;
cout << value;


output 1 or 0? Thanks for your time.
It will output 0. Why not compile the program and see what it does?

Dividing by integer 2 is like moving the bits of the number over to the right 1 spot

1 = 01
2 = 10

1/2 = 01->0
If you want to get 1 then change expression value/2 to value%2.:)
Oh that's really smart! I never thought about it that way. And cause I'm having compiler problems at the moment, and because I'm at a place without a compiler.
Also is there a way to raise integers to a power? The pow command only takes decimals, and I'm trying to keep the output from being ugly with the .0 at the end.
You can write yourself such functon.
Ok. Thanks a bunch!
> And cause I'm having compiler problems at the moment, and because I'm at a place without a compiler.

Use this in the interim: http://liveworkspace.org/


> Also is there a way to raise integers to a power?

1
2
3
4
5
int m = 5 ;
unsigned int n = 6 ;
int m_to_the_power_n = 1 ;
for( ; n > 0 ; --n ) m_to_the_power_n *= m ; 
// watch out for overflow! 

Woah Live work space is awesome. thanks!

And I just did like vlad from moscow said. I wrote a little thing to put up at the top of the code to do it:
1
2
3
4
5
6
7
8
9
10
int Exponent(int Base, int Exponent)
{
   int ReturnValue = 1;
	while(Exponent != 0)
	{
		ReturnValue = ReturnValue*Base;
		Exponent--;
	}
	return ReturnValue;
}
Topic archived. No new replies allowed.