将BOOL数组初始化为TRUE ?? [英] Initialising a BOOL array to TRUE ??

查看:592
本文介绍了将BOOL数组初始化为TRUE ??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BOOL bMyarray [1000];

memset(bMyArray,0xFF,sizeof(BOOL)* 1000);


clean&快速直到别人的代码发现我返回-1到

意味着真实


因为我的BOOL是4个字节长,所以内存中的TRUE可以解决作为01 00 00

00这是一个正确的痛苦。


在for循环中执行它是10倍慢btw。

这是一个多余的问题,还有另外一种方法吗?


TIA

BOOL bMyarray[1000];
memset(bMyArray , 0xFF, sizeof(BOOL) * 1000);

clean & quick until someone else''s code found I was returning -1 to
mean true

Because my BOOL is 4 bytes long, TRUE in memory works out as 01 00 00
00 which is a right pain.

Doing it in a for loop is 10 x slower btw.

Bit of a numpty question this, is there another way?

TIA

推荐答案

Simon L写道:
Simon L wrote:

BOOL bMyarray [1000];

memset(bMyArray,0xFF,sizeof(BOOL)* 1000);


clean&快速直到别人的代码发现我返回-1到

意味着真实


因为我的BOOL是4个字节长,所以内存中的TRUE可以解决如01 00 00

00这是一个正确的痛苦。
BOOL bMyarray[1000];
memset(bMyArray , 0xFF, sizeof(BOOL) * 1000);

clean & quick until someone else''s code found I was returning -1 to
mean true

Because my BOOL is 4 bytes long, TRUE in memory works out as 01 00 00
00 which is a right pain.



为什么不使用bool?


-

Ian Collins。

Why not use bool?

--
Ian Collins.



为什么不使用bool?


-

伊恩柯林斯。
Why not use bool?

--
Ian Collins.



我在C工作(oops,错误组......)


I''m working in C (oops, wrong group..)


Simon L< si*******@hotmail.comwrites:
Simon L <si*******@hotmail.comwrites:

BOOL bMyarray [1000];

memset(bMyArray,0xFF,sizeof (BOOL)* 1000);


clean&快速直到别人的代码发现我返回-1到

意味着真实


因为我的BOOL是4个字节长,所以内存中的TRUE可以解决作为01 00 00

00这是一个正确的痛苦。


在for循环中执行它是10倍慢btw。

这是一个多余的问题,还有另外一种方法吗?
BOOL bMyarray[1000];
memset(bMyArray , 0xFF, sizeof(BOOL) * 1000);

clean & quick until someone else''s code found I was returning -1 to
mean true

Because my BOOL is 4 bytes long, TRUE in memory works out as 01 00 00
00 which is a right pain.

Doing it in a for loop is 10 x slower btw.

Bit of a numpty question this, is there another way?



如果您有很多布尔值,可能值得将它们存储为

位。使用更少的内存,更多的向量将保留在L1缓存中,因此访问它们的
将更快(即使考虑到

打包/解包):

#include< iostream>

#include< limits.h>

typedef int bit; //一位,0或1

typedef int word; //一些比特

#ifndef WORD_BIT

#define WORD_BIT(sizeof(word)* CHAR_BIT)

#endif


class bit_vector {

protected:

unsigned int dimension;

word * words;

public:

bit_vector(unsigned int dimension){

this-> dimension = dimension;

this-> words = new word [(this-> dimension + WORD_BIT-1)/ WORD_BIT];

}

virtual~bit_vector(){

delete [] this-> words;

}


内联位运算符[](unsigned int index){

return((index<) ; this-> dimension)

?(1&(this-> words [index / WORD_BIT]>>(index%WORD_BIT)))

: 0); }


内联位设置(unsigned int index,bit value){

if(index< this-> dimension){

if(value == 0){

this-> words [index / WORD_BIT]& =(〜(1<<(index%WORD_BIT)));

} else {

this-> words [index / WORD_BIT] | =(1<<(index%WORD_BIT)); } $

return(value); }


//所以现在填充向量的速度将快256倍:


无效填充(位值){

word filler =(value == 0)?(0):(〜0);

for(unsigned int i = 0; i<(this-> dimension + WORD_BIT-1) )/ WORD_BIT; i ++){this-> words [i] = filler;使用命名空间std;

};


int main(void){

bit_vector v(1000);

v.fill(1);

cout<< v [0]<<" ;,"<< v [999]<< endl;

v.fill(0);

cout<<< v [0]< ;<","<< v [999]<< endl;

v.set(0,1);

cout<< ; v [0]<<","<< v [999]<< endl;

v.set(999,1);

cout<< v [0]<<","<< v [999]<< endl;

return(0);

}


-

__Pascal Bourguignon__

If you have a lot of booleans, it may be worthwhile to store them as
bits. Using less memory, more of the vector will stay in L1-cache, so
accessing them will be faster (even considering the bit
packing/unpacking):
#include <iostream>
#include <limits.h>
typedef int bit; // one bit, 0 or 1
typedef int word; // some bits
#ifndef WORD_BIT
#define WORD_BIT (sizeof(word)*CHAR_BIT)
#endif

class bit_vector {
protected:
unsigned int dimension;
word* words;
public:
bit_vector(unsigned int dimension){
this->dimension=dimension;
this->words=new word[(this->dimension+WORD_BIT-1)/WORD_BIT];
}
virtual ~bit_vector(){
delete [] this->words;
}

inline bit operator[](unsigned int index){
return((index<this->dimension)
?(1&(this->words[index/WORD_BIT]>>(index%WORD_BIT)))
:0); }

inline bit set(unsigned int index,bit value){
if(index<this->dimension){
if(value==0){
this->words[index/WORD_BIT]&=(~(1<<(index%WORD_BIT)));
}else{
this->words[index/WORD_BIT]|=(1<<(index%WORD_BIT)); }}
return(value); }

// so now filling the vector will be 256 times faster:

void fill(bit value){
word filler=(value==0)?(0):(~0);
for(unsigned int i=0;i<(this->dimension+WORD_BIT-1)/WORD_BIT;i++){ this->words[i]=filler; }}

};

using namespace std;

int main(void){
bit_vector v(1000);
v.fill(1);
cout<<v[0]<<", "<<v[999]<<endl;
v.fill(0);
cout<<v[0]<<", "<<v[999]<<endl;
v.set(0,1);
cout<<v[0]<<", "<<v[999]<<endl;
v.set(999,1);
cout<<v[0]<<", "<<v[999]<<endl;
return(0);
}

--
__Pascal Bourguignon__


这篇关于将BOOL数组初始化为TRUE ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆