Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What is the use of the shift operator <<?

Posted on 2004-09-05
3
Medium Priority
?
492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 820 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 80 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 820 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

604 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