Using array, returning odd numbers

I'm rusty with arrays. I have an array: int bits [8] = { 128, 64, 32, 16, 8, 4, 2, 1 }

When I try to use a for loop to output each element, it does output all of the elements but then about 20 times outputs odd negative and large positive numbers.

Why?

1
2
3
	int bits [8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
	for (int i = 0; i < sizeof(bits); i++)
		cout << bits[i] << endl;


outputs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
128
64
32
16
8
4
2
1
-858993460
2883008
18818072
0
2883020
18755367
2882940
2882860
1181429501
0
0
2147303424
0
2882544
-858993460
30198988
-858993460
-858993460
2882544
-858993460
-858993460
30198988
-858993460
-858993460


I'm pretty sure it has something to do with the sizeof function. How can I iterate the for loop, without using i < 8 - I want it to measure the array size of bits and check i against that.
Last edited on
I think size of your array is 32 bytes (8*4bytes per integer).
So it outputs 8 elements from the array and 24 integer from beyond.
Read this article: http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/
And you can use this part to determine the size of array:
1
2
3
4
5
6
template <typename T, size_t N>
inline
size_t SizeOfArray( const T(&)[ N ] )
{
  return N;
}
You can use this

1
2
3
4
int bits [8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
int len=sizeof(bits)/sizeof(int);
for (int i = 0; i < len; i++)
     cout << bits[i] << endl;

This is good int len=sizeof(bits)/sizeof(int);

but perhaps slightly more general would be:
int len=sizeof(bits)/sizeof(bits[0]);
The latter will continue to work even if the array bits is a different datatype.
Good - The following code outputs 0000000.0000000.0000000.0000000 and not the correct thing I'm after. I'm using the below code to check if bits[i] - octetsIP[j] is negative (which in this case would mean vector octetsBits should push_back 0), but if it does subtract, and is not negative it should be a 1.

What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getNHBits(vector<int> &octetsIP, vector<int> &octetsMask){
	// Determine Binary /--
	int bits[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
	vector<int> octetsBits;
	for(int j=0; j < octetsIP.size(); j++){		// loop each of the 4 ip octets
		for(int i=0; i < 8; i++){					// while looping .. loop each 8 elements of bits array
			if (bits[i] - octetsIP[j] < 0)				// test if bits - octetsIP is a negative number ...
				octetsBits.push_back(0);				// ... if negative, push a 0 to octetsBits
			else
				octetsBits.push_back(1);				// ... else push a 1 to octetsBits
		cout << octetsBits[i];						// output octetsBits elements, as binary 0 or 1
		}											// STOP looping 8 bits array
	cout << ".";								// make binary conversion into dot notation
	}											// STOP looping 4 ip octets
	return 0;
}
Last edited on
For Diagnostics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int getNHBits(vector<int> &octetsIP, vector<int> &octetsMask){
	// Determine Binary /--
	int bits[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
	vector<int> octetsBits;
	for(int j=0; j < octetsIP.size(); j++){	// loop each of the 4 ip octets
		cout << " :" << octetsIP[j] << ": ";    // For Diagnostics //
		for(int i=0; i < 8; i++){			// while looping .. loop each 8 elements of bits array
			if (bits[i] - octetsIP[j] < 0)				// test if bits - octetsIP is a negative number ...
				octetsBits.push_back(0);			// ... if negative, push a 0 to octetsBits
			else
				octetsBits.push_back(1);			// ... else push a 1 to octetsBits
		cout << octetsBits[i];					// output octetsBits elements, as binary 0 or 1
		}										// STOP looping 8 bits array
	cout << ".";							// make binary conversion into dot notation
	}										// STOP looping 4 ip octets
	return 0;
}


Outputs:
 
:192: 00000000. :168: 00000000. :0: 00000000. :1: 00000000


For example, the eight 0's after :192: should actually be: 11000000
Last edited on
sorry please ignore.
Last edited on
So I should probably create a result and assign bits[i] - octetsIP[j] to it, and then test if result is negative or positive?
Sorry - I don't think i've accounted for the new assignment of octetsIP[j]'s new total after successful subtraction of bits, for each iteration.... I'm just having trouble figuring out with octetsBits is always 0 now. Maybe a side effect of the aforementioned

EDIT* Figured it out, I just have to account for the new totals.. i think
Last edited on
I got a bit lost in all that code. I wasn't really sure of the actual goal. Were you intending to convert each octet into a binary representation, or something different?

Yes, you got it - welcome to the backwards logic and jambalaya that is my brain

I think I know the problem I have to assign octetsIP[j] - bits[i] to a int result.

For every time I push back 1, i need to remove the value in bits[i] from result???

*EDIT - This for what im trying to do: http://www.petri.co.il/csc_convert_ip_address_from_decimal_to_binary.htm
Last edited on
You can just test the relevant bit using the bitwise & operator.
This is a trimmed down version of the code:
1
2
3
4
5
6
7
8
9
10
11
12
    for (int j=0; j < octetsIP.size(); j++)
    {
        if (j>0)
            cout << '.';

        int mask = 128;
        for (int i=0; i < 8; i++)
        {
            cout <<  ((octetsIP[j] & mask) != 0);
            mask >>= 1;
        }
    }


Note, mask >>= 1; is equivalent to mask = mask/2;
Last edited on
Thanks for making me look bad. This is a great alternative, thanks.
Last edited on
Please don't feel bad. I'm sure I borrowed this idea from someone else, so it's yours now.
So simple, to just divide mask / 2 - because each byte is just bits cubed - good - thanks! I'll be sure to (try) to remember this one.
Last edited on

Also, the inner loop can be simplified from this:
1
2
3
4
5
6
    int mask = 128;
    for (int i=0; i < 8; i++)
    {
        cout <<  ((octetsIP[j] & mask) != 0);
        mask >>= 1;
    }


to this:
1
2
3
4
5
6
    int mask = 128;
    while (mask)
    {
        cout <<  ((octetsIP[j] & mask) != 0);
        mask >>= 1;
    }


or even this:
1
2
3
    int mask = 256;
    while (mask>>=1)
        cout <<  ((octetsIP[j] & mask ) != 0);

Last edited on
Topic archived. No new replies allowed.