Problems understanding java syntax

Duplicate topics... posts merged

Page 1 of 1

6 Replies - 3932 Views - Last Post: 21 December 2008 - 08:24 AM Rate Topic: -----

#1 Nite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Problems understanding java syntax

Posted 20 December 2008 - 09:15 AM

Hi,
referring to the page of the class PixelGrabber in the java 6 api page, http://java.sun.com/...xelGrabber.html ,
i would like to have some clarifications on the following syntax, taken from the above webpage :

 public void handlesinglepixel(int x, int y, int pixel) {
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel	  ) & 0xff;
		// Deal with the pixel as necessary...
 }



What does it mean the operator >> in this case? and what about "& 0xff" ?


Thanks for help,


Nite

Is This A Good Question/Topic? 0
  • +

Replies To: Problems understanding java syntax

#2 Nite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Problems understanding java syntax

Posted 20 December 2008 - 09:25 AM

Hi,
referring to the page of the class PixelGrabber in the java 6 api page, http://java.sun.com/...xelGrabber.html ,
i would like to have some clarifications on the following syntax, taken from the above webpage :

 public void handlesinglepixel(int x, int y, int pixel) {
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel	  ) & 0xff;
		// Deal with the pixel as necessary...
 }



What does it mean the operator >> in this case? and what about "& 0xff" ?


Thanks for help,


Nite
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Problems understanding java syntax

Posted 20 December 2008 - 09:47 AM

that sign means it shifts bits to the right with the sign extension. Its a bitwise operator and the way the signs points is the shift direction.
In your case, it is shifting the number of bits 'n' to the right 'p' positions. So the amount of bits in your variable pixel are being shift to the right 8, 16 and 24 times

This post has been edited by nick2price: 20 December 2008 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problems understanding java syntax

Posted 20 December 2008 - 10:01 AM

View PostNite, on 20 Dec, 2008 - 08:25 AM, said:

Hi,
referring to the page of the class PixelGrabber in the java 6 api page, http://java.sun.com/...xelGrabber.html ,
i would like to have some clarifications on the following syntax, taken from the above webpage :

 public void handlesinglepixel(int x, int y, int pixel) {
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel	  ) & 0xff;
		// Deal with the pixel as necessary...
 }



What does it mean the operator >> in this case? and what about "& 0xff" ?


Thanks for help,


Nite

The >> is the right shift operator. (pixel >> 24) means shift the integer pixel 24 bits to the right. The result is the same as dividing pixel by 2**24. Left shift << is the same as multiplying by 2**shift.

See Bitwise and Bit Shift Operators

int alpha = (pixel >> 24) & 0xff; The 0xff is a hexadecimal literal with a value of 255 decimal. Since an 8-bit byte can represent binary values of zero to 255 with zero meaning no bit in the byte is a one and 255 meaning that all 8 bits are set to one. The statement int alpha = (pixel >> 24) & 0xff; is shifting the contents of pixel 24 bit positions to the right and masking off the low byte of pixel by ANDing the mask with the shifted contents. This is effectively masking off the high byte of the original contents of pixel. As the other statements show, each of the 8-bit values for red, green and blue colors as well as the alpha mask are being separated from the composite pixel.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problems understanding java syntax

Posted 20 December 2008 - 10:04 AM

Please do NOT double post.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Problems understanding java syntax

Posted 20 December 2008 - 09:56 PM

Pixels are defined in a int (32 bits) where:
0xAABBCCDD

AA represent the Alpha, the transparency
BB the Red RGB color
CC the Green RGB color
DD the Blue RGB color

the >> are to shift the int to the right to isolate in the rightmost bits the 8 bits of these elements
the & 0xFF is an AND to set to 000000 the leftmost part of the int and so to isolate the Alpha or the color
Was This Post Helpful? 0
  • +
  • -

#7 Nite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Problems understanding java syntax

Posted 21 December 2008 - 08:24 AM

View Postn8wxs, on 20 Dec, 2008 - 09:04 AM, said:

Please do NOT double post.


I'm sorry, that was just due to the slow connection here.

Thanks to everyone for the good help by the way :)


Nite
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1