Swap the odd and even bits of an integer binary digit

Table of contents

1. Plan 1

1. Find the binary sequence of the operand to be operated

2. Create an array to store the binary sequence of the operands to be operated on

 3. Exchange parity bits of binary sequence

 4. Output the binary sequence after parity bit exchange

 5.Code

2. Option 2 (implementation of macro)

1. Find the even digits of the binary sequence of the operand to be operated

2. Find the odd digits of the binary sequence of the operand to be operated

3. Find the binary sequence in which the parity bits of the operands to be exchanged

 4.Code

 

1. Plan 1

1. Find the binary sequence of the operand to be operated

        The lowest bit of the binary sequence can be obtained by bitwise ANDing an integer with 1. Use the left shift operator to shift one bit to the left for each bitwise AND. In this way, the complete binary sequence can be obtained after 32 cycles.

2. Create an array to store the binary sequence of the operands to be operated on

        It is stored starting from the last element arr[31] of the array, and decreases in sequence. That is, the highest bit of the binary sequence is placed at the far left of the array, and the lowest bit is placed at the far right. Be consistent with the actual binary sequence order.

 

 3. Exchange parity bits of binary sequence

        Exchange the elements arr[0] and arr[1] in the array. After the execution, take two steps backward to exchange arr[2] and arr[3]. By analogy, if there is no exchange once, take two steps and repeat the operation until the exchange. Complete the last element arr[31].

 

 4. Output the binary sequence after parity bit exchange

        Essentially, it outputs the array after swapping the order.

 5.Code

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void swap_b(int x)
{
	int i = 0;
	int arr[32] = { 0 };
	for (i = 31; i >= 0; i--)
	{
		if ((x & 1) == 0)
		{
			arr[i] = 0;
		}
		else
		{
			arr[i] = 1;
		}
		x = x >> 1;
	}
	printf("二进制位序列:\n");
	for (i = 0; i < 32; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	for (i = 0; i < 32; i += 2)
	{
		int tmp = arr[i];
		arr[i] = arr[i + 1];
		arr[i + 1] = tmp;

	}
	printf("交换奇偶位的二进制位序列:\n");
	for (i = 0; i < 32; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}
int main()
{
	int a = 0;
	scanf("%d", &a);
	swap_b(a);
	return 0;
}

Summary: This solution can only output the binary sequence after exchanging the parity bits of the binary sequence of the operand, and will not change the operand itself.

2. Option 2 (implementation of macro)

1. Find the even digits of the binary sequence of the operand to be operated

        Perform a bitwise AND operation on the operand and a binary sequence in which the even bits are all 1 and the odd bits are all 0 (0x AA AA AA AA) to obtain the even bits of the operand.

 

 

2. Find the odd digits of the binary sequence of the operand to be operated

        Perform a bitwise AND operation between the operand and a binary sequence in which the odd bits are all 1 and the even bits are all 0 (0x 55 55 55 55) to obtain the odd bits of the operand.

 

3. Find the binary sequence in which the parity bits of the operands to be exchanged

        Shift the obtained binary sequence with known even digits and all odd digits to 0 to the left by one bit and the known binary sequence with odd digits and all even digits to 0 to the right by one bit, and then shift left and right respectively. Add the binary sequences to get the binary sequence in which the parity bits of the operands are exchanged.

 4.Code

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define SWAP_B(x) (x)=((((x)&(0xAAAAAAAA))>>1)+(((x)&(0x55555555))<<1))
int main()
{
	int a = 0;
	scanf("%d", &a);
	int i = 0;
	for (i = 31; i >= 0; i--)
	{
		if (((a >> i) & 1) == 1)
		{
			printf("%d ", 1);
		}
		else
		{
			printf("%d ", 0);
		}
	}
	printf("\n");
	SWAP_B(a);
	printf("%d\n", a);
	for (i = 31; i >= 0; i--)
	{
		if (((a >> i) & 1) == 1)
		{
			printf("%d ", 1);
		}
		else
		{
			printf("%d ", 0);
		}
	}
	printf("\n");
	return 0;
}

Summary: This solution directly operates on the operand and will change the value of the operand. The implementation of the macro can not only obtain the binary sequence after exchanging the parity bits, but also obtain the number corresponding to the binary sequence.

Guess you like

Origin blog.csdn.net/libj2023/article/details/131861053