Solved

What is the use of the shift operator <<?

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 118
How to split this in C++ 4 102
Autosar OS Multicore Share Resources confusion ? 2 37
DCT of  2D array using fftw in c++ 9 39
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

770 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