unary minus operator applied to unsigned type, result still unsigned

Hi, i want to create some poker software and first i need hand equity calculation and i found that best could be open source project Pokerstove. So i downloaded all the source code and included all the headers and cpp files in resource files folder. Than i tryed to compile everything to see if its working and i got this error


Error 12 error C4146: unary minus operator applied to unsigned type, result still unsigned


Compiler ponts to the plases where all the uint64_t , uint32_t and uint16_t are used

Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef __LASTBIT_H
#define __LASTBIT_H

#include "utypes.h"

inline uint firstbit (uint64_t v)
{
  //unsigned int v;  // 32-bit value to find the log2 of 
  // TODO: find utype identifier for 8-byte int (uL may not be portable)
  const uint64_t b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000uLL};
  const unsigned int S[] = {1, 2, 4, 8, 16, 32};
  int i;

  register unsigned int r = 0; // result of log2(v) will go here
  for (i = 5; i >= 0; i--) // unroll for speed...
    {
      if (v & b[i])
        {
          v >>= S[i];
          r |= S[i];
        } 
    }
  return r;
}

inline int lastbit (uint32_t v)
{
  int r;
  static const int MultiplyDeBruijnBitPosition[32] = 
    {
      0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 
      31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
    };
  r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
  return r;
}

inline int lastbit (uint16_t v)
{
  int r;
  static const int MultiplyDeBruijnBitPosition[32] = 
    {
      0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 
      31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
    };
  r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
  return r;
}

inline int lastbit (uint64_t v)
{
  if (v)
    {
      uint32_t lower = static_cast<uint32_t>(v&0xFFFFFFFF);
      if (lower)
        return lastbit(lower);
      uint32_t upper = static_cast<uint32_t>
        ((v & 0xFFFFFFFF00000000uLL)>>32);
          
      //uint32_t upper = static_cast<uint32_t>((v>>32)&0xFFFFFFFF);
      return lastbit(upper)+32;
    }
  return 0;
}

// this one is slow on XP 32
inline int lastbit64 (uint64_t v)
{
  int r;           // put the result in r
  static const int Mod67BitPosition[] = // map a bit value mod 37 to its position
    {
      0,   0,  1, 39,  2, 15, 40, 23,  3, 12, 16, 59,
      41, 19, 24, 54,  4,  0, 13, 10, 17, 62, 60, 28,
      42, 30, 20, 51, 25, 44, 55, 47,  5, 32,  0, 38,
      14, 22, 11, 58, 18, 53, 63,  9, 61, 27, 29, 50,
      43, 46, 31, 37, 21, 57, 52,  8, 26, 49, 45, 36,
      56,  7, 48, 35,  6, 34, 33,
    };
 r = Mod67BitPosition[(-v & v) % 67];

 //cout << (-v&v)%37 << " " << lastbit(v) << endl;
//  cout << (-v&v)%67 << " " << lastbit(v) << " " << r << endl;
//  if (lastbit(v) != r)
//  cout << "fuck\n";
 
 return r;
}

#endif


and code of utypes.h what lastbit.h is including

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef __UTYPES_H
#define __UTYPES_H

#include <string>
#include <boost/cstdint.hpp>

// extract exactly these types globally
using boost::uint64_t;
using boost::uint32_t;
using boost::uint16_t;
using boost::int64_t;
using boost::uint8_t;

typedef unsigned int  uint;
//typedef unsigned char ubyte;

const uint64_t ONE64 = 1;

/**
 * Produce a string corresponding to the bits in the data. 
 * A space separates every 8 bits. 
 */
template <typename T>
std::string toBitString (T t)
{
  std::string ret;
  const int BYTE_SIZE = 8;
  for (unsigned int i=0; i<sizeof(T)*BYTE_SIZE; i++)
    {
      if ( (i%BYTE_SIZE) == 0 )
	ret += " ";
      if (t>>i & 0x01)
	ret += "1";
      else
	ret += "0";
    }
  return ret;
}

/**
 * Count the number of bits that are on in the input.  This may need
 * to be fixed to work with unsigned input
 */
#if 1

template <typename T>
int countbits (T v)
{
  size_t c;
  for (c = 0; v; c++)
    {
      v &= v - 1; // clear the least significant bit set
    }
  return c;
}


#else
template <typename T>
int countbits (T x)
{
  int rval = 0;
  while (x != 0)
    {
      if (x & 0x01)
        rval++;
      x >>= 1;
    }
  return rval;
}
#endif

std::string makeCommaNumStr (int64_t n);

#endif


Im using Visual studio 2013 and im creating an empty project. I saw that on top of the visual studio i can shoose to debug in 2 modes - Win 32 and x64 so i tryed them both and still this.
Im using 64 bit Win 7 Home premium in case someone needs to know.

I already tryed to build my own program that calculates hand equity but i got one result with 1% precision only in about 1 second so would be really cool if i could get this runing. Thanks
Last edited on
which line gives you this error?
Technically this should not be an error as results are well-defined.
There probably is switch which stops VS from giving you an error on that. In the meantime you can use (~x + 1) instead of -x
which line gives you this error?

Those are lines 34 and 79 . And there are multiple errors (the same one) of that single line

Thanks a lot for so quick answer. Ill try to use (~x + 1) instead of -x like u suggested
Slight fix: use (~x + 1u) to be sure.
Alternatively turn off "Threat warnings as errors" feature: http://stackoverflow.com/a/2520873
You might want just turn off that specific warning, but you will have to find how to do that yourself.
I tryed to use the previous one and i didnt get those errors anymore but i will change it to this one :) But i have new error but i think i know how to fix it but i dont kknow where to write that line of code

This error points to some weird file that i didnt even include myself - its called
xutility
and its super long, full with templates and stuff.

Error is this :
Error 3 error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'

I search in google a bit and i saw that this line
#pragma warning(disable: 4996) could disable this warning. But where should i put it?

Disable "threat warnings as errors" first. Trying to fix all potential warnings in third-party projects does not worth it.

You need to place that line before line where warning originates (it is not xutility: that is merely end of long chain of inclusions)
Threat warnings as errors was already disabled

Maybe that waring might be coz of (~x + 1) instead of (~x + 1u)?
PS - i cant modify that file that gives me this error. Also i changed (~x + 1) to 1 more file and i cant find where it was =/ So i cant check if this would solve the error just yet
Last edited on
Threat warnings as errors was already disabled
It still enabled somewhere: all your problems should be warnings, not errors.

You can do what error message suggests and define _SCL_SECURE_NO_WARNINGS
https://msdn.microsoft.com/en-us/library/aa985974.aspx
Could it work if i include
1
2
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable:4996) 

In all of my header files?
It is better to pass it to the compiler options.
https://msdn.microsoft.com/en-us/library/hhzbb5c8.aspx
I included
1
2
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(disable:4996)  

in every possible place i could and it actually worked. But i will do the 2nd thing u told just to follow good example. I havent yet checked out if all the functions are working but we will see :)

Thanks you big time for helping me out again. This isnt the 1st time :) Thank you sooooooooo much!!!!!
Topic archived. No new replies allowed.