简体   繁体   English

移位位Java

[英]Shifting bits java

To insert the value, start by using a mask to clear out the 8-bits of the pixel corresponding to the given color channel. 要插入该值,请先使用遮罩清除与给定颜色通道相对应的像素的8位。 For example, in the case of red, shift an 8-bit mask of ones left 16 bits, invert it (using the ~ operator), and “and” (&) this mask with the RGB value, which clears out the 8 bits of red and leaves the other bits unchanged. 例如,在红色的情况下,将一个8位的掩码左移16位,将其反转(使用〜运算符),然后用RGB值将该掩码“与”(&),以清除8位红色,其他位保持不变。 Next, shift the parameter value (red, in this case) left the same number of bits (16, in the case of red) and “or” (|) the shifted value into the pixel value. 接下来,将参数值(在这种情况下为红色)左移相同的位数(在红色的情况下为16),并将移位后的值“或”(|)转换为像素值。

int getRed(){
    red = (Pixel>>16);
    red = ~Pixel;
    red = Pixel<<16 | Pixel;
    return red;
}

What am I doing wrong according to the directions? 根据指示,我在做什么错?

The problem here seems to be a fundamental problem in understanding how assignment works (in Java ... and just about every imperative programming language!). 这里的问题似乎是理解赋值工作原理的一个基本问题(在Java中……以及几乎所有命令式编程语言!)。 For example: 例如:

red = (Pixel>>16);
red = ~Pixel;

That says: 说的是:

  1. Assign to red the value of Pixel shifted by 16 bits red Pixel值偏移16位

  2. Assign to red the value of Pixel negated bitwise. 将按位取反的Pixel值分配给red This clobbers the value of red that you calculated in the previous step. 这将破坏您在上一步中计算出的red值。

If you want to negate the value that you calculated in step 1, then you need to do this: 如果要否定在步骤1中计算出的值,则需要执行以下操作:

red = ~red;

I believe you simply don't understand what's going on. 我相信您根本不了解发生了什么。 The very first line is already incorrect: 第一行已经不正确:

it says, "in the case of red, shift an 8-bit mask of ones left 16 bits, invert it (using the ~ operator), and “and” (&) this mask with the RGB value" 它说:“在红色的情况下,将一个8位的掩码左移16位,将其反转(使用〜运算符),然后将该掩码与RGB值“和”(&)”)

Which means 意思是

8 bit mask of ones:  0xFF  (00000000 00000000 00000000 11111111)
shift left 16 bits:  0xFF << 16  (giving you  00000000 11111111 00000000 00000000)
invert it         :  ~ (0xFF << 16)  (giving you (11111111 00000000 11111111 11111111)
& this mask with RGB value:  result = pixel & (~(0xFF << 16))

The result is the pixel with 17th-24th bit cleared. result是清除了第17-24位的pixel

That's the first step (yes there are subsequent steps, as described in your homework) to set the "Red" value in pixel 这是第一步(是,如作业中所述,还有后续步骤),以pixel为单位设置“红色”值

I am not sure if it is your intention but what you developed has nothing to do with your question: instead of setting red value, it seems you are getting red value. 我不确定这是否是您的意图,但是您开发的内容与您的问题无关:不是设置红色值,而是您正在获得红色值。

However, still, what you developed is still far from correct. 但是,您开发的内容仍然不正确。 eg you should have a similar mask with 17th-24th bits being 1 and other bits being 0, then & the pixel with this mask, and then shift the remaining value (which located at 17th-24th bits) to 0-7th bits. 例如,您应该有一个相似的掩码,第17-24位为1,其他位为0,然后&带有该掩码的像素,然后将剩余值(位于第17-24位)移至第0-7位。

I am not going to give you the actual answer because it is your work to learn from it. 我不会给您实际的答案,因为从中学习是您的工作。 However I believe the hints I gave is more than enough. 但是我相信我给出的提示绰绰有余。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM