从图像获取/设置位值 [英] Getting/Setting a bit value from image

查看:87
本文介绍了从图像获取/设置位值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题基于以下站点

有人可以解释这些行的含义吗?

Could someone explain the meaning of these lines:

private int getBitValue(int n, int location) {
      int v = n & (int) Math.round(Math.pow(2, location));
      return v==0?0:1;
 }

private int setBitValue(int n, int location, int bit) {
    int toggle = (int) Math.pow(2, location), bv = getBitValue(n, location);
    if(bv == bit)
        return n;
    if(bv == 0 && bit == 1)
        n |= toggle;
    else if(bv == 1 && bit == 0)
        n ^= toggle;
    return n;
}


推荐答案

  int v = n & (int) Math.round(Math.pow(2, location));

Math.pow(2,location)将2提高到给定的幂。将其舍入并转换为整数。以二进制形式,如果 location == 0 则为00000001,如果 location == 1 则为00000010,如果 location == 2 ,等等。(更好的是 1<<位置将 1移动a一定的位数,在右边填充0位。使用 Math.pow 可能会在每次调用时尝试计算2的对数。)

Math.pow(2, location) raises 2 to the given power. This is rounded and converted to an integer. In binary, this will be 00000001 if location==0, 00000010 if location==1, 00000100 if location==2, etc. (Much better would be 1 << location which shifts a "1" by a certain number of bits, filling in 0 bits at the right. Using Math.pow will probably try to compute the logarithm of 2 every time it's called.)

n& ... 是按位AND。由于右边的项目仅设置了一位,因此其效果是将 n 中的每一位(除了该位)清零,然后将结果放入 v 。这意味着,如果 v 中的某个位在 n 中为0,则该位为0,否则该位为0。是`,表示

n & ... is a bitwise AND. Since the item on the right has just one bit set, the effect is to zero out every bit in n except for that one bit, and put the result in v. This means that v will be 0 if that one bit is 0 in n, and something other than 0 if that bit is `, which means

  return v==0?0:1;

如果该位清零,则返回0;如果该位置1,则返回1。

returns 0 if the bit is clear and 1 if it's set.

int toggle = (int) Math.pow(2, location), bv = getBitValue(n, location);

toggle 设置为 Math.pow 我已经描述过的东西。 bv 设置为 n 中已经存在的位,即0或1。如果等于将其重新设置为,则我们无需执行任何操作 n

toggle is set to that Math.pow thing I already described. bv is set to the bit that's already in n, which is 0 or 1. If this equals the thing you're setting it to, then we don't need to do anything to n:

if(bv == bit)
    return n;

否则,我们要么需要将其设置为1(请记住 toggle 只会设置一位)。 n | =切换 n = n |切换 | 是按位或,因此将在 n 中设置一个位,而在<$中设置所有其他位c $ c> n 将保持不变

Otherwise, either we need to set it to 1 (remember that toggle will have just one bit set). n |= toggle is the same as n = n | toggle. | is a bit-wise OR, so that one bit will be set in n and all other bits in n will remain the same"

if(bv == 0 && bit == 1)
    n |= toggle;

或者我们需要设置该位到0。 n ^ =切换 n = n ^切换 n 是异或运算符,如果到达这里,则 n 中的位为1,而 toggle 为1,我们想将 n 中的位设置为0,因此,异或将将该位更改为0,而其他所有位都相同:

Or we need to set the bit to 0. n ^= toggle is the same as n = n ^ toggle. n is an exclusive OR. If we get here, then the bit in n is 1, and the bit in toggle is 1, and we want to set the bit in n to 0, so exclusive OR will change that bit to 0 while leaving every other bit the same:

else if(bv == 1 && bit == 0)
    n ^= toggle;

这篇关于从图像获取/设置位值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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