无法到达 c 中 char 的第一位

can not reach first bit of char in c

我正在尝试使用以下代码访问 char 的第一位。

#include <stdio.h>

int main(int argc, char **argv){

        char *a = argv[1];
        printf("%d ", (128 & (a[0]))>>7);
        putc('\n',stdout);
        return 0;
}

但是对于任何输入它都是输出 0。我的按位运算符有什么问题?


在C语言中,char的大小为1个字节,表示其表示范围为0255的无符号十进制值。

一旦第一位通常是最重要的,对于输入介于 128255 之间的值,您的程序的结果应该是 1

为了测试操作(128 & (a[0]))>>7)是否正确,我构建了另一个程序在0255之间迭代并在stdout中打印这个结果应用于每个值的操作。

这是源代码。

/* program.c */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    for (unsigned int i = 0; i < 256; i++) {
        printf("Code: %d; First bit: %d\n", i, (128 & i) >> 7);
    }
    return EXIT_SUCCESS;
}

这是结果。

$ gcc -Wall -std=c99 -o program program.c
$ ./program
Code: 0; First bit: 0
Code: 1; First bit: 0
Code: 2; First bit: 0
Code: 3; First bit: 0
Code: 4; First bit: 0
Code: 5; First bit: 0
Code: 6; First bit: 0
Code: 7; First bit: 0
Code: 8; First bit: 0
Code: 9; First bit: 0
Code: 10; First bit: 0
Code: 11; First bit: 0
Code: 12; First bit: 0
Code: 13; First bit: 0
Code: 14; First bit: 0
Code: 15; First bit: 0
Code: 16; First bit: 0
Code: 17; First bit: 0
Code: 18; First bit: 0
Code: 19; First bit: 0
Code: 20; First bit: 0
Code: 21; First bit: 0
Code: 22; First bit: 0
Code: 23; First bit: 0
Code: 24; First bit: 0
Code: 25; First bit: 0
Code: 26; First bit: 0
Code: 27; First bit: 0
Code: 28; First bit: 0
Code: 29; First bit: 0
Code: 30; First bit: 0
Code: 31; First bit: 0
Code: 32; First bit: 0
Code: 33; First bit: 0
Code: 34; First bit: 0
Code: 35; First bit: 0
Code: 36; First bit: 0
Code: 37; First bit: 0
Code: 38; First bit: 0
Code: 39; First bit: 0
Code: 40; First bit: 0
Code: 41; First bit: 0
Code: 42; First bit: 0
Code: 43; First bit: 0
Code: 44; First bit: 0
Code: 45; First bit: 0
Code: 46; First bit: 0
Code: 47; First bit: 0
Code: 48; First bit: 0
Code: 49; First bit: 0
Code: 50; First bit: 0
Code: 51; First bit: 0
Code: 52; First bit: 0
Code: 53; First bit: 0
Code: 54; First bit: 0
Code: 55; First bit: 0
Code: 56; First bit: 0
Code: 57; First bit: 0
Code: 58; First bit: 0
Code: 59; First bit: 0
Code: 60; First bit: 0
Code: 61; First bit: 0
Code: 62; First bit: 0
Code: 63; First bit: 0
Code: 64; First bit: 0
Code: 65; First bit: 0
Code: 66; First bit: 0
Code: 67; First bit: 0
Code: 68; First bit: 0
Code: 69; First bit: 0
Code: 70; First bit: 0
Code: 71; First bit: 0
Code: 72; First bit: 0
Code: 73; First bit: 0
Code: 74; First bit: 0
Code: 75; First bit: 0
Code: 76; First bit: 0
Code: 77; First bit: 0
Code: 78; First bit: 0
Code: 79; First bit: 0
Code: 80; First bit: 0
Code: 81; First bit: 0
Code: 82; First bit: 0
Code: 83; First bit: 0
Code: 84; First bit: 0
Code: 85; First bit: 0
Code: 86; First bit: 0
Code: 87; First bit: 0
Code: 88; First bit: 0
Code: 89; First bit: 0
Code: 90; First bit: 0
Code: 91; First bit: 0
Code: 92; First bit: 0
Code: 93; First bit: 0
Code: 94; First bit: 0
Code: 95; First bit: 0
Code: 96; First bit: 0
Code: 97; First bit: 0
Code: 98; First bit: 0
Code: 99; First bit: 0
Code: 100; First bit: 0
Code: 101; First bit: 0
Code: 102; First bit: 0
Code: 103; First bit: 0
Code: 104; First bit: 0
Code: 105; First bit: 0
Code: 106; First bit: 0
Code: 107; First bit: 0
Code: 108; First bit: 0
Code: 109; First bit: 0
Code: 110; First bit: 0
Code: 111; First bit: 0
Code: 112; First bit: 0
Code: 113; First bit: 0
Code: 114; First bit: 0
Code: 115; First bit: 0
Code: 116; First bit: 0
Code: 117; First bit: 0
Code: 118; First bit: 0
Code: 119; First bit: 0
Code: 120; First bit: 0
Code: 121; First bit: 0
Code: 122; First bit: 0
Code: 123; First bit: 0
Code: 124; First bit: 0
Code: 125; First bit: 0
Code: 126; First bit: 0
Code: 127; First bit: 0
Code: 128; First bit: 1
Code: 129; First bit: 1
Code: 130; First bit: 1
Code: 131; First bit: 1
Code: 132; First bit: 1
Code: 133; First bit: 1
Code: 134; First bit: 1
Code: 135; First bit: 1
Code: 136; First bit: 1
Code: 137; First bit: 1
Code: 138; First bit: 1
Code: 139; First bit: 1
Code: 140; First bit: 1
Code: 141; First bit: 1
Code: 142; First bit: 1
Code: 143; First bit: 1
Code: 144; First bit: 1
Code: 145; First bit: 1
Code: 146; First bit: 1
Code: 147; First bit: 1
Code: 148; First bit: 1
Code: 149; First bit: 1
Code: 150; First bit: 1
Code: 151; First bit: 1
Code: 152; First bit: 1
Code: 153; First bit: 1
Code: 154; First bit: 1
Code: 155; First bit: 1
Code: 156; First bit: 1
Code: 157; First bit: 1
Code: 158; First bit: 1
Code: 159; First bit: 1
Code: 160; First bit: 1
Code: 161; First bit: 1
Code: 162; First bit: 1
Code: 163; First bit: 1
Code: 164; First bit: 1
Code: 165; First bit: 1
Code: 166; First bit: 1
Code: 167; First bit: 1
Code: 168; First bit: 1
Code: 169; First bit: 1
Code: 170; First bit: 1
Code: 171; First bit: 1
Code: 172; First bit: 1
Code: 173; First bit: 1
Code: 174; First bit: 1
Code: 175; First bit: 1
Code: 176; First bit: 1
Code: 177; First bit: 1
Code: 178; First bit: 1
Code: 179; First bit: 1
Code: 180; First bit: 1
Code: 181; First bit: 1
Code: 182; First bit: 1
Code: 183; First bit: 1
Code: 184; First bit: 1
Code: 185; First bit: 1
Code: 186; First bit: 1
Code: 187; First bit: 1
Code: 188; First bit: 1
Code: 189; First bit: 1
Code: 190; First bit: 1
Code: 191; First bit: 1
Code: 192; First bit: 1
Code: 193; First bit: 1
Code: 194; First bit: 1
Code: 195; First bit: 1
Code: 196; First bit: 1
Code: 197; First bit: 1
Code: 198; First bit: 1
Code: 199; First bit: 1
Code: 200; First bit: 1
Code: 201; First bit: 1
Code: 202; First bit: 1
Code: 203; First bit: 1
Code: 204; First bit: 1
Code: 205; First bit: 1
Code: 206; First bit: 1
Code: 207; First bit: 1
Code: 208; First bit: 1
Code: 209; First bit: 1
Code: 210; First bit: 1
Code: 211; First bit: 1
Code: 212; First bit: 1
Code: 213; First bit: 1
Code: 214; First bit: 1
Code: 215; First bit: 1
Code: 216; First bit: 1
Code: 217; First bit: 1
Code: 218; First bit: 1
Code: 219; First bit: 1
Code: 220; First bit: 1
Code: 221; First bit: 1
Code: 222; First bit: 1
Code: 223; First bit: 1
Code: 224; First bit: 1
Code: 225; First bit: 1
Code: 226; First bit: 1
Code: 227; First bit: 1
Code: 228; First bit: 1
Code: 229; First bit: 1
Code: 230; First bit: 1
Code: 231; First bit: 1
Code: 232; First bit: 1
Code: 233; First bit: 1
Code: 234; First bit: 1
Code: 235; First bit: 1
Code: 236; First bit: 1
Code: 237; First bit: 1
Code: 238; First bit: 1
Code: 239; First bit: 1
Code: 240; First bit: 1
Code: 241; First bit: 1
Code: 242; First bit: 1
Code: 243; First bit: 1
Code: 244; First bit: 1
Code: 245; First bit: 1
Code: 246; First bit: 1
Code: 247; First bit: 1
Code: 248; First bit: 1
Code: 249; First bit: 1
Code: 250; First bit: 1
Code: 251; First bit: 1
Code: 252; First bit: 1
Code: 253; First bit: 1
Code: 254; First bit: 1
Code: 255; First bit: 1
$

正如预期的那样,结果是 00127 之间,1128255 之间。