社区
数据结构与算法
帖子详情
为了节省空间,怎样利用c++中如:int, char类型的每一个比特位
bcloud
2003-08-24 10:42:35
有没有什么函数可以直接知道某一位的值,以及直接对某一位置0或1。
谢谢。
...全文
207
11
打赏
收藏
为了节省空间,怎样利用c++中如:int, char类型的每一个比特位
有没有什么函数可以直接知道某一位的值,以及直接对某一位置0或1。 谢谢。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
用位域呀!
找本基础的书看看就知道了!
C语言:数值(
int
)在
char
类型
下的存储
首先,我们要了解到:在C语言中,
char
类型
变量在内存中存储占1个字节=8个
比特
位
;而数值是以二进制补码的形式在内存中存储(其中,正数:原码=反码=补码;负数: 反码=原码除符号
位
按
位
取反、补码=反码+1)。数值有...
C++
17 的 std::byte
类型
在大多数平台上,字节(也被称为 8
位
组),是计算机程序存储数据的基本单
位
。...
char
还真不是,它是
一个
字符
类型
,在
C++
的世界里一直被认为是整型数的一种,只是在大多数系统中恰好也是 8 个
比特
位
而已。
c++
byte 转
int
(
C++
基本
类型
整形)
但在
C++
中byte可以用unsigned
char
来表示,即无符号
类型
。那么如何将
C++
中的Byte转成整形呢? 其实在
C++
中,无论是BYTE转
int
还是
int
转BYTE,都是可以借助对应的方法的,即
c++
byte转
int
的方法是:bytesTo
Int
();反...
C++
int
型与
char
型辨析
在ASCII码中,
一个
英文字母(不分大小写)占
一个
字节的
空间
,
一个
中文汉字占两个字节的
空间
,
一个
字节是8
位
,
一个
汉字编码两个字节是十六
位
。英文标点占
一个
字节,中文标点占两个字节。
一个
二...
查找
char
型数组中第
一个
置0的
比特
位
的
位
置。
get_zero_bit_position_set 函数查找
char
型数组中第
一个
为0的
比特
位
的
位
置,找到后并将该bit
位
置1,然后返回找到的
位
置clear_set_bit_position函数与上
一个
函数作用相反:将
char
型数组中某个bit
位
值清0。...
数据结构与算法
33,027
社区成员
35,335
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章