GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Processing
  • Download
  • Reference
  • Environment
  • Libraries
  • Tools
  • Wiki
  • Tutorials
  • Examples
  • Books
  • Forum
  • Overview
  • People
  • Privacy

This view

  • Original page
  • New address

GeezerVison is showing processing.org — >> (right shift) / Reference

Reference+
Documentation→Reference→Math→Bitwise Operators

Name

>> (right shift)

Description

Shifts bits to the right. The number to the left of the operator is shifted the number of places specified by the number to the right. Each shift to the right halves the number, therefore each right shift divides the original number by 2. Use the right shift for fast divisions or to extract an individual number from a packed number. Right shifting only works with integers or numbers which automatically convert to an integer such at byte and char.

Bit shifting is helpful when using the color data type. A right shift can extract red, green, blue, and alpha values from a color. A left shift can be used to quickly reassemble a color value (more quickly than the color() function).

Examples

  • int m = 8 >> 3;    // In binary: 1000 to 1
    println(m);  // Prints "1"
    int n = 256 >> 6;  // In binary: 100000000 to 100 
    println(n);  // Prints "4"
    int o = 16 >> 3;   // In binary: 10000 to 10 
    println(o);  // Prints "2"
    int p = 26 >> 1;   // In binary: 11010 to 1101 
    println(p);  // Prints "13"
    
  • // Using "right shift" as a faster technique than red(), green(), and blue()
    color argb = color(204, 204, 51, 255);
    int a = (argb >> 24) & 0xFF;
    int r = (argb >> 16) & 0xFF;  // Faster way of getting red(argb)
    int g = (argb >> 8) & 0xFF;   // Faster way of getting green(argb)
    int b = argb & 0xFF;          // Faster way of getting blue(argb)
    fill(r, g, b, a);
    rect(30, 20, 55, 55);
    

Syntax

  • value >> n

Parameters

  • valueint: the value to shift
  • nint: the number of places to shift right

Related

  • leftshift
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.