为了节省空间,怎样利用c++中如:int, char类型的每一个比特位

bcloud 2003-08-24 10:42:35
有没有什么函数可以直接知道某一位的值,以及直接对某一位置0或1。
谢谢。
...全文
174 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcloud 2003-08-26
  • 打赏
  • 举报
回复
谢谢大家,长见识。
littlecpu 2003-08-25
  • 打赏
  • 举报
回复
Java是超强类型,int与boolean不自动转换
getBit的另一版本 i---值, pos---位置,
... ... ... 7 6 4 3 2 1 0
int GetBit(int i, int pos)
{
return (i >> pos) & 1;
}
boolean GetBit(int i, int pos)
{
return (i >> pos) & 1 == 1;
}
a0002 2003-08-25
  • 打赏
  • 举报
回复

32的程序啊!

a0002 2003-08-25
  • 打赏
  • 举报
回复
#include "stdafx.h"

#include <iostream.h>
#include <stdio.h>

struct CELL // Declare CELL bit field
{
unsigned bit01 : 1; //0000000000000000000000000000000?
unsigned bit02 : 1; //000000000000000000000000000000?0
unsigned bit03 : 1;
unsigned bit04 : 1;
unsigned bit05 : 1;
unsigned bit06 : 1;
unsigned bit07 : 1;
unsigned bit08 : 1;
unsigned bit09 : 1;
unsigned bit10 : 1;
unsigned bit11 : 1;
unsigned bit12 : 1;
unsigned bit13 : 1;
unsigned bit14 : 1;
unsigned bit15 : 1;
unsigned bit16 : 1;
unsigned bit17 : 1;
unsigned bit18 : 1;
unsigned bit19 : 1;
unsigned bit20 : 1;
unsigned bit21 : 1;
unsigned bit22 : 1;
unsigned bit23 : 1;
unsigned bit24 : 1;
unsigned bit25 : 1;
unsigned bit26 : 1;
unsigned bit27 : 1;
unsigned bit28 : 1;
unsigned bit29 : 1;
unsigned bit30 : 1;
unsigned bit31 : 1;
unsigned bit32 : 1; //?0000000000000000000000000000000
};

union IntToBit // Declare union type
{
int iNum;
struct CELL bits;
};

int main(int argc, char* argv[])
{
IntToBit iBlob;

cout << endl << "Enter your number: ";

cin >> iBlob.iNum;
cout << endl << "int number: " << iBlob.iNum << endl;

cout << endl << "binary: " << iBlob.bits.bit32
<< iBlob.bits.bit31
<< iBlob.bits.bit30
<< iBlob.bits.bit29
<< iBlob.bits.bit28
<< iBlob.bits.bit27
<< iBlob.bits.bit26
<< iBlob.bits.bit25
<< iBlob.bits.bit24
<< iBlob.bits.bit23
<< iBlob.bits.bit22
<< iBlob.bits.bit21
<< iBlob.bits.bit20
<< iBlob.bits.bit19
<< iBlob.bits.bit18
<< iBlob.bits.bit17
<< iBlob.bits.bit16
<< iBlob.bits.bit15
<< iBlob.bits.bit14
<< iBlob.bits.bit13
<< iBlob.bits.bit12
<< iBlob.bits.bit11
<< iBlob.bits.bit10
<< iBlob.bits.bit09
<< iBlob.bits.bit08
<< iBlob.bits.bit07
<< iBlob.bits.bit06
<< iBlob.bits.bit05
<< iBlob.bits.bit04
<< iBlob.bits.bit03
<< iBlob.bits.bit02
<< iBlob.bits.bit01
<< endl << endl;

getchar();
return 0;
}
bcloud 2003-08-25
  • 打赏
  • 举报
回复
我就是手头没有书嘛!
不过用>>,<<可以搞一搞。
不知还有没有其他的更好用的函数?
寻开心 2003-08-25
  • 打赏
  • 举报
回复
bool GetBit(int i, int v) {return v & (1<<i);}
void SetBit(int i, int v) { v |= 1<< i;};
void ClearBit(int i,int v){ v &= ~(1<<i);};
Smile_Tiger 2003-08-25
  • 打赏
  • 举报
回复
//对第4位置1
int a = 0x0100;

a = a | 0x10;

寻开心 2003-08-25
  • 打赏
  • 举报
回复
bool GetBit(int i, int var) {return var&(1<<i);};
void SetBit(int i, int var) { var |= 1<<i; };
void ClearBit(int i, int var ) { var &= ~(1<<i);};
Smile_Tiger 2003-08-25
  • 打赏
  • 举报
回复
int a = 0x0100; //第8位为1

assert(a&0x0100);

BlueSky2008 2003-08-25
  • 打赏
  • 举报
回复
同意happy__888。要是改成宏或内联函数就更好了。
a0002 2003-08-24
  • 打赏
  • 举报
回复

用位域呀!

找本基础的书看看就知道了!

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧