如果可能,如何在C中定义2位数? [英] How to define 2-bit numbers in C, if possible?

查看:194
本文介绍了如果可能,如何在C中定义2位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的大学过程,我正在模拟一个称为随机顺序吸附的过程。
我要做的事情之一就是将正方形(不能重叠)随机存放到格子上,直到没有剩余空间为止,重复几次以找到平均干扰覆盖率%。

For my university process I'm simulating a process called random sequential adsorption. One of the things I have to do involves randomly depositing squares (which cannot overlap) onto a lattice until there is no more room left, repeating the process several times in order to find the average 'jamming' coverage %.

基本上我正在对大量整数执行操作,其中存在3个可能的值:0,1和2.标有0的站点为空,标有1的网站已满。最初,数组定义如下:

Basically I'm performing operations on a large array of integers, of which 3 possible values exist: 0, 1 and 2. The sites marked with '0' are empty, the sites marked with '1' are full. Initially the array is defined like this:

int i, j;
int n = 1000000000;
int array[n][n];

for(j = 0; j < n; j++)
{
    for(i = 0; i < n; i++)
    {
        array[i][j] = 0;
    }
}

我想要随机存入5 * 5个正方形数组(不能重叠),以便正方形用'1'表示。这可以通过随机选择x和y坐标然后创建一个5 * 5平方的'1'来实现,该点的正方形点从该点开始。然后我会将广场附近的景点标记为'2'。这些代表不可用的站点,因为在这些站点上放置一个正方形将导致它与现有正方形重叠。这个过程将继续,直到没有更多的空间在阵列上存放方块(基本上,阵列上没有剩下'0')

Say I want to deposit 5*5 squares randomly on the array (that cannot overlap), so that the squares are represented by '1's. This would be done by choosing the x and y coordinates randomly and then creating a 5*5 square of '1's with the topleft point of the square starting at that point. I would then mark sites near the square as '2's. These represent the sites that are unavailable since depositing a square at those sites would cause it to overlap an existing square. This process would continue until there is no more room left to deposit squares on the array (basically, no more '0's left on the array)

无论如何,要点。我想通过使用按位运算使这个过程尽可能高效。如果我不必在广场附近标记网站,这将很容易。我想知道是否可以创建一个2位数字,以便我可以说明标有2的网站。

Anyway, to the point. I would like to make this process as efficient as possible, by using bitwise operations. This would be easy if I didn't have to mark sites near the squares. I was wondering whether creating a 2-bit number would be possible, so that I can account for the sites marked with '2'.

很抱歉,如果这听起来很复杂,我只想解释为什么我要这样做。

Sorry if this sounds really complicated, I just wanted to explain why I want to do this.

推荐答案

您可以使用两个独立的1位数组,而不是两位数组。一个持有填充的正方形,一个持有相邻的正方形(如果效率更高,可以使用可用的正方形)。

Instead of a two-bit array you could use two separate 1-bit arrays. One holds filled squares and one holds adjacent squares (or available squares if this is more efficient).

我不确定这有什么好处但是包装2将字段转换为单词。
我会选择字节数组,除非你真的缺少内存。

I'm not really sure that this has any benefit though over packing 2-bit fields into words. I'd go for byte arrays unless you are really short of memory.

这篇关于如果可能,如何在C中定义2位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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