Solved

What is the use of the shift operator <<?

Posted on 2004-09-05
3
384 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now