binary conversion

Can someone check if my functions are working correctly. I believe they do but i am not sure so someone just tell me if i did something wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void binary(int input,int arr[]) { //makes binary numbers from decimal into an array
	double i;
	for (i=7;i>=0;i--) {
		if (input-pow(2.0,i)>=0) {
			arr[static_cast<int>(i)]=1;
			input-=pow(2.0,i);
		} else {
			arr[static_cast<int>(i)]=0;
		}
	}
}

int invbinary(int arr[]) { // makes decimal from binary array
	double i;
	int output=0;
	for (i=7;i>=0;i--) {
		if (arr[static_cast<int>(i)]==1) {
			output+=pow(2.0,i);
		} 
	}
	return output;
}
I just had a very quick look.
I was a little bit surprised, as I expected the array to be characters (that is a c-string) rather than an array of integers. But that's not important.

One comment pow(2.0,i) is slightly overworked, since a simple bit shift will do the same thing faster and more simply: 1<<i. That requires i to be an int rather than a double, but that's no bad thing either.
Topic archived. No new replies allowed.