什么是这些#define语句的意义是什么? [英] What is the significance of these #defines?

查看:224
本文介绍了什么是这些#define语句的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历的问题MAXCOUNT的最佳解决方案,并发现了几行字,我不明白。

I was going through the best solution of problem MAXCOUNT and found few lines which i didn't understand.

codechef问题
最佳的解决方案提交

当我在读code观察的方法,我碰到这些线在code的顶部:

While I was reading the code to observe the approach, I encountered these lines at the top of code:

#define isSet(n) flags[n>>5]&(1<<(n&31))
#define unset(n) flags[n>>5] &= ~(1<<(n&31))
#define set(n) flags[n>>5]|=(1<<(n&31))

我不知道什么是使用这些线的意义。结果
任何人都可以请解释一下这些线路,以及为什么使用它们?

I have no idea what is the significance of using these lines.
Can anyone please explain these lines and why are they used?

推荐答案

它看起来像它指的是存储为整数位的数组。 N >> 5手段,32分吧,有32位在一个int,使索引数组。然后右手侧使用的余数(31位模式为 0b11111 义和操作,将筛选出位的所有的休息,只留下五至少显著的 - 这恰好除以32之后的余数相匹配),以找到特定的位是感兴趣的(1 <<;&下; N带球位置n的一比特掩码以及与放大器;算滤除一切,但这一点)。

It looks like it is referring to an array of bits stored as integers. n >> 5 means divide it by 32, and there's 32 bits in an int so that indexes the array. Then the right hand side uses the remainder (the bit pattern of 31 is is 0b11111 meaning the and operation with it will filter out all the rest of the bits, leaving just the five least significant ones - which happens to match the remainder after dividing by 32) to find the specific bit it is interested in (1 << n makes a one-bit mask in position n, and the & operator filters out everything but that).

取消设置使用〜,它反转面具 - 清位,留下其余不变。 设置使用按位或操作,它设置一个位。

Unset uses ~, which inverts the mask - clearing the bit, leaving the rest the same. set uses the bitwise or operator, which sets a single bit.

因此​​,他们是在整数数组与各个位工作只是方便宏。

So they are just convenience macros for working with individual bits in an array of integers.

这篇关于什么是这些#define语句的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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