C/C++ question

Discussion of chess software programming and technical issues.

Moderator: Ras

Cardoso
Posts: 363
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

C/C++ question

Post by Cardoso »

Hi every one,
I had a function that returned wrong values and after taking a good look at it I found the following statement:

Code: Select all

  score >> 8;
after I changed it to:

Code: Select all

  score = score >> 8;
everything turned out right, no more bugs.
So my question is why there were no warnings from the compiler, something like "missing assignment", "inconsequent statement" or something?
I use Visual Studio 2013 with the intel studio xe 2015 c++ compiler.

best regards,
Alvaro
bnemias
Posts: 373
Joined: Thu Aug 14, 2008 3:21 am
Location: Albuquerque, NM

Re: C/C++ question

Post by bnemias »

The relevant warning is off. There are lots of reasons that could happen. Check the build settings globally, and for that file. Check the header chain for warning pragmas.

Maybe the global warning level is very low. You can verify this easily by writing a small program and compiling from the cmdline, eg:

Code: Select all

int main()
{
        int score = 12;
        score << 8;
        return score;
}
AlvaroBegue
Posts: 932
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: C/C++ question

Post by AlvaroBegue »

Learn how to crank up the warning level in your compiler. With g++ this does it:

Code: Select all

$ g++ kk.cpp -o kk -O3 -std=c++11 -Wall -Wextra -pedantic
kk.cpp: In function ‘int main()’:
kk.cpp:5:9: warning: statement has no effect [-Wunused-value]
   score << 8; 
         ^
mar
Posts: 2675
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: C/C++ question

Post by mar »

use warning level 4 (/W4), can be found in C/C++, General, Warning level

Code: Select all

  score = score >> 8;
assuming score is signed, you have a little problem (or not if you don't care about subtle inaccuracy or if score is unsigned)

arithmetic shift right rounds towards -infinity (assuming 2's complement standard representation)

while this property may be beneficial depending on what you do
EDIT: with fixed point, this is useful because (score+128) >> 8 rounds to nearest integer (but one has to be careful to avoid overflow)

-1 >> 8 = -1 but +1 >> 8 = 0, so you'll get different scores (off by 1 unit) depending on color

score/256 gives the expected result, but the compiler needs to efficiently encode something like this:

Code: Select all

(score + 255*(score<0)) >> 8
of course, it will do better than literally doing this but worse than plain >>
Rein Halbersma
Posts: 752
Joined: Tue May 22, 2007 11:13 am

Re: C/C++ question

Post by Rein Halbersma »

Cardoso wrote:

Code: Select all

  score = score >> 8;
everything turned out right, no more bugs.
Equivalent code but shorter:

Code: Select all

score >>= 8; 
Most operators have a compound-assignment version, e.g. addition

Code: Select all

 score += 1; // equivalent to score = score + 1; or even better: ++score;