使用C ++进行位级访问 [英] bit level access using C++
问题描述
是否可以使用C ++访问字节中的每个位。例如,如果我有
char * a = NULL;
a =新字符[500];
或
char a [500];
就像使用索引访问每个元素的方式一样,有没有在C ++中访问每个位的类似方法。
如果在C ++中可行,请提及一种方法,以及在指针方法和普通数组中访问这些位的区别初始化方法。
是否可以使用这些位组合实现二维矩阵形式的线性字符串。
< blockquote>假设,一般性:
const int N = 500 ;
a = new char [N];
char b [N];
您访问位以相同的方式 bitno
a
或 b
注意:未经测试):
int getbit( int bitno, int x [], int size)
{
if (bitno< 0 || bitno> =( size * 8 ))
{
// 在这里抛出异常
}
int byte,b;
byte = bitno / 8 ;
b = bitno% 8
return (x [byte]>> b)& 0×01;
}
Is it possible to access each bit in a byte using C++. For example if i have
char * a = NULL; a = new char[500];
or
char a[500];
Like how one accesses each individual element using its index , is there any similar method to access each bit in C++.
Please mention a method if feasible in C++ and also the difference between accessing those bits in the pointer method and in the normal array initialization method.
Is it possible to realise a linear string in 2 dimensional matrix form using these combination of bits.
Suppose, for generality:
const int N = 500; a = new char[N]; char b[N];
You access bitbitno
of eithera
orb
in the same way (note: not tested):
int getbit(int bitno, int x[], int size) { if ( bitno < 0 || bitno >= (size*8) ) { // throw an exception here } int byte, b; byte = bitno / 8; b = bitno % 8 return (x[byte] >> b) & 0x01; }
这篇关于使用C ++进行位级访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!