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. 4 4193
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<int>' ? 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
"Victor Bazarov" <v.********@comAcast.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<int>' ? 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\nRight 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;
}
G Iveco wrote:
"Victor Bazarov" <v.********@comAcast.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<int>' ? 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
On Jul 31, 5:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
G Iveco wrote:
"Victor Bazarov" <v.Abaza...@comAcast.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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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) ...
|
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...
|
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...
| |
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...
|
by: Xoomer |
last post by:
I want to know what problem with cin, cout, overlow operator using
shift left and right?
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |