切换位,但不切换回来?

Wic*_*Sec 0 c bit switching

我正在搞乱一些代码并决定尝试在文件中切换一些位,然后将它们切换回来获取原始文件.不知何故,它改变了位,但不会改变它们.

这是我有的:

魔术发生在我打开文件并进入rByte [0]和rByte [1].

unsigned char rByte[] = {0, 0};

int isBitSet(unsigned char byte, int bytePosition){

    unsigned char mask[] = {128, 64, 32, 16, 8, 4, 2, 1};
    return ( (byte & mask[bytePosition]) != 0 );
}

unsigned char setBit(unsigned char byte, int pos) {
    byte |= (0x01 << pos);
    return byte;
}

unsigned char clearBit(unsigned char byte, int pos){
    byte &= ~(0x01 << pos);
    return byte;
}

/* DO NOT TOUCH */

void switchBits (unsigned char byte1, unsigned char byte2, int x, int y) {

    int reg1 = 0;
    int reg2 = 0;

    reg1 = isBitSet(byte1, x);
    reg2 = isBitSet(byte2, y);

    if ( (reg1 == reg2) ) {

    }

    if ( (reg1 == 0) && (reg2 != 0) ){
        rByte[0] = setBit(byte1, x);
        rByte[1] = clearBit(byte2, y);
    }

    if( (reg2 == 0) && (reg1 != 0) ){
        rByte[0] = clearBit(byte1, x);
        rByte[1] = setBit(byte2, y);
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,我假设如果switchBits()应用相同的方式,我认为它的方式,程序应该回到常规.我制作了一个音乐文件发出奇怪的声音,但没有恢复正常.

Kam*_*aze 5

假设它rByte是全局定义的(这不是很好).

isBitSet()setBit()和相比,你的位的位置相反clearBit().

假设示例 pos = 0

isBitSet检查MSB(最重要的位),mask[0] == 128 但在setBitclearBit移位功能0x01 << 0意味着它对应于a 1.

编辑:我建议你改变

unsigned char mask[] = {128, 64, 32, 16, 8, 4, 2, 1};
Run Code Online (Sandbox Code Playgroud)

unsigned char mask[] = {1, 2, 4, 8, 16, 32, 64, 128};
Run Code Online (Sandbox Code Playgroud)

编辑结束

你应该过度思考你的函数设计,只是链接if看起来很奇怪,你想要实现的逻辑可以改进.这里有一个简化逻辑的例子,你可能会采用另一种方式来更具体地说明逻辑,所以只需将其视为一个建议.

if ( (reg1 == reg2) ) {

}
else{
  if ( (reg1 == 0)){
    rByte[0] = setBit(byte1, x);
    rByte[1] = clearBit(byte2, y);
  }

  else{
    rByte[0] = clearBit(byte1, x);
    rByte[1] = setBit(byte2, y);
  }
}
Run Code Online (Sandbox Code Playgroud)

这将再次简化程序的另一件事是一个开关功能,只是让一个0出来的1,反之亦然.这可以通过XORoperator轻松完成^.意思是如果两者都不同,你只需要切换两个位的状态,这样就会减少你的功能.

if ( (reg1 != reg2) ) {
  rByte[0] = switchBit(byte1,x);
  rByte[1] = switchBit(byte2,y);
}
Run Code Online (Sandbox Code Playgroud)