Thread: comparison between signed and unsigned

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    2

    comparison between signed and unsigned

    Hi, sorry if i post in the wrong place but i need some little help.. i don't know what to do here , im confuse
    i have this error :
    Code:
    safebox.cpp: In member function 'CItem* CSafebox::GetItem(DWORD)':
    safebox.cpp:164: warning: comparison between signed and unsigned integer express  ions
    safebox.cpp: In member function 'bool CSafebox::MoveItem(DWORD, DWORD, BYTE)':
    safebox.cpp:183: warning: comparison between signed and unsigned integer express  ions
    safebox.cpp:183: warning: comparison between signed and unsigned integer express  ions
    and here is the lines for where warning come
    comparison between signed and unsigned-7c8cd501520ef8e91d268cebb597a047-png

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    When comparing integers, the underlying cpu operations compare only two unsigned representations or two signed representations. So you need to explicitly choose the reinterpretation of one of the values or the system will choose for you, possibly yielding unexpected results.
    Code:
    unsigned u =  0;
    int      i = -1;
    
    if (u > i) // If I asked you if zero is greater than negative 1 you would say yes.
               // So you might assume it implicitly means int(u) > i.
               // But instead it means u > unsigned(i), and the small negative
               // value is interpreted as a large positive value.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        unsigned u =  0;
        int      i = -1;
        // the uppercase strings are printed
        if (    u  >          i ) cout << "yes\n"; else cout << "NO\n";
        if (int(u) >          i ) cout << "YES\n"; else cout << "no\n";
        if (    u  > unsigned(i)) cout << "yes\n"; else cout << "NO\n";
    }
    In your code, DWORD is probably unsigned and therefore the other variable is signed. If you are sure that the signed variable will not be negative, then you can interpret it as an unsigned value. If it's possible for the signed variable to be negative then you could interpret the unsigned value as signed.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    When casting you might get over- / underflow problems. To avoid this problem the GSL has a type gsl::narrow that detects this problem.
    GitHub - gsl-lite/gsl-lite: gsl-lite – A single-file header-only version of ISO C++ Guidelines Support Library (GSL) for C++98, C++11, and later

    Another option would be to use boost::numeric_cast
    Boost Numeric Conversion Library - numeric_cast

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    A small note on integers representation: In math the ℤ domain is an extension of ℕ domain... The ℕ domain is a set of integer values from 0 to +infinity... We use a special notation to extend ℕ below zero adding the '-' symbol in front of the natural number.

    As you know, binary representation uses only 0's and 1's. There is no root to use a '-' symbol (because this '-' is just a notation). So we can mimic this symbol using the most significant bit as if it is que '+' (0) or '-' (1) symbol, which gives us the 'signed integers'. But there are 3 ways to use this trick. Negative numbers could be:

    1 - the signal in the msb plus the positive value of lesser bits;
    2 - if the msb is 1, we can invert all the bits to get the positive part;
    3 - We can use the msb as if it is a negative power of 2, added to the lesser bits (which will be positive).

    C (and I think, C++) admits all three possibilities, but I never seen a processor using (1) or (2). The third representation is called "two's complement".

    So... all data in your computer is stored as BYTES (by definition, unsigned). The way the compiler will deal with "signed" depends on the type used. Comparisons with signed values takes care of this msb. With unsined values, doesn't. Notice that adding values are **unsigned**, always (multiplications aren't)... In two's complement when you write -1 you get all bits set, so 1 + -1, using a byte as type, is 0b00000001 + 0b11111111.

    Since unsigned integers have more precision (more signiticant bits) than signed integers of the same size, C (and C++) promotes signed integers to unsigned before doing an arithmentic operation when both types are used. It is the same thing as saying signed integers will be seen as unsigned... Then:
    Code:
    unsigned int x;  // initialized somewhere!
    
    if ( x < 0 ) ...
    Has no meaning, since 0 (signed int) will be converted to unsigned int **and** 'x' cannot be less then 0. The compiler can warn you about this because it will strip this if from the code (because is an impossible condition to match).
    Last edited by flp1969; 02-05-2022 at 07:38 AM.

  5. #5
    Registered User
    Join Date
    Feb 2022
    Posts
    2

    i have another problem

    ty you for help me, that help me alot buut i ahve another problem right now.. this is my problem

    comparison between signed and unsigned-5c82f81c2c9abfc0985e636128e2e3ee-png
    here is the lines for the problems
    offline_privateshop.h:
    Code:
    #pragma once#include "vid.h"
    #include "entity.h"
    #include "sectree_manager.h"
    #include "../common/tables.h"
    #include "packet.h"
    
    
    class CPremiumPrivateShop : public CEntity
    {
    	public:
    		CPremiumPrivateShop(LPSHOP pkShop, DWORD dwTime, DWORD dwRace);
    		virtual ~CPremiumPrivateShop(void);
    
    
    		void Initialize(void);
    		void Destroy(void);
    
    
    		DWORD GetRaceVnum() { return m_dwRaceVnum; }
    		DWORD GetVid() { return m_dwVid; }
    		DWORD GetTime() { return m_dwEndTime; }
    		DWORD GetShopVid() { return m_dwShopVid; }
    		DWORD GetPlayerID() { return m_dwPlayerID; }
    		std::string GetOwnerName() { return m_strOwnerName; }
    		LPSHOP GetShop() { return m_pkShop; }
    		void SetShopVid(DWORD dwShopVid) { m_dwShopVid = dwShopVid; }
    		void SetSign(const std::string& strSign) { m_strSign = strSign; }
    		void SetOwnerName(const std::string& strOwnerName) { m_strOwnerName = strOwnerName; }
    		void SetPlayerID(DWORD dwPlayerID) { m_dwPlayerID = dwPlayerID; }
    
    
    	protected:
    		virtual void EncodeInsertPacket(LPENTITY ent);
    		virtual void EncodeRemovePacket(LPENTITY ent);
    
    
    		DWORD m_dwRaceVnum;
    		DWORD m_dwVid;
    		DWORD m_dwEndTime;
    		DWORD m_dwStartTime;
    		DWORD m_dwShopVid;
    		DWORD m_dwPlayerID;
    		LPSHOP m_pkShop;
    		std::string m_strSign;
    		std::string m_strOwnerName;
    };
    and here is the other
    offline_privateshop.cpp
    Code:
    #include "stdafx.h"#include "offline_privateshop.h"
    #include "char_manager.h"
    #include "shop.h"
    #include "desc.h"
    #include "db.h"
    
    
    CPremiumPrivateShop::CPremiumPrivateShop(LPSHOP pkShop, DWORD dwTime, DWORD dwRace) : 
    			m_pkShop(pkShop), m_dwEndTime(dwTime), m_dwRaceVnum(dwRace), m_dwVid(CHARACTER_MANAGER::instance().AllocVID())
    {
    	Initialize();
    }
    
    
    CPremiumPrivateShop::~CPremiumPrivateShop(void)
    {
    	Destroy();
    }
    
    
    void CPremiumPrivateShop::Initialize(void)
    {
    	CEntity::Initialize(ENTITY_SHOP);
    	m_dwShopVid = 0;
    	m_dwPlayerID = 0;
    	m_strSign = "";
    	m_strOwnerName = "";
    	m_dwStartTime = time(0);
    }
    
    
    void CPremiumPrivateShop::Destroy(void)
    {
    	CEntity::Destroy();
    
    
    	if (GetSectree())
    		GetSectree()->RemoveEntity(this);
    
    
    	m_dwRaceVnum = 0;
    	m_dwVid = 0;
    	m_dwStartTime = 0;
    	m_dwEndTime = 0;
    	m_dwShopVid = 0;
    	m_dwPlayerID = 0;
    	
    	M2_DELETE(m_pkShop);
    	m_pkShop = NULL;
    	m_strSign = "";
    	m_strOwnerName = "";
    }
    
    
    void CPremiumPrivateShop::EncodeInsertPacket(LPENTITY ent)
    {
    	if (ent->GetType() != ENTITY_CHARACTER)
    		return;
    
    
    	LPDESC d;
    
    
    	if (!(d = ent->GetDesc()))
    		return;
    
    
    	TPacketGCCharacterAdd pack;
    	memset(&pack, 0, sizeof(pack));
    
    
    	pack.header = HEADER_GC_CHARACTER_ADD;
    	pack.dwVID = m_dwVid;
    	pack.bType = CHAR_TYPE_NPC;
    	pack.x = GetX();
    	pack.y = GetY();
    	pack.z = GetZ();
    	pack.wRaceNum = m_dwRaceVnum;
    	pack.bStateFlag = ADD_CHARACTER_STATE_GUNGON;
    
    
    	d->Packet(&pack, sizeof(pack));
    
    
    	TPacketGCCharacterAdditionalInfo addPacket;
    	memset(&addPacket, 0, sizeof(TPacketGCCharacterAdditionalInfo));
    
    
    	addPacket.header = HEADER_GC_CHAR_ADDITIONAL_INFO;
    	addPacket.dwVID = m_dwVid;
    	strlcpy(addPacket.name, m_strOwnerName.c_str(), sizeof(addPacket.name));
    
    
    	d->Packet(&addPacket, sizeof(addPacket));
    
    
    	TPacketGCShopSign p;
    
    
    	p.bHeader = HEADER_GC_SHOP_SIGN;
    	p.dwVID = m_dwVid;
    	strlcpy(p.szSign, m_strSign.c_str(), sizeof(p.szSign));
    
    
    	d->Packet(&p, sizeof(p));
    }
    
    
    void CPremiumPrivateShop::EncodeRemovePacket(LPENTITY ent)
    {
    	if (ent->GetType() != ENTITY_CHARACTER)
    		return;
    
    
    	LPDESC d;
    
    
    	if (!(d = ent->GetDesc()))
    		return;
    
    
    	TPacketGCCharacterDelete pack;
    
    
    	pack.header	= HEADER_GC_CHARACTER_DEL;
    	pack.id	= m_dwVid;
    
    
    	d->Packet(&pack, sizeof(TPacketGCCharacterDelete));
    
    
    	TPacketGCShopSign p;
    
    
    	p.bHeader = HEADER_GC_SHOP_SIGN;
    	p.dwVID = m_dwVid;
    	p.szSign[0] = '\0';
    
    
    	d->Packet(&p, sizeof(p));
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you inherited a large project which doesn't even compile any more?

    How much C++ do you know?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-30-2015, 10:33 AM
  2. Replies: 19
    Last Post: 09-25-2014, 10:02 PM
  3. Signed Long comparison in IF Statement
    By dcwang3 in forum C Programming
    Replies: 21
    Last Post: 04-25-2013, 02:01 PM
  4. signed or unsigned?
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 05-08-2007, 01:06 AM
  5. signed vs unsigned
    By char in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 01:10 PM

Tags for this Thread