Solved

What is the use of the shift operator <<?

Posted on 2004-09-05
3
436 Views
Last Modified: 2010-04-01
Dear All,

I am writing the code for RGB to gray level conversion.
The NTSC standard for luminance:

I = 0.299r + 0.587g + 0.114b

While searching the web, i came across the code that does the task:

#define RGB_SCALE 14
#define cR (int)(0.299*(1 << RGB_SCALE) + 0.5)
#define cG (int)(0.587*(1 << RGB_SCALE) + 0.5)
#define cB ((1 << RGB_SCALE) - cR - cG)

//I is greyLevel output
//r is red value
//g is green value
//b is blue value

I = cR*r+cG*g+cB*b;
I = (((I) + (1 << ((RGB_SCALE)-1))) >> (RGB_SCALE));

What is the purpose of the bitwise operations involving RGB_SCALE, especially for the last line of code?

Thank you.

With Best Regards
HCK
0
Comment
Question by:hengck23
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 205 total points
ID: 11987404
In the #define's, the shifts are just to scale the values up by 2^14in order to use integer values rather than floats.  Note that cR and cG are cast back to int and cB uses integer arithmetic and does not need to be converted back.

In the final line, the value is shifted back (scaled down by 2^14) to get back to a normal integer grayscale value.  Each of RGB is an 8-bit number, and the coefficients in the grayscale calculation add up to 1, so the grayscale value is also in the range 0-256 (8 bits).  The '+ (1 << ((RGB_SCALE)-1))' part is just to round to what will be the LSB after the final right shift.

In short, this algorithmj just multiplies the original RGB values by a big number so integer math can be used to compute the grayscale value.  Then after the calculation, the big number is divided back out.  The '+ 0.5' and the '+ (2^13)' just round results.
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 20 total points
ID: 11987474
also "shift" instructions in a processor are expected to be faster than a multiply instruction
0
 
LVL 19

Assisted Solution

by:drichards
drichards earned 205 total points
ID: 11987612
The way this code is written, there is only one shift actually done in the executable code - the final right shift.  cR, cG, cB, and (1 << ((RGB_SCALE)-1)) are computed by the preprocessor and put into the code as constants.

And you would expect a shift to be much faster than a divide.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question