c++ - 使用 bool 类型转换 : unexpected behaviour 的变量原始打印

标签 c++ boolean theory

出于好奇,我试图通过将变量指针转换为 boolean 值并将指针递增“sizeof(int) * 8”次来打印一个 int 作为原始数据,但我没有得到预期的行为.你能解释一下为什么吗?我错过了什么?

这是我的代码:

#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;
}

这是我的输出:

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

最佳答案

bool 不仅仅是 1 位类型。它的大小是 1 个字节。

关于c++ - 使用 bool 类型转换 : unexpected behaviour 的变量原始打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262377/

相关文章:

theory - 了解 Coq 中的 "well founded"证明

c++ - 为什么 std::bind1st 被认为是 "almost unusable"?

c++ - 标准库或Boost中有什么东西可以在运行时编译dll吗?

c++ - 从文件 C++ 中保存和读取双 vector

android - 如何让 Android 翻译 boolean 值?

algorithm - 关于如何在给定可变数量的数据点的情况下找到曲线方程的理论

c++ - extconf.rb:如何生成依赖于头文件更改的 makefile 目标?

带有 EditText 的 Java/Android 对话框 - 对字符串进行 boolean 检查

java - 具有输入值的二维 boolean 数组

graphics - 理论计算机图形学 - 光线转换算法