c++ - Return the signed value in certain bits range -
the assignment:
return signed value of 4 bits in bits 0-3 return signed value of 6 bits in bits 4-9respectively.
here examples:
signedbits0through3: input: 0x3fa, output: 0xfffffffa signedbits0through3: input: 0x3f7, output: 0x07 signedbits4through9: input: 0x38f, output: 0xfffffff8 signedbits4through9: input: 0x18f, output: 0x18// homecoming signed value in bits 0 through 4 int signedbits0through3(int v) { if (((v & 15) & (1<<3))== 0) { homecoming (v & 15); } else homecoming ~(v & 15); } // homecoming signed value in bits 4 through 9 int signedbits4through9(int v) { if (((v & 1008)&(1<<9))==(1<<9)) { homecoming ~((v & 1008)>>4); } else homecoming ((v & 1008)>>4); }
any help!!
it's lot simpler think. mild bit of masking, sign-extending, , shifting back.
// homecoming signed value in bits 0 through 3 int signedbits0through3(int v) { homecoming ((v & 0xf) << 28) >> 28; } // homecoming signed value in bits 4 through 9 int signedbits4through9(int v) { homecoming ((v & 0x3f0) << 22) >> 26; }
c++ binary bits
No comments:
Post a Comment