public int getRGB(Object inData) {
return (getAlpha(inData) << 24)
| (getRed(inData) << 16)
| (getGreen(inData) << 8)
| (getBlue(inData) << 0);
}
So, what does this return statement actually do? Four ints are shifted, but what is returned?
It returns an int whose first (MSB) byte is the Alpha value, its 2nd byte is the Red value, its 3rd byte is the Green value and its last byte is the Blue value.
highest lowest
bit bit
|--------|--------|--------|--------|
Alpha Red Green Blue
(8 bits) (8 bits) (8 bits) (8 bits)
What you get is the following :
Alpha | Red | Green | Blue - an 32 bit ARGB value - 8 bits for each. As you can see, alpha is shifted 24 bits to the left (leftmost - most significant bits), after which comes the red, with 8 bits, thus placing red in second first 8 bits and masking the remaining 16. Afterward, green is shifted with 8 bits, masking the last byte and finally, blue is put in its place.
| is bitwise OR with strict evaluation, in contrast to ||, which may stop evaluation of the statements in the condition as soon as one expression returns true. But these methods (presumably) return integers, so in Java you (to my knowledge) cannot test these against true directly anyway. No problem since that is not what you are doing - you are shifting the individual results from the methods, filling up new bits with 0 and cutting off the old ones. What this achieves is to pack the (presumably) at most byte-sized, positive values (0 to 255) in one of the 4 bytes which make up an integer. Essentially this is packing 4 pieces of information which require one byte each into one variable of type integer. The type of the target variable could be anything, as long as it has enough bytes to store the information, but it gets more sloppy and questionable from there.
Related
I have a very simple question: I want to know what is the meaning of this java operation. I'm a beginner in java and can't understand what it does. I know that it is a packed int storing a color
(alpha << 24) | (red << 16) | (green << 8) | blue
Another question: I want to go from a 0xFFFFFFFF value to a RGB type color, how can I separate this packed int into RGB channels?
Regarding the first question:
The expressison is loading four 8-bit quantities into a 32-bit int. Although generally this kind of boolean manipulation is done with unsigned ints to avoid complications I won't go into here.
The line you're showing is shifting the alpha component's value (e.g. its 8-bits) 24 bits to the left, to create a result which has alpha occupying the high-order byte of a 4-byte (32-bit) integer.
Then it shifting the red component 16 bits to the left (increasing its numeric value binary orders of magnitude), and logically ORing the result of that shift operation into the 32-bit integer that alpha was stored in, merging in the value, to occupy the 3rd byte. The result at that point will be that the top two bytes will have the alpha and red values, and so on.
Regarding the second question:
You can invert the procedure and use boolean operators:
unsigned int v = 0xff557788;
int blue = v & 0xff;
int green = v >> 8 & 0xff;
int red = v >> 16 & 0xff;
int alpha = v >> 24 & 0xff;
In this example we're shifting the field right in multiples of 8 to get the different bytes into the lowest byte, and then masking (e.g. logically ANDing with) 0xff to get a value that excludes the high order 3 bytes.
I am trying to understand the command "someByte << 2" in java. For what is it for? At the iscsi docmentation there is a caching mode page saying about DEMAND READ RETENTION PRIORITY and WRITE RETENTION PRIORIRY.
at the source there is this code for these messages:
// serialize byte 3
b = (byte)((demandReadRetentionPriority << 4) | writeRetentionPriority);
buffer.put(b);
Why do they use "<< 4" command with demandReadRetentionPriority and not with writeRetentionPriority? And what does << means in that case?
Thanks.
You can see from the documentation that the demandReadRetentionPriority is in the upper 4 bits (bits 7,6,5, and 4) of the byte and writeRetentionPriority is stored in the lower 4 bits (3,2,1, and 0) of the byte.
The code you provided is simply shifting the value stored in the demandReadRetentionPriority variable to the upper 4 bits. The << is a bit shift operation.
For example, if the value of demandReadRetentionPriority were 1 then it would be shifted 4 bits and the byte would have a binary representation as follows:
00010000
And in order for one of the lower bits of b to be set to 1, the corresponding bit in writeRetentionPolicy would have to also be set to 1, since the lower 4 bits of demandReadRetentionPolicy will be 0 after the bit shift.
<< is the "Signed left shift" operator, a bit shifting operator.
Example:
You have stored the number 279 that would be 100010111 in decimal. When you shift 4 steps to the left you get 1000101110000 (2224) because it will "move" the decimal number to the left and fill the spaces with zeroes.
100010111 << 4
=> 1000101110000
++++
Shifting operations are very fast because they are usually implemented in the hardware as a single machine instruction.
| is also an operator on the bit-level: The bitwise inclusive OR.
Summary of operators in java
I am working on a small project. During the searching on google I see some code in java .
int red = (pixel >> 16) & 0x000000FF;
But I do not understand what is the meaning of that ? CAn anybody explain summary of that logical operation ? I read that it is shifted sth like that but shift of what ?
The red component of a color is stored as a number between 0-255, but "packed" into an integer. Here's my ASCII art, in binary, 32 bits per integer, showing the 8 bits each for Alpha,Red,Green,and Blue. Read the bit number vertically! I'm numbering from 1, not 0. (Also, you can argue that the numbers go the wrong direction - they are there just to help you count!)
11111111112222222222333
12345678901234567890123456789012
AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB the pixel value
The pixel >> 16 will shift the result right by 16 bits. In effect, this removes the G and B stuff
AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB move everything 16 to the right, this becomes
----------------AAAAAAAARRRRRRRR
where the - depends on sign. You can look it up but in this case it doesn't matter.
So, you have your Red number, almost. There is still the Alpha component, all that AA.... If you mask ("and") with 0xFF, which is the lowest 8 bits
----------------AAAAAAAARRRRRRRR (result of the bit shift)
00000000000000000000000011111111 (0xFF)
you get
000000000000000000000000RRRRRRRR
which is the result desired, a number between 0 and 255.
This is a bitwise "AND" operator. When it is applied to the number FF (hex) it clears all bits of an int except the final 8.
Recall that bitwise "AND" goes through a binary representation of a number, and puts ones in the result only in positions where both operands have ones. Since a 32-bit mask containing FF looks like this in binary
00000000000000000000000011111111
the upper three bytes of the result are going to be zeros. The last byte, where FF has all ones, will be equal to the last byte of the first operand.
& - bitwise AND operator
>> - right shift operator - shifts a bit pattern to the right
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
For example
4 in binary 100, if you do >>1 that would be 010 which is 2
8 in binary 1000, if you do >>1 that would be 0100 which is 4
See also
What are bitwise shift (bit-shift) operators and how do they work?
I have a simple question - I need to write a function for my program to change the 3rd bit of a given byte.
I wrote those lines :
public byte turnOn(Byte value)
{
int flag = 8;
value = (byte) (value | flag);
return value;
}
I'm not sure if it's the right way to do that, because I saw also this way (with which I am unfamiliar)
value = (byte) (value | (1 << 2) );
which way is better, and what does 1 << 2 means (2 means the third bit, but what is the 1 )
Thanks!
1 << 2 means 1 shifted two bits to the left. Since shifting left by one bit is similar to multiplying by two, this gives 4. In binary, this is
00000100
i.e. the 3rd bit from the right is set.
The constant 1 is used since that number only has a single bit set - the rightmost bit. After shifting left, only the 3rd bit (from the right) is set:
00000001 original value
00000010 after shifting left once
00000100 after shifting left again
I prefer using 1 << 2 instead of a constant like 8, as it makes it clearer which bit is being set. It also prevents you inadvertently using a constant that has multiple bits set - unless you actually want that, of course. Even then, it's clearer in my opinion to add together several bits, for clarity:
final int bitsToSet = (1 << 2) + (1 << 5);
4 (or 1 << 2) is 00000100 in binary¹. ORing with this mask sets the third least significant bit (or the fifth significant bit in a byte).
8 (or 1 << 3) is 00001000 in binary, so you're setting the fourth least significant bit (or the fifth one of a byte).
It does not matter which expression you use, the shifting just makes it clear you're using a bitmask. Alternatively, you can use the hexadecimal 0x04 which is (imho) easier to translate to the binary bitmask.
1 The leading zeros do not change the value, but should simplify counting the position of the set bit in a byte.
(1 << 2) will left-shift the value 1 twice. Generically, (x << y) means x * (2 ^ y). So 1 << 2 is 4.
Generally speaking, it should not matter whether you use the bit-shift or bit-set method. The compiler should optimize either way.
That said, are you looking for the 3rd bit, indexed from 0 or indexed from 1? If you're looking for the third-right-most bit starting at index 1, you want your flag to be 4 instead of 8. Additionally, the | operator is the set-value operator. If you literally want to "change" the bit, you want to use the ^ operator -- bitwise XOR -- which is the toggle-value operator.
Does that make sense?
I can't find out what << means in Java because I can't search for it on Google - I am absolutely lost!
The code in question is:
public int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
It's taken from: http://java.sun.com/docs/books/tutorial/essential/concurrency/example/ImmutableRGB.java
I would really appreciate someone telling me, thanks!
Left shift of the bits
If red == 4 (which in binary is: 00000100) then red << 16 will insert sixteen 0-bits at its right, yielding: 000001000000000000000000 which is 262144 in decimal
Q. What is this?
A. An "operator"
Q. How do I get to know about operators in java?
A. Google for "Java operators"
And the result is this:
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
Left shift a number of bits. It is equivalent to multiplying by two that many times.
It's used for setting specific bits in a byte, or specific bytes in a word.
It is the left shift operator. Here's some more information on shift operators from the Java Tutorial.
In your example code the three integer values: red, green and blue will typically have values 0-255. Hence, it is possible to combine these values and represent them as a single integer by shifting the red value by 16 bits, shifting the green value by 8 bits and then performing a bitwise-OR operation to combine the values.
its a bit shift. search for operators java, it will return you detailed explanations.
"<<" means left shift the bits of a value.
">>" means right shift the bits of a value.
example:
int a = 5; //the binary value of 5 is 101
a = a << 3; //left shift 3 bits on 101, 101 000<< add 3 bits(0) on the right, become '101000'
System.out.println(a); //this will display 40, the decimal for '101000'
int b = 9; //the binary value of 8 is 1001
b = b >> 3; //right shift 3 bits on >>000 1001 add 3 bits(0) on the left, truncate the last 3 bits on the right become '001'
System.out.println(b); //this will display 1, the decimal for '001'
Its a left bit shift
Its left shifting and convert Red, Green, Blue into 24 bit Number