I saw a some solutions but its look complex.
What are the most effective way to swap between two bits in n,m postions?
int swapBits(int num, int nPostion, int mPosition);
Given integer n in which we wants to swap bit at location p1 and p2 :
Algorithm : if both bits are same then just return same value else toggle both bits using XOR.
unsigned int swapBits(unsigned int n, unsigned int p1, unsigned int p2)
{
return (((n >> p1) & 1) == ((n >> p2) & 1) ? n : ((n ^ (1 << p2)) ^ (1 << p1)));
}
Not sure it is the most effective, but I think this is a rather simple solution:
int bitValue(int num, int nPosition)
{
return ( num >> nPosition ) % 2;
}
int swapBits(int num, int nPosition, int mPosition)
{
int nVal = bitValue(num, nPosition);
int mVal = bitValue(num, mPosition);
if (nVal != mVal)
{
if (1 == nVal)
{
num -= 1<<nPosition;
num += 1<<mPosition;
}
else
{
num += 1<<nPosition;
num -= 1<<mPosition;
}
}
return num;
}
Same solution in a more efficient (but less readable) way:
int swapBits2(int num, int nPosition, int mPosition)
{
int nVal = ( num >> nPosition ) % 2;
int mVal = ( num >> mPosition ) % 2;
if (nVal != mVal)
{
num += (-1)*(2*mVal-1)*(1<<mPosition) + (-1)*(2*nVal-1)*(1<<nPosition);
}
return num;
}
and last:
int swapBits3(int num, int nPosition, int mPosition)
{
int k = ((num >> nPosition) & 1) - (num >> mPosition) & 1;
return num + k*(1<<mPosition) - k*(1<<nPosition);
}
Parth Bera's answer contains a branch, but the xor-idea is correct.
Assume the bits of p are ????A????B???. To turn A into B and B into A, we need to xor them with (A^B). For convenience, let X=A^B
????A????B???
0000X0000X000 ^
=============
????B????A???
How do we generate 0000X0000X000 ?
????A????B??? >> (nPostion-mPostion)
?????????A??? ^
?????????X??? & (1<<mPosition)
000000000X000 << (nPostion-mPostion)
0000X00000000 +
0000X0000X000 ^
????A????B??? ==
????B????A???
You can use the following macro to avoid temporary variables or a stack allocation, and it will work with any numeric type:
#define SWAP_BITS(v,b1,b2) \
(((v)>>(b1)&1)==((v)>>(b2)&1)?(v):(v^(1ULL<<(b1))^(1ULL<<(b2))))
Building on Shay Gold's solution, here is one with a few bug fixes:
unsigned int swapBits(unsigned int num, int nPosition, int mPosition) {
unsigned int k = ((num >> nPosition) & 1) - ((num >> mPosition) & 1);
return num + k * ((1U << mPosition) - (1U << nPosition));
}
unsigned int swapbits(unsigned int num, unsigned int pos1, unsigned int pos2) {
unsigned int bits_of_interest_mask = (1 << pos1) | (1 << pos2);
unsigned int bits_of_interest = num & bits_of_interest_mask;
unsigned int null_factor = ((bits_of_interest != 0) & (bits_of_interest != bits_of_interest_mask));
unsigned int xor_mask = null_factor * bits_of_interest_mask;
return num ^ xor_mask;
}
(Compilers remove the multiplication by the boolean: https://godbolt.org/z/a4z3jnh7c, https://godbolt.org/z/aM3TK7bq1)
Related
I can rotate a word to left or right by a certain amount like this:
#define ROR(x, r) ((x >> r) | (x << (64 - r)))
#define ROL(x, r) ((x << r) | (x >> (64 - r)))
[...]
ROR(var1, 11);
ROL(var1, 11);
How can I do the same but with an entire array of bytes (I mean: all the bits in array sequence)? An array like this:
uint32_t somearray[12] = {
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
};
PS: There is a similar question here, but I need to know how to do it with some amount.
There are 2 problems with the ROR and ROL macros:
the macro assumes that the argument x is a uint64_t.
shifting a uint64_t by 64 positions has undefined behavior. If the number of shifts r can be null, you should modify the expression to avoid shifting by 64.
the macro arguments should be parenthesized in the expansion to avoid precedence issues.
Here is a modified version:
// assuming x is a uint64_t and r in the range 0..63
#define ROR64(x, n) (((x) >> (n)) | ((x) << (63 - (n)) << 1))
#define ROL64(x, n) (((x) << (n)) | ((x) >> (63 - (n)) >> 1))
Note that it is recommended to use inline functions instead of macros to avoid problems with operands with side effects and enforce operand sizes:
// assuming r in the range 0..63
static inline uint64_t ror64(uint64_t x, int n) {
return (x >> n) | (x << (63 - n) << 1);
}
static inline uint64_t rol64(uint64_t x, int n) {
return (x << n) | (x >> (63 - n) >> 1);
}
To rotate a full array of words, it is simpler to use a different array as the source and destination and write a loop:
void ror32_array(uint32_t *dst, const uint32_t *src, size_t size, size_t n) {
size_t dist = n / 32 % size;
int shift = n % 32;
for (size_t i = 0; i < size; i++) {
dst[(i + dist) % size] = src[i] >> shift;
}
if (shift) {
for (size_t i = 0; i < size; i++) {
dst[(i + dist + 1) % size] |= src[i] << (32 - shift);
}
}
}
void rol32_array(uint32_t *dst, const uint32_t *src, size_t size, size_t n) {
size_t dist = n / 32 % size;
int shift = n % 32;
for (size_t i = 0; i < size; i++) {
dst[(i + size - dist) % size] = src[i] << shift;
}
if (shift) {
for (size_t i = 0; i < size; i++) {
dst[(i + size - dist - 1) % size] |= src[i] >> (32 - shift);
}
}
}
Here is a test program:
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
[...]
void print(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
while (*fmt) {
if (*fmt == '%') {
if (fmt[1] == 'd') {
printf("%d", va_arg(ap, int));
fmt += 2;
continue;
} else
if (fmt[1] == 'p') {
const uint32_t *src = va_arg(ap, uint32_t *);
size_t size = va_arg(ap, size_t);
for (size_t i = 0; i < size; i++) {
printf("%08lX%c", (long)src[i], " "[i + 1 == size]);
}
fmt += 2;
continue;
}
}
putchar(*fmt++);
}
va_end(ap);
}
int main() {
uint32_t array[] = { 0, 1, 2, 4, 8, 16 };
size_t size = sizeof(array) / sizeof(*array);
uint32_t dest_array[size];
int i, n, shift[] = { 0, 1, 2, 3, 8, 15, 24, 32, 48, 64, -1 };
print("array = { %p }\n", array, size);
for (i = 0; (n = shift[i]) >= 0; i++) {
ror32_array(dest_array, array, size, n);
memcpy(array, dest_array, sizeof(array));
print("ror(array, %d) = { %p }\n", n, array, size);
}
while ((n = shift[--i]) != 0) {
rol32_array(dest_array, array, size, n);
memcpy(array, dest_array, sizeof(array));
print("rol(array, %d) = { %p }\n", n, array, size);
}
return 0;
}
To rotate all the bits in an array, create a function that takes a size, a pointer to the data, and a shift amount.
void rotate_right(size_t sz, void *a, unsigned shift);
#define ROR_ARRAY(a, sh) rotate_right((sizeof (a)), (a), (sh))
For an array of 32-bit values, the bit shift of the array may be be the same arithmetically as some_32_bit >> sh due to endian. More advanced use of macros with _Generic solve solve that.
How can I create a bit-field array with variable size?
The following code is what I tried, which didn't work.
#include <stdio.h>
int main()
{
int n=4;
struct bite{
unsigned a1:2;
unsigned a2:2;
:
:
unsigned a(n-1):2;
unsigned a(n):2;
}bits;
for(i=1;i<=n;i++)
bits.a[i]=i;
for(i=1;i<=n;i++)
printf("%d ",bits.a[i]);
return 0;
}
The members of a struct cannot be defined at runtime.
You could simulate a bit array using a char array and some macros.
#define BitArray(array, bits) \
unsigned char array[bits / 8 + 1]
#define SetBit(array, n) \
do { array[n / 8] |= (1 << (n % 8)) } while (0)
#define GetBit(array, n) \
((array[n / 8] >> (n % 8)) & 1)
int main(void)
{
BitArray(bits, 42); /* Define 42 bits and init to all 0s
(in fact this allocates memory for (42/8 + 1) * 8 bits). */
SetBit(bits, 2); /* Set bit 2. */
int bit2 = GetBit(bits, 2); /* Get bit 2 */
...
Similar for 2-bit words are per your code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define Define2BitWordArray(array, two_bit_words) \
unsigned char array[two_bit_words / 4 + 1]
#define Set2BitWord(array, n, value) \
do { array[n / 4] |= (unsigned char)((value & 0x11) << (2 * (n % 4))); } while (0)
#define Get2BitWord(array, n) \
((array[n / 4] >> (2 * (n % 4))) & 0x11)
int main(void)
{
size_t n = 10;
Define2BitWordArray(bits, n); /* Define 10 two-bits words
(in fact this allocates memory
for (10/4 + 1) * 4 two-bit bits). */
memset(bits, 0, sizeof bits); /* Set all bits to 0. */
for(size_t i = 0; i < n; i++) /* C array's indexes are 0-based. */
{
Set2BitWord(bits, i, i);
}
for(size_t i = 0; i < n; i++)
{
printf("%d ", Get2BitWord(bits, i));
}
}
Remember your array always starts at 0 position.
You can do this:
#include <stdio.h>
typedef struct
{
unsigned int x : 1;
} one;
int main() {
int n = 10;
one xx[n];
int i;
for(i=0;i<n;i++)
xx[i].x=i;
for(i=0;i<n;i++)
printf("%d ",xx[i].x);
return 0;
}
if you want to operate on the particular bit or bits on data which has not limited size (actually limited to the max value of the size_t divided by 8). Little endian in this example
void setbit(void *obj, size_t bit)
{
uint8_t *p = obj;
p[bit >> 3] |= (1 << (bit & 7));
}
void resetbit(void *obj, size_t bit)
{
uint8_t *p = obj;
p[bit >> 3] &= ~(1 << (bit & 7));
}
void assignbit(void *obj, size_t bit, unsigned value)
{
uint8_t *p = obj;
p[bit >> 3] &= ~(1 << (bit & 7));
p[bit >> 3] != (!!value << (bit & 7));
}
void setbits(void *obj, size_t bit, size_t num)
{
while (num--)
setbit(obj, bit + num);
}
void resetbits(void *obj, size_t bit, size_t num)
{
while (num--)
resetbit(obj, bit + num);
}
void assignbits_slow(void *obj, size_t pos, size_t size, unsigned value)
{
for (size_t i = 1, j = 0; j < size; j++, i <<= 1)
assignbit(obj, pos + j, !!(value & i));
}
int getbit(void *obj, size_t bit)
{
uint8_t *p = obj;
return !!(p[bit >> 3] & (1 << (bit & 7)));
}
void *getbits(void *obj, void *buff, size_t bit, size_t nbits)
{
memset(buff, 0, nbits >> 3 + !!(nbits & 7));
do
{
nbits--;
assignbit(buff, bit + nbits, getbit(obj, bit + nbits));
} while (nbits);
return buff;
}
I need to create a memory struct for a project in which each word comprises 15 bits. When I check the size of the array I get that it is 2000 bytes in size, I assume it is because compiler byte alignment.
Is there a way to create the struct that it will be 1875 bytes in size?
This is the code I used:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(push,1)
struct myword
{
unsigned int val : 15;
};
struct mymemmory{
struct myword arr[1000];
};
#pragma pack(pop)
int main()
{
int size = sizeof(struct mymemmory);
printf("Size of arr: %d\n",size);
return 0;
}
When I use #pragma pack(push,0) I get that the size is 4000 bytes.
No, there isn't. If you need bit level granularity, you have to implement it yourself, making an array of the 1875 bytes and manually calculating indices and bit masks to extract the desired 15 bits. If you want to stay sane, you factor out this functionality into accessor functions, ideally using C++ or the like where you can make custom classes that abstract away the work entirely (so simple indexing use does all the "real index" and bit shift/mask work behind the scenes; std::vector<bool> already does something quite like this for single bits).
Of course, true sanity is realizing it's silly to quibble over 125 bytes. There are very few scenarios where saving one bit in sixteen for each value (especially for so few values) is worth it, and the ones I can think of (actually needing a compact representation on disk) are still handled better by converting from compact disk representation to expanded memory representation on read, and converted back on write, to avoid the hassle and computational overhead of dealing with all the shifting and masking on every read/write.
Unless you have a machine with 15-bit chars, you'll need to do a lot of bit manipulation to spread your 15-bit values over up to three unsigned chars using shifts and bit masks.
The following code works for machines with CHAR_BIT between 8 and 15 inclusive.
set15_le(mem, index, val) has mem pointing to an array of unsigned char providing the storage for an emulated array 15-bit words, index is the index of a 15-bit word, and val is a 15-bit value to be stored. get15_le(mem, index) returns the 15-bit word from the specified index. The 15-bit words are stored in "little-endian" byte order.
set15_be(mem, index, val) and get15_be(mem, index) are similar to the above, except that the 15-bit words are stored in "big-endian" byte order.
The main function tests both flavors, by storing a set of 15-bit, pseudo-random numbers in the array, reading them back, and checking they match the expected values.
#include <limits.h>
#if CHAR_BIT > 15
#error "Unsupported CHAR_BIT value"
#endif
unsigned short get15_le(const unsigned char *mem, unsigned long index)
{
unsigned long mem_index;
unsigned int mem_bitpos;
unsigned int val_bitpos;
unsigned short val_mask;
unsigned short val;
mem_index = (index * 15) / CHAR_BIT;
mem_bitpos = (index * 15) % CHAR_BIT;
val = 0;
val_bitpos = 0;
val_mask = (1U << 15) - 1;
while (val_mask)
{
unsigned int nbits;
unsigned char mem_mask;
unsigned char mem_byte;
nbits = CHAR_BIT - mem_bitpos;
if (nbits > 15 - val_bitpos)
{
nbits = 15 - val_bitpos;
}
mem_mask = val_mask << mem_bitpos;
mem_byte = mem[mem_index];
mem_byte &= mem_mask;
val |= (mem_byte >> mem_bitpos) << val_bitpos;
mem_bitpos += nbits;
if (mem_bitpos == CHAR_BIT)
{
mem_bitpos = 0;
mem_index++;
}
val_bitpos += nbits;
val_mask >>= nbits;
}
return val;
}
void set15_le(unsigned char *mem, unsigned long index, unsigned short val)
{
unsigned long mem_index;
unsigned int mem_bitpos;
unsigned int val_bitpos;
unsigned short val_mask;
mem_index = (index * 15) / CHAR_BIT;
mem_bitpos = (index * 15) % CHAR_BIT;
val_bitpos = 0;
val_mask = (1U << 15) - 1;
val &= val_mask;
while (val_mask)
{
unsigned int nbits;
unsigned char mem_mask;
unsigned char mem_byte;
nbits = CHAR_BIT - mem_bitpos;
if (nbits > 15 - val_bitpos)
{
nbits = 15 - val_bitpos;
}
mem_mask = val_mask << mem_bitpos;
mem_byte = mem[mem_index];
mem_byte &= ~mem_mask;
mem_byte |= ((val >> val_bitpos) << mem_bitpos) & mem_mask;
mem[mem_index] = mem_byte;
mem_bitpos += nbits;
if (mem_bitpos == CHAR_BIT)
{
mem_bitpos = 0;
mem_index++;
}
val_bitpos += nbits;
val_mask >>= nbits;
}
}
unsigned short get15_be(const unsigned char *mem, unsigned long index)
{
unsigned long mem_index;
unsigned int mem_bitpos;
unsigned int val_bitpos;
unsigned short val_mask;
unsigned short val;
mem_index = (index * 15) / CHAR_BIT;
mem_bitpos = CHAR_BIT - (index * 15) % CHAR_BIT;
val = 0;
val_bitpos = 15;
val_mask = (1U << 15) - 1;
while (val_mask)
{
unsigned int nbits;
unsigned char mem_mask;
unsigned char mem_byte;
nbits = mem_bitpos;
if (nbits > val_bitpos)
{
nbits = val_bitpos;
}
val_bitpos -= nbits;
mem_bitpos -= nbits;
mem_mask = (val_mask >> val_bitpos) << mem_bitpos;
mem_byte = mem[mem_index];
mem_byte &= mem_mask;
val |= (mem_byte >> mem_bitpos) << val_bitpos;
if (mem_bitpos == 0)
{
mem_bitpos = CHAR_BIT;
mem_index++;
}
val_mask >>= nbits;
}
return val;
}
void set15_be(unsigned char *mem, unsigned long index, unsigned short val)
{
unsigned long mem_index;
unsigned int mem_bitpos;
unsigned int val_bitpos;
unsigned short val_mask;
mem_index = (index * 15) / CHAR_BIT;
mem_bitpos = CHAR_BIT - (index * 15) % CHAR_BIT;
val_bitpos = 15;
val_mask = (1U << 15) - 1;
val &= val_mask;
while (val_mask)
{
unsigned int nbits;
unsigned char mem_mask;
unsigned char mem_byte;
nbits = mem_bitpos;
if (nbits > val_bitpos)
{
nbits = val_bitpos;
}
val_bitpos -= nbits;
mem_bitpos -= nbits;
mem_mask = (val_mask >> val_bitpos) << mem_bitpos;
mem_byte = mem[mem_index];
mem_byte &= ~mem_mask;
mem_byte |= ((val >> val_bitpos) << mem_bitpos) & mem_mask;
mem[mem_index] = mem_byte;
if (mem_bitpos == 0)
{
mem_bitpos = CHAR_BIT;
mem_index++;
}
val_mask >>= nbits;
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct mymemory
{
unsigned char content[(1000 * 15 + CHAR_BIT - 1) / CHAR_BIT];
};
int main(void)
{
struct mymemory mem;
unsigned long i;
unsigned short v;
printf("Memory size for 1000 15-bit words = %lu bytes (%lu bits)\n",
(unsigned long)sizeof(mem.content),
(unsigned long)sizeof(mem.content) * CHAR_BIT);
printf("Testing little-endian version\n");
memset(mem.content, 42, sizeof(mem.content));
srand(5);
for (i = 0; i < 1000; i++)
{
v = rand() & ((1U << 15) - 1);
set15_le(mem.content, i, v);
}
srand(5);
for (i = 0; i < 1000; i++)
{
unsigned int w;
v = rand() & ((1U << 15) - 1);
if ((w = get15_le(mem.content, i)) != v)
{
printf("Error at word %lu! got %u, expected %u\n", i, w, v);
break;
}
}
if (i == 1000)
{
printf("Passed!\n");
}
printf("Testing big-endian version\n");
memset(mem.content, 42, sizeof(mem.content));
srand(23);
for (i = 0; i < 1000; i++)
{
v = rand() & ((1U << 15) - 1);
set15_be(mem.content, i, v);
}
srand(23);
for (i = 0; i < 1000; i++)
{
unsigned int w;
v = rand() & ((1U << 15) - 1);
if ((w = get15_be(mem.content, i)) != v)
{
printf("Error at word %lu! got %u, expected %u\n", i, w, v);
break;
}
}
if (i == 1000)
{
printf("Passed!\n");
}
return 0;
}
I'm trying to write code to print the binary representation of a number. This was my attempt:
#include <stdio.h>
void getBinary(int num);
int main(void) {
int num = 2;
getBinary(num);
return 0;
}
void getBinary(int num) {
int mask = 1 << 31, i;
int temp;
for (i = 31; i >= 0; i--) {
temp = num & mask;
if (temp > 0)
printf("1");
else
printf("0");
mask = mask >> 1;
}
printf("\n");
}
And this doesn't work. If i make num = 2 my output will be ...0011. This is the answer I was given:
void getBinary(int);
int main()
{
int num=0;
printf("Enter an integer number :");
scanf("%d",&num);
printf("\nBinary value of %d is =",num);
getBinary(num);
return 0;
}
/*Function definition : getBinary()*/
void getBinary(int n)
{
int loop;
/*loop=15 , for 16 bits value, 15th bit to 0th bit*/
for(loop=15; loop>=0; loop--)
{
if( (1 << loop) & n)
printf("1");
else
printf("0");
}
}
and this will give the correct output. Why? What is different between my code and this. Aren't they doing the same thing with the solution doing it in fewer steps?
#include <stdio.h>
void getBinary(int num);
int main(void) {
unsigned int num = 2;
getBinary(num);
return 0;
}
void getBinary(int num) {
unsigned int mask = 1 << 31;
int i;
unsigned int temp;
for (i = 31; i >= 0; i--) {
temp = num & mask;
if (temp > 0)
printf("1");
else
printf("0");
mask = mask >> 1;
}
printf("\n");
}
For those curious, this is the correct answer. Just make the mask unsigned.
You have some problems in the code:
1 << 31 invokes undefined behavior because of signed arithmetic overflow. On your platform it most likely produces the value INT_MIN, that has the expected bit configuration but is negative.
temp > 0 might fail incorrectly if num < 0, because the sign bit will make temp negative too.
shifting mask to the right when its value is negative has an implementation defined result: On your platform it duplicates the sign bit, therefore mask will not have a single bit as expected, but all bits set above the current one. This explains the observed behavior.
You should use type unsigned int in function getBinary for num, temp and mask to get correct behavior.
Here is a corrected version of your code:
#include <stdio.h>
void getBinary(unsigned int num);
int main(void) {
unsigned int num = 2;
getBinary(num);
return 0;
}
void getBinary(unsigned int num) {
unsigned int mask = 1U << 31;
unsigned int temp;
for (int i = 31; i >= 0; i--) {
temp = num & mask;
if (temp != 0)
printf("1");
else
printf("0");
mask = mask >> 1;
}
printf("\n");
}
The proposed solution only prints 16 bits, which may or may not have been the specification. If the int type is larger than 16 bits, the bit shift will not overflow and everything is fine. If int has 16 bits, 1 << 15 invokes undefined behavior, so the solution is not strictly correct, but will function on most current platforms.
Here is a simpler solution:
void getBinary(unsigned int num) {
for (int shift = 32; shift-- > 0;) {
putchar('0' + ((num >> shift) & 1));
}
putchar("\n");
}
Initialize shift to 16 to print only the low order 16 bits.
I'm trying to get my program to work, where I shift bits to the left and add the shifted bits to the right. For example 00111000, if you shift it 4 positions to the left, the outcome should be 10000011. How can I make this work, I know that I need to use the bitwise OR. I have added the main function below.
#include <stdio.h>
#include <stdlib.h>
void printbits(int b){
int i;
int s = 8 * (sizeof b) - 1; /* 31 if int is 32 bits */
for(i=s;i>=0;i--)
putchar( b & 1<<i ? '1' : '0');
}
int main(){
char dir; /* L=left R=right */
int val, n, i;
scanf("%d %d %c",&val, &n, &dir);
printbits(val);putchar('\n');
for (i=0; i<10; i++){
if (dir=='L' || dir =='l')
rotateLeft(&val, n);
else
rotateRight(&val,n);
printbits(val); putchar('\n');
}
return;
}
This is the rotateLeft en rotateRight function.
#include <stdio.h>
#include <stdlib.h>
void rotateLeft(int *val, int N){
int num = val[0];
int pos = N;
int result = num << pos;
}
void rotateRight(int *val, int N){
int num = val[0];
int pos = N;
int result = num >> pos;
}
Here is a tested and non-optimized solution to complete your source code:
void rotateLeft(int *val, int N){
unsigned int num = val[0];
int pos = N;
unsigned int part1 = num << pos;
unsigned int part2 = (num >> ((sizeof(val[0])*CHAR_BIT)-pos));
if (N != 0) {
val[0] = part1 | part2;
}
}
void rotateRight(int *val, int N){
unsigned int num = val[0];
int pos = N;
unsigned int part1 = num >> pos;
unsigned int part2 = (num << ((sizeof(val[0])*CHAR_BIT)-pos));
if (N != 0) {
val[0] = part1 | part2;
}
}
To prevent automatic carry during the shift right, you have to
consider value as unsigned int.
To prevent N = 0 interference, assign the result to the entry only when (N != 0). (See remark on post ROL / ROR on variable using inline assembly in Objective-C)
MSB = (n >> (NUM_OF_BITS_IN_INT - 1))
n = (n << 1) | MSB;
Left bit rotation of n by 1 bit.
You are just shrugging off the MSB but not adding it back at LSB position.
use the functions in this link for performing the rotations:
https://en.wikipedia.org/wiki/Circular_shift