Search This Blog

Saturday, December 22, 2012

Shift Operators: >> and <<


expression << expression 
expression >> expression
The bitwise shift operators are as follows:
  • Right shift (>>)
  • Left shift (<<)
Both operands of the shift operators must be of integral types. Integral promotions are performed according to the rules described in Integral Promotions. The type of the result is the same as the type of the promoted left operand. The value of a right-shift expression x >> y is x / 2y, and the value of a left-shift expression x << y is x * 2y.
The result is undefined if the right operand of a shift expression is negative or if the right operand is greater than or equal to the number of bits in the (promoted) left operand. No shift operation is performed if the right operand is zero (0).
The left-shift operator causes the bit pattern in the first operand to be shifted to the left by the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled. This is a logical shift instead of a shift-and-rotate operation.
The right-shift operator causes the bit pattern in the first operand to be shifted to the right by the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled for unsigned quantities. For signed quantities, the sign bit is propagated into the vacated bit positions. The shift is a logical shift if the left operand is an unsigned quantity; otherwise, it is an arithmetic shift.

Microsoft Specific

The result of a right shift of a signed negative quantity is implementation-dependent. Although Microsoft C++ propagates the most-significant bit to fill vacated bit positions, there is no guarantee that other implementations will also do so.
// expre_Shift_Operators.cpp
// compile with: /EHsc
// Demonstrate shift operators
#include 
using namespace std;

int main() {
   cout   << "5 times 2 is " << (5 << 1) << endl
         << "20 divided by 4 is " << (20 >> 2) << endl;
}
Output
5 times 2 is 10
20 divided by 4 is 5

Fibonacci Sequence Calculator

Fibonacci Sequence Calculator
 

The term of the Fibonacci sequence is .
 
The Fibonacci sequence is the sequence of integers 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,... It is defined by
For example, 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8, etc.
The above calculator computes the nth term of the Fibonacci sequence for n ≤ 1474 and will subsequently compute the previous and next terms. It uses Binet's formula,
which is an explicit formula for the nth term of the Fibonacci sequence. StatCounter - Free Web Tracker and Counter

Understanding and Using Floating Point Numbers

Numbers are surely the most prevalent kind of data in computer programs. They are so fundamental that people don't spend much time talking about them—surely everybody knows how to use numbers in their programs. Well, one of the wonderful things about programming is that nearly everywhere you look, you find more than meets the eye.


difference between float and double

difference between float and double

In c++ any value like 4.12 is treated as a double by default.

Comparing floats and doubles can be a tricky business because of the difference in precision leading to minute errors. For example:

float a = 4.12;

if(a==4.12)
{
   cout<<"hello";
}
else
{
    cout<<"bye "<
} 


This will show you the output as "bye 4.12" 



Why?



Because by default 4.12 is a double (such as in the if statement or in the assignment 
to our variable), but storing it in a float it loses some precision, and so comparing 
then comparing a double with a float lead to microscopic changes in the precision of 
the number--remember that floats and doubles are not precise. 



Two lessons here: one is that floating point numbers shouldn't be compared directly 
most of the time, and the other is that the default size and type of a hard-coded 
floating point number is double.



For more on floating point numbers, read understanding floating point numbers--accuracy and precision.