Domanda

I'm trying to print an int as raw data, just for curiosity, by casting the variable pointer to a bool one and incrementing the pointer "sizeof(int) * 8" times but I don't get the expected behaviour. can you explain me why? what am I missing?

this is my code:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int integer = 0;
    int *puntInt = 0;
    bool *puntBool = 0;
    puntInt = &integer;
    for (int j = 0; j < 100; j++)
    {
        integer = j;
        puntBool = (bool*) puntInt;
        printf("%02d => ", integer);
        for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
        {
            printf("%d", (*puntBool ? 1 : 0));
            puntBool++;
        }
        printf("\n");
    }
   getchar();
   return 0;
}

and this is my output:

00 => 00000000100011111100111111000000
01 => 10001000100011111100111111000000
02 => 10001000100011111100111111000000
03 => 10001000100011111100111111000000
04 => 10001000100011111100111111000000
05 => 10001000100011111100111111000000
06 => 10001000100011111100111111000000
07 => 10001000100011111100111111000000
08 => 10001000100011111100111111000000
09 => 10001000100011111100111111000000
10 => 10001000100011111100111111000000
11 => 10001000100011111100111111000000
12 => 10001000100011111100111111000000
13 => 10001000100011111100111111000000
14 => 10001000100011111100111111000000
15 => 10001000100011111100111111000000
16 => 10001000100011111100111111000000
17 => 10001000100011111100111111000000
18 => 10001000100011111100111111000000
19 => 10001000100011111100111111000000
20 => 10001000100011111100111111000000
21 => 10001000100011111100111111000000
22 => 10001000100011111100111111000000
23 => 10001000100011111100111111000000
24 => 10001000100011111100111111000000
25 => 10001000100011111100111111000000
26 => 10001000100011111100111111000000
27 => 10001000100011111100111111000000
28 => 10001000100011111100111111000000
29 => 10001000100011111100111111000000
30 => 10001000100011111100111111000000
31 => 10001000100011111100111111000000
32 => 10001000100011111100111111000000
33 => 10001000100011111100111111000000
34 => 10001000100011111100111111000000
35 => 10001000100011111100111111000000
36 => 10001000100011111100111111000000
37 => 10001000100011111100111111000000
38 => 10001000100011111100111111000000
39 => 10001000100011111100111111000000
40 => 10001000100011111100111111000000
41 => 10001000100011111100111111000000
42 => 10001000100011111100111111000000
43 => 10001000100011111100111111000000
44 => 10001000100011111100111111000000
45 => 10001000100011111100111111000000
46 => 10001000100011111100111111000000
47 => 10001000100011111100111111000000
48 => 10001000100011111100111111000000
49 => 10001000100011111100111111000000
50 => 10001000100011111100111111000000
51 => 10001000100011111100111111000000
52 => 10001000100011111100111111000000
53 => 10001000100011111100111111000000
54 => 10001000100011111100111111000000
55 => 10001000100011111100111111000000
56 => 10001000100011111100111111000000
57 => 10001000100011111100111111000000
58 => 10001000100011111100111111000000
59 => 10001000100011111100111111000000
60 => 10001000100011111100111111000000
61 => 10001000100011111100111111000000
62 => 10001000100011111100111111000000
63 => 10001000100011111100111111000000
64 => 10001000100011111100111111000000
65 => 10001000100011111100111111000000
66 => 10001000100011111100111111000000
67 => 10001000100011111100111111000000
68 => 10001000100011111100111111000000
69 => 10001000100011111100111111000000
70 => 10001000100011111100111111000000
71 => 10001000100011111100111111000000
72 => 10001000100011111100111111000000
73 => 10001000100011111100111111000000
74 => 10001000100011111100111111000000
75 => 10001000100011111100111111000000
76 => 10001000100011111100111111000000
77 => 10001000100011111100111111000000
78 => 10001000100011111100111111000000
79 => 10001000100011111100111111000000
80 => 10001000100011111100111111000000
81 => 10001000100011111100111111000000
82 => 10001000100011111100111111000000
83 => 10001000100011111100111111000000
84 => 10001000100011111100111111000000
85 => 10001000100011111100111111000000
86 => 10001000100011111100111111000000
87 => 10001000100011111100111111000000
88 => 10001000100011111100111111000000
89 => 10001000100011111100111111000000
90 => 10001000100011111100111111000000
91 => 10001000100011111100111111000000
92 => 10001000100011111100111111000000
93 => 10001000100011111100111111000000
94 => 10001000100011111100111111000000
95 => 10001000100011111100111111000000
96 => 10001000100011111100111111000000
97 => 10001000100011111100111111000000
98 => 10001000100011111100111111000000
99 => 10001000100011111100111111000000
È stato utile?

Soluzione 2

A char is the smallest addressable unit, you can't go any lower than that in C/C++.
Printing the value that puntBool holds:

for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
{
        //printf("%d", (*puntBool ? 1 : 0));
        printf("%p\n",puntBool++);

}

gives me:

0xbfbb7d80
0xbfbb7d81
0xbfbb7d82
0xbfbb7d83
0xbfbb7d84
0xbfbb7d85
0xbfbb7d86
0xbfbb7d87
0xbfbb7d88
0xbfbb7d89
0xbfbb7d8a
0xbfbb7d8b

So, you bool* is esentially acting as a char* and that's why your result is not as expected.

Altri suggerimenti

bool is not just 1 bit type. Its size is 1 byte.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top