当前位置:网站首页>C语言程序设计 | 交换二进制数奇偶位(宏实现)
C语言程序设计 | 交换二进制数奇偶位(宏实现)
2022-07-29 02:46:00 【回不去_从前了丶】
题目要求
交换奇偶位
写一个宏,可以将一个整数的二进制位的奇数位和偶数位交换。
思路讲解
假设我们现在有这么一个二进制数

0是偶数位,那么可以看出红色的就是偶数位
31是奇数位,那么绿色的就是奇数位
我们先把偶数位的提取出来,然后再把红色的提取出来。
然后偶数位右移一位,奇数位左移一位,得出来得两个二进制数相加即使我们所要交换得结果
提取偶数位

提取奇数位

偶数位右移一位

奇数位左移一位

二者相加

代码示例
#define _CRT_SECURE_NO_WARNINGS 1
#define SWAP_BIT(n) (n=((n&0x55555555)<<1)+((n&0xaaaaaaaa)>>1))
#include <stdio.h>
int main()
{
int num = 0;
scanf("%d", &num);
int ret = SWAP_BIT(num);
printf("%d ", ret);
return 0;
}
边栏推荐
- navicat新建数据库
- Analysis of OWT server source code (III) -- video module analysis of mixer in
- 第十天笔记
- 看机器人教育引领素质教育主流
- Day 8 notes
- Mongodb index (3)
- VIM common commands
- Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
- Multiple inheritance and derived class member identification
- RestfulAPI——C#——增加header用户名密码验证
猜你喜欢

Flink生产环境经典问题汇总

Polygon zkevm - Introduction to HERMEZ 2.0

Advanced architects, 16 common principles of microservice design and Governance

Implement encapsulated public method global call in laravel framework

Which is a good automatic account distribution system?

golang 协程的实现原理

centos安装mysql8

C language: hollow square pattern

Flink内核源码(七)Flink SQL提交流程

UE4.25 Slate源码解读
随机推荐
Pytest environment deployment + use case execution management + use case parameterization
R语言ERROR: compilation failed for package ‘****‘
Ffmpeg+sdl+qt is a simple video player
Notes on the seventh day
会议OA之反馈功能
混淆矩阵学习笔记
C语言:小乐乐与欧几里得
10.书写规则-文件搜寻
FTP protocol details
解析Steam教育中的项目式学习创造力
Pgzero飞机大战
SOA(面向服务架构)是什么?
Restfulapi - C - add header username and password authentication
Advanced architects, 16 common principles of microservice design and Governance
外挂---线段树懒标记板子+简单数学推理
R language error: compilation failed for package '****‘
Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
C#从网址异步获得json格式的数据
Analyzing the subjective consciousness of emotional resonance between robots and human beings
Which is a good automatic account distribution system?