binary - Bit Selection in C -
i trying select bits [0:2] , bits [6:8] of bit-string 1010000000001. bits [0:2] 001 , bits [6:8] 000. tried select these bits with:
int instr = 0x1401; int src2 = (instr & 0x0006); //get bits [2:0] int src1 = (instr & 0x01c0) >> 6; //get bits [6:8] printf("%04x, %04x",src2, src1);
however getting src1 , src2 both 0000. can please me understand doing incorrectly can select bits [0:2] , [6:8]?
look @ code:
#include <stdio.h> int main (void) { unsigned instr = 0x1401; unsigned src2 = instr & 0x0007; // 7 in hex == 0000 0000 0111 in binary unsigned src1 = (instr & 0x01c) >> 6; // 1c in hex == 0001 1100 0000 in binary printf("%04x, %04x", src2, src1); }
it masks out desired bits in instr
, shifts them correct offset. also, when doing bit manipulation, unsigned types preferred.
Comments
Post a Comment