Set a particular bits in a byte

This question already has an answer here:

  • How do you set, clear, and toggle a single bit? 26 answers

  • This should help http://en.wikipedia.org/wiki/Bitwise_operations_in_C

    If you need working example let me know but i encourage you to figure it out on your own.


    unsigned char my_byte = 0x3B; // 0b00111011
    
    // clear the bits
    my_byte &= 0xE3;
    
    // set the bits
    my_byte |= 0x14;
    

    You'll find many people have many different preferences on how to write the 0xE3 and 0x14. Some like to shift bits, however ultimately this is the code that should be produced.

    链接地址: http://www.djcxy.com/p/28798.html

    上一篇: 将指针的最低有效位设置为0

    下一篇: 在一个字节中设置一个特定的位