2.63 Logic & Arithmetic Right Shift
★★★
unsigned srl(unsigned x, int k) {
/* Perform shift arithmetically */
unsigned xsra = (int)x >> k;
.
.
.
}
int sra(int x, int k) {
/* Perform shift logically */
int xsrl = (unsigned)x >> k;
.
.
.
}#include <stdio.h>
#include <assert.h>
unsigned srl(unsigned x, int k) {
/* Perform shift arithmetically */
unsigned xsra = (int)x >> k;
int w = (sizeof(int)) << 3;
int mask = ((int)-1) << (w - k);
return xsra & ~mask;
}
unsigned sra(int x, int k) {
/* Perform shift logically */
int xsrl = (unsigned)x >> k;
int w = (sizeof(int)) << 3;
int mask = ((int)-1) << (w - k);
/* Get the most significant bit unchanged, 0 for other bits */
int m = 1 << (w - 1);
/* Make mask unchanged if the most significant bit of x is 1, changed otherwise */
mask &= !(x & m) - 1;
return xsrl | mask;
}
int main() {
unsigned uval = 0x12345678; // the most significant bit is 0
int val = 0x12345678;
assert(srl(uval, 4) == uval >> 4);
assert(sra(val, 4) == val >>4);
uval = 0x87654321; // the most significant bit is 1
val = 0x87654321;
assert(srl(uval, 4) == uval >> 4);
assert(sra(val, 4) == val >>4);
return 0;
}Last updated