473,666 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Precision of C++ left/right shift operator..

I am using this type of code to do right-shifting,

B = 3;
data1 = (data + (1 << (B-1))) >B;

data1 seems incorrect when data = -4-8*i.. which means it
rounds -1.5 to -1 instead of -2.

On the positive side, 1.5 is rounded to 2, which ic correct.
For left-shift, it's simply as follows, no pitfalls, am I right?
data1 = data << B;

Thanks.

Jul 31 '07 #1
4 4204
G Iveco wrote:
I am using this type of code to do right-shifting,

B = 3;
data1 = (data + (1 << (B-1))) >B;

data1 seems incorrect when data = -4-8*i.. which means it
rounds -1.5 to -1 instead of -2.
Do you mean 'data' is declared as 'std::complex<i nt>' ? If not,
how is it declared? What's the type of 'data1'?
On the positive side, 1.5 is rounded to 2, which ic correct.
Are you sure it's -1.5? What if it's -1.49999999999?
For left-shift, it's simply as follows, no pitfalls, am I right?
data1 = data << B;
Please post the complete program (not all of your code, but
something we can just compile and run to see the behaviour).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #2

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:f8******** **@news.datemas .de...
>G Iveco wrote:
>I am using this type of code to do right-shifting,

B = 3;
data1 = (data + (1 << (B-1))) >B;

data1 seems incorrect when data = -4-8*i.. which means it
rounds -1.5 to -1 instead of -2.

Do you mean 'data' is declared as 'std::complex<i nt>' ? If not,
how is it declared? What's the type of 'data1'?
>On the positive side, 1.5 is rounded to 2, which ic correct.

Are you sure it's -1.5? What if it's -1.49999999999?
Thank you for reply.

data and data1 are integer. Herer I use -1.5 to mean
-12.0/8 = -1.5, when right-shift, i was expecting -2.0 instead of -1.0..

same goes with 1.5, which means 12.0/8.0 and yield 2, as expected.

>For left-shift, it's simply as follows, no pitfalls, am I right?
data1 = data << B;

Please post the complete program (not all of your code, but
something we can just compile and run to see the behaviour).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
My complete source is simply a test of ideas.. as follows,

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i, j;
int data;
double tmp;
int data0;
int data1;
int data2;
FILE *fp;

fp = fopen("./data.txt", "w");
int KK = -153;
int B = 5;
fprintf(fp, "\n\n\nRigh t shifting, division by 2^B");
for(i=0; i<300; i++) {
data = KK + i;
tmp = data * 1.0 / 32.0;
data0 = (int)tmp;
data1 = (data + (1 << (B-1))) >B;
data2 = (data >B) + ((data >B-1) & 0x1);
fprintf(fp, "\ndata = %2d, tmp = %3.2f, data1 = %3d, data2 = %3d", data,
tmp, data1, data2);
}

fprintf(fp, "\n\n\nLeft shifting, multiply by 2^B");
for(i=0; i<300; i++) {
data = KK + i;
tmp = data * 8.0;
data0 = (int)tmp;
data1 = data << 3;
fprintf(fp, "\ndata = %2d, tmp = %3.2f, data1 = %3d, data0 = %3d", data,
tmp, data1, data0);
}
fclose(fp);
system("PAUSE") ;
return EXIT_SUCCESS;
}
Jul 31 '07 #3
G Iveco wrote:
"Victor Bazarov" <v.********@com Acast.netwrote in message
news:f8******** **@news.datemas .de...
>G Iveco wrote:
>>I am using this type of code to do right-shifting,

B = 3;
data1 = (data + (1 << (B-1))) >B;

data1 seems incorrect when data = -4-8*i.. which means it
When you wrote 'data = -4-8*i' I took it that it's "complex" and
its real part is -4 and imaginary part is -8 (*i is the notation
to indicate the formula of a compex number 'a+b*i'). Since your
code didn't have any 'i', there was no other way for me to
interpret your "-4-8*i" expression.
>>rounds -1.5 to -1 instead of -2.

Do you mean 'data' is declared as 'std::complex<i nt>' ? If not,
how is it declared? What's the type of 'data1'?
>>On the positive side, 1.5 is rounded to 2, which ic correct.

Are you sure it's -1.5? What if it's -1.49999999999?

Thank you for reply.

data and data1 are integer. Herer I use -1.5 to mean
-12.0/8 = -1.5, when right-shift, i was expecting -2.0 instead of
-1.0..
WHY??? Right shift does not round. It discards the bits that are
shifted "out of existence".

I am guessing you're confused about shifting, division (those are
two separate operations), and how it is all done for integers versus
doubles. You need to read up on integral operations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '07 #4
On Jul 31, 5:36 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
G Iveco wrote:
"Victor Bazarov" <v.Abaza...@com Acast.netwrote in message
data and data1 are integer. Herer I use -1.5 to mean
-12.0/8 = -1.5, when right-shift, i was expecting -2.0 instead of
-1.0..
WHY??? Right shift does not round. It discards the bits that are
shifted "out of existence".
Actually, right shift isn't well defined for negative numbers.
It's implementation defined whether it shifts in the sign or a
0. And of course, the results (either way) depend on the
representation of negative numbers.

But whatever. As you say, right shift deals with bits (or the
bit representation) and not the numeric values. If you want to
deal with the numeric values, use the numeric operators.
(Except that in the current version of the C++ standard, which
way division rounds when the results are negative is
implementation defined as well.)
I am guessing you're confused about shifting, division (those are
two separate operations), and how it is all done for integers versus
doubles. You need to read up on integral operations.
Exactly.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7842
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
4
13050
by: Kevin | last post by:
I was looking through some source code and noticed the used of the C# << operator. Why is this being used here and under what circumstances is an left-shift operator useful. internal enum InterestLevel { Ignore = 0, Display = 1<<0, Interesting = 1<<1, Parents = 1<<2,
4
4278
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) requires unsigned bits...so how do i go thro this ,since VB does not support unsigned operations Post your suggestions!!!
56
15328
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And how can I get rid of that?
1
1686
by: marsguy85 | last post by:
a program that repeatedly request an integer from the user and displays the integer as a binary number. It should terminate when the value 9999 is entered. A typical run might look like: Enter integer: 16 00000000000000000000000000010000 Enter integer: -1 11111111111111111111111111111111 Enter integer: 9999 Try various values including negative and check that they are correct – the storage of signed numbers in binary was discussed in the...
16
3717
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this solution for the question:
1
1346
by: Xoomer | last post by:
I want to know what problem with cin, cout, overlow operator using shift left and right?
0
2556
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23 significand bits (with an implicit leading 1, for 24 total), and 8 exponent bits double precision: 64 bits including 1 sign bit, 52 significand bits
8
2075
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as a char* mantissa, an int exponent and a sign. For example, the number "123.45" would have a mantissa of 12345, an exponent of 2, and a sign of +. I'm essentially storing it as "1.2345x10^2". The mantissa is allocated dynamically at runtime to...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.