Bit field structure arrays in C - c

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;
}

Related

How can I rotate to left/right all the bits in a byte array by some amount

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.

CRC32 - wrong checksum using TABLE algorithm and 04C11DB7 polynomial

I am following a painless guide to code correction algorithms. (https://zlib.net/crc_v3.txt) I've managed to write a TABLE algorithm, using extra loop for augmented part (I hope so). I am trying to write a most widely used CRC32 version (with 0x04C11DB7 polynomial), but I can not get the right CRC value.
I've achieved the correct table for CRC32 values with mentioned polynomial.
My code for generating CRC32 (chapter 9 and 10):
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define CRC32_BYTE_POSSIBLE_VALUES 255
#define CRC32_LAST_BIT_MASK 0x80000000
#define CRC32_POLYNOMIAL 0x04C11DB7
uint32_t __crc32_table[CRC32_BYTE_POSSIBLE_VALUES] = { 0 };
void __crc32_fill_crc_table() {
uint32_t reg;
uint8_t byte = 0;
for (;;) {
reg = (byte << 24);
for (uint8_t byte_size = 0; byte_size < 8; byte_size++) {
if (reg & CRC32_LAST_BIT_MASK) {
reg <<= 1;
reg ^= CRC32_POLYNOMIAL;
} else {
reg <<= 1;
}
}
__crc32_table[byte] = reg;
if (byte == 255)
break;
else
byte++;
}
}
void __crc32_print_table(uint32_t *arr) {
printf(" 0x%08X ", arr[0]);
for (uint32_t i = 1; i < 256; i++) {
if (!(i % 8))
printf("\n");
printf(" 0x%08X ", arr[i]);
}
printf("\n");
}
uint8_t inverse_byte(uint8_t byte) {
uint8_t reflected_byte = 0;
for (uint8_t i = 0; i < 8; i++) {
if (byte & (1 << i))
reflected_byte |= (1 << (7 - i));
}
return reflected_byte;
}
uint32_t inverse(uint32_t src) {
uint32_t toret;
for (uint8_t i = 0; i < 32; i++) {
if (src & (1 << i))
toret |= (1 << (31 - i));
}
return toret;
}
uint32_t __crc32_table_approach( unsigned char *data, size_t size) {
uint32_t reg = -1;
uint8_t top_byte;
for (size_t i = 0; i < size; i++) {
top_byte = (uint8_t)(reg >> 24);
reg = (reg << 8) | inverse_byte(data[i]);
reg ^= __crc32_table[top_byte];
}
for (size_t i = 0; i < 4; i++) {
top_byte = (uint8_t) (reg >> 24);
reg = (reg << 8) ;
reg ^= __crc32_table[top_byte];
}
return inverse(reg) ^ -1;
}
uint32_t calc_crc32(unsigned char *data, size_t size) {
if (!__crc32_table[1])
__crc32_fill_crc_table();
__crc32_print_table(__crc32_table);
return __crc32_table_approach(data, size);
}
int main( int argc, char** argv )
{
unsigned char* test = "123456789";
size_t test_len = strlen(test);
uint32_t crc = calc_crc32(test, test_len);
printf("CRC32: 0x%08X", crc);
return 0;
}
The inverse function reverses bits of UINT32 value, and function inverse_byte inverses bits of UINT8 value.
But for the '123456789' string I get the wrong checksum.
Could someone help me? Or give some advice?
Input string: '123456789'
Outputted CRC: CRC32: 0x22016B0A
Desired CRC: CRC32: 0xCBF43926
You made your array one word too short, and so overwrote the allocated memory. It needs to be:
#define CRC32_BYTE_POSSIBLE_VALUES 256
Though that part probably still worked, because C.
You need to initialize the variable you are reversing into:
uint32_t toret = 0;
These lines:
top_byte = (uint8_t)(reg >> 24);
reg = (reg << 8) | inverse_byte(data[i]);
need to be:
top_byte = (uint8_t)(reg >> 24) ^ inverse_byte(data[i]);
reg <<= 8;
and you need to delete these lines:
for (size_t i = 0; i < 4; i++) {
top_byte = (uint8_t) (reg >> 24);
reg = (reg << 8) ;
reg ^= __crc32_table[top_byte];
}
Then you get the right answer.
If you would like to implement the table approach on the augmented message as described in Chapter 9 (requiring another four iterations of the CRC at the end as in your code), then you need to first read these important notes in the document:
Note: The initial register value for this algorithm must be the
initial value of the register for the previous algorithm fed through
the table four times. Note: The table is such that if the previous
algorithm used 0, the new algorithm will too.
To get the same effect as the initial value of 0xffffffff (notably not zero) with the non-augmented message version, which is how that standard CRC is defined, then you'd need to find an initial value such that applying 32 zero bits to it using the CRC gives you 0xffffffff. That value is 0x46af6449, obtained by reversing the CRC bit-wise algorithm:
uint32_t x = -1;
for (unsigned i = 0; i < 32; i++)
x = x & 1 ? ((x ^ 0x4c11db7) >> 1) | 0x80000000 : x >> 1;
Then your code will work if you fix the array size and the toret initialization errors, and simply replace:
uint32_t reg = -1;
with:
uint32_t reg = 0x46af6449;
Either augmented or not, reversing every single input byte as you are doing is a waste of time. You can and should instead just reverse the calculation and the polynomial. See rcgldr's answer.
Example code using right shifting CRC (0xedb88320 is a reflected version of 0x04C11DB7):
#include <iostream>
#include <iomanip>
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
uint32_t crctbl[256];
void gentbl(void)
{
uint32_t crc;
uint32_t c;
uint32_t i;
for(c = 0; c < 0x100; c++){
crc = c;
for(i = 0; i < 8; i++){
crc = (crc & 1) ? (crc >> 1) ^ 0xedb88320 : (crc >> 1);
}
crctbl[c] = crc;
}
}
uint32_t crc32(uint8_t * bfr, size_t size)
{
uint32_t crc = 0xfffffffful;
while(size--)
crc = (crc >> 8) ^ crctbl[(crc & 0xff) ^ *bfr++];
return(crc ^ 0xfffffffful);
}
int main(int argc, char**argv)
{
uint32_t crc;
uint8_t msg[10] = "123456789";
gentbl();
crc = crc32(msg, 9);
std::cout << "crc " << std::hex << std::setw(8) << std::setfill('0') << crc << std::endl;
return(0);
}

Swap bytes within an unsigned long (8 bytes) in C

unsigned long swap_bytes(unsigned long n) {
unsigned char bytes[8];
unsigned long temp;
bytes[0] = (n >> ??) & 0xff;
bytes[1] = (n >> ??) & 0xff;
// ...
}
I am really confused on how to shift an 8 byte unsigned long from
ex: 0x12345678deadbeef to 0x34127856addeefbe. I started this but I am now stuck. Please help.
You can mask every other byte, in both versions, using & binary AND.
Then shift both masked values, in opposite directions and combine them using | binary OR.
n = ((n & 0xff00ff00ff00ff00ull)>>8ull)
| ((n & 0x00ff00ff00ff00ffull)<<8ull);
In case it is 4byte (what I consider an unsigned long in contrast to the unsigned long long, but that is implementation specific), then the code is
n = ((n & 0xff00ff00ul)>>8ul)
| ((n & 0x00ff00fful)<<8ul);
You could try this
void swap(char *ptr, int i, int j)
{
char temp=*(ptr+i);
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
}
int main()
{
//unsigned long n = 0x12345678deadbeef;
uint64_t n = 0x12345678deadbeef;
char *ptr=(char *)&n;
swap(ptr, 0, 1);
swap(ptr, 2, 3);
swap(ptr, 4, 5);
swap(ptr, 6, 7);
}
The address of n is cast into a char pointer and stored in ptr.
ptr+i will give the address of the ith byte of n whose value (ie, *(ptr+i)) is modified appropriately by the swap() function.
Edit: If you are not sure if your unsigned long's size is 8 bytes, you could use uint64_t from inttypes.h.
universal & horrible
void swapbytes(void *p, size_t size)
{
uint8_t tmp, *ptr = p;
for (size_t i = 0; i < (size - 1); i += 2)
{
tmp = *(ptr + i);
*(ptr + i) = *(ptr + i + 1);
*(ptr + i + 1) = tmp;
}
}
usage:
uint64_t n;
swapbytes(&n, sizeof n);

15bit struct in C

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;
}

Swap two bits in given integer

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)

Resources