简体   繁体   English

使用按位运算符的模式

[英]Pattern with bitwise operators

How do I set the bits of a variable to assume the pattern I want? 如何设置变量的位以采用我想要的模式? For exemple, if I have to print this sequence, how do i proceed? 例如,如果我必须打印此序列,我该如何处理?

11010010 11010010 11010010 11010010 

I wrote the code that print and separate bits in this configuration, but don't know how to set them as I want. 我编写了在此配置中打印和分隔位的代码,但不知道如何根据需要设置它们。

#include <stdio.h>
#include <limits.h>
int a;
void stampabit(a);
int main()
{
    int i;
    int n= sizeof(int) * CHAR_BIT;
    int mask = 1 << (n-1);

    for (i=1; i<=n; ++i){
        putchar(((a & mask)==0)?'0':'1');
        a<<=1;
        if(i%CHAR_BIT==0 && i<n)
            putchar(' ');
    }
}

You must shift mask instead of shift the variable 您必须移动蒙版而不是移动变量

#include <stdio.h>
#include <limits.h>
unsigned int a = 0xAA55AA55;

int main()
{
    size_t i;
    unsigned int  n= sizeof(int) * CHAR_BIT;
    unsigned int  mask = 1 << (n-1);

    for (i=1; i<=n; ++i){
        putchar(((a & mask)==0)?'0':'1');
        mask>>=1;
        if(i%CHAR_BIT==0 && i<n)
            putchar(' ');
    }
    putchar('\n');
}

Output will be 输出将是

10101010 01010101 10101010 01010101

Changing value of a to 0xD2D2D2D2 as you want output will be 根据需要将a值更改为0xD2D2D2D2将是

11010010 11010010 11010010 11010010 

Edit: 编辑:
if you need just set some bits, simply use bitwise or, 如果你只需要设置一些位,只需使用按位或,
and if you need to clear some bits, use bitwise and: 如果你需要清除一些位,请使用按位和:

uint32_t   a=1;
a |= 0xD2D2D2D2 ; //set bits to 11010010 11010010 11010010 11010010
a &= ~1; // clear first bit (or mask) 

and it is good to separate display code from main code to have clear code: 将显示代码与主代码分开以获得清晰的代码是很好的:

#include <stdio.h> 
#include <stdint.h>

void print_bits(uint32_t u){
    int i=31;
    do{
        if (u & 0x80000000) putchar('1'); else putchar('0');
        u <<= 1;
        if(i%8 == 0) putchar(' ');
    }while( --i >= 0);  
} 

int main(){  
    uint32_t   a=1;
    a |= 0xD2D2D2D2 ; //set bits to 11010010 11010010 11010010 11010010
    a &= ~1; // clear first bit (or mask) 
    print_bits(a); //output: 11010010 11010010 11010010 11010010
}

Old: 旧:
if you need just set some bits, simply use bitwise or like this: 如果您只需要设置一些位,只需使用按位或如下:

void stampabit(int setBits){
    a |= setBits;
}

to set your pattern 11010010 11010010 11010010 11010010 use this: 设置您的模式11010010 11010010 11010010 11010010使用此:

stampabit(0xD2D2D2D2 ); // stampabit(0b11010010110100101101001011010010);

working example: 工作实例:

#include <stdio.h>
#include <limits.h>
//#include <stdint.h>
int a=0;
void stampabit(int setBits){
    a |= setBits;
}
int main()
{
    int i;
    stampabit(0xD2D2D2D2 ); // stampabit(0b11010010110100101101001011010010);
    int n= sizeof(int) * CHAR_BIT; // number of bits in int (8, 16, 32, 64, ... bits)
    int mask = 1 << (n-1); // sign bit mask

    for (i=1; i<=n; ++i){
        putchar(((a & mask)==0)?'0':'1');
        a<<=1;
        if(i%CHAR_BIT==0 && i<n)
            putchar(' ');
    }
}

output: 输出:

11010010 11010010 11010010 11010010

Some notes: 一些说明:
here it is just sample code and it works fine, but using unsigned types is clearly shows you don't need sign bit, and it is bug proof. 这里只是示例代码,它工作正常,但使用无符号类型清楚地表明您不需要符号位,并且它是防错的。
using local variables or shifting local variables is better than using or shifting global variables, unless it is intentional. 使用局部变量或移动局部变量比使用或移动全局变量更好,除非是故意的。

last but not least if you don't need platform dependent int use int32_t or uint32_t from 最后但并非最不重要的,如果你不需要平台依赖的int使用int32_t或uint32_t

#include <stdint.h>

and if you need to clear some bits, use bitwise and: 如果你需要清除一些位,请使用按位和:

uint32_t   a=1;
a |= 0xD2D2D2D2 ; //set bits to 11010010 11010010 11010010 11010010
a &= ~1; // clear first bit (or mask)

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM