当前位置:网站首页>QT custom bit operation class
QT custom bit operation class
2022-06-29 06:56:00 【Mr.codeee】
1. It encapsulates a bit operation 、 Class with simple byte operation , For reference only
Go straight to source :
//.h
#ifndef BITSMANAGER_H
#define BITSMANAGER_H
#include <qglobal.h>
class BitsManager
{
public:
BitsManager();
// Specify a bit as 1 Bit number [0~31]
static quint32 setBit1(quint32 value,int bitNum);
// Specify a bit as 0 Bit number [0~31]
static quint32 setBit0(quint32 value,int bitNum);
// Specify a bit to negate
static quint32 setBitReverse(quint32 value,int bitNum);
// Get the value of a bit
static quint32 getBit(quint32 value,int bitNum);
// Get single byte [0~3]
static quint32 getByte(quint32 value,int byteNum);
// Clear a byte [0~3]
static quint32 setByte0(quint32 value,int byteNum);
// Set a byte to 1 [0~3]
static quint32 setByte1(quint32 value,int byteNum);
};
#endif // BITSMANAGER_H
//.cpp
#include "BitsManager.h"
BitsManager::BitsManager()
{
}
quint32 BitsManager::setBit1(quint32 value, int bitNum)
{
return value|(1<<bitNum);
}
quint32 BitsManager::setBit0(quint32 value, int bitNum)
{
return value&=~(1<<bitNum);
}
quint32 BitsManager::setBitReverse(quint32 value, int bitNum)
{
return value^=(1<<bitNum);
}
quint32 BitsManager::getBit(quint32 value, int bitNum)
{
return (value)>>(bitNum)&1;
}
quint32 BitsManager::getByte(quint32 value, int byteNum)
{
quint32 ret = 0;
if(byteNum == 0)
ret = (value>>0) & 0x000000ff;
else if(byteNum == 1)
ret = (value>>8) & 0x000000ff;
else if(byteNum == 2)
ret = (value>>16) & 0x000000ff;
else if(byteNum == 3)
ret = (value>>24) & 0x000000ff;
return ret;
}
quint32 BitsManager::setByte0(quint32 value, int byteNum)
{
quint32 ret = 0;
if(byteNum == 0)
ret = value &= 0xffffff00;
else if(byteNum == 1)
ret = value &= 0xffff00ff;
else if(byteNum == 2)
ret = value &= 0xff00ffff;
else if(byteNum == 3)
ret = value &= 0x00ffffff;
return ret;
}
quint32 BitsManager::setByte1(quint32 value, int byteNum)
{
quint32 ret = 0;
if(byteNum == 0)
ret = value |= 0x000000ff;
else if(byteNum == 1)
ret = value |= 0x0000ff00;
else if(byteNum == 2)
ret = value &= 0x00ff0000;
else if(byteNum == 3)
ret = value &= 0xff000000;
return ret;
}
边栏推荐
- JDBC | Chapter 5: closing and releasing JDBC connection resources
- Configuring MySQL 5.7 and 8 under CentOS
- Json对象和Json字符串的区别
- Ribbon 服务调用与负载均衡
- Service grid ASM year end summary: how do end users use the service grid?
- Some high-level usage of localstorage
- 想请教一下,究竟有哪些劵商推荐?在线开户是安全么?
- WDCP accesses all paths that do not exist and jumps to the home page without returning 404
- 施工企业选择智慧工地的有效方法
- Unity AR Shadow 阴影
猜你喜欢

The annual technology inventory of cloud primitives was released, and it was the right time to ride the wind and waves

Message queue avoiding repeated refund by idempotent design and atomic lock

Qt 串口编程

Delete tag

Creating a new generation of production and service tools with robot education

Presto-Trial

二叉树的迭代法前序遍历的两种方法

开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
![[C language] flexible array](/img/22/3255740602232abfdf69624762adca.jpg)
[C language] flexible array

Li Kou today's question -324 Swing sort II
随机推荐
Fault: KDC warning log for id29
Qt QLineEdit详解
VerilogA——动态比较器
Analysis on the wave of learning robot education for children
try anbox (by quqi99)
Li Kou daily question - day 30 -594 Longest harmonic subsequence
[when OSPF introduces direct connection routes, it makes a summary by using static black hole routes]
Delete tag
[c language] [sword finger offer article] - print linked list from end to end
How to do the performance pressure test of "Health Code"
Introduction to Ceres Quartet
力扣每日一题-第30天-594.最长和谐子序列
解析学习幼儿机器人教育的浪潮
Presto-Trial
WDCP accesses all paths that do not exist and jumps to the home page without returning 404
'only_ full_ group_ The influence of by'sql mode on group by and its treatment
【OSPF引入直连路由时巧借静态黑洞路由做汇总】
Part 63 - interpreter and compiler adaptation (II)
json tobean
[deep learning] - maze task learning I (to realize the random movement of agents)