Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++ signed and unsigned
New Posts  All Forums:Forum Nav:

c++ signed and unsigned

post #1 of 12
Thread Starter 
I have been doing a little bit of C++, And i am having troubles with the variables unsigned and signed, From what i know, signed is negative values and unsigned is positive values and zero.

Can someone shed some light on this for me?

I shud make my own questions about programming thread, I have already done 3 threads.
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
post #2 of 12
Using integer allows you to use number in a pool, if you use signed (the default) then the pool is halved, half of it is negative and half is positive (positive is one shorter due to zero). If you use unsigned the whole pool goes to positive so you can go to higher number but won't have negatives.

Edit: With actual numbers for signed/unsigned int:
signed min: -32767
signed max: 32767
unsigned min: 0
unsigned max: 65535
Edited by Aximous - 8/2/11 at 5:34am
Footrest
(17 items)
 
server
(17 items)
 
HTPC
(12 items)
 
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC 
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi 
OSOSOSPower
unRaid pfSense Ubuntu CX430v2 
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller 
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot 
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV 
KeyboardCase
Lenovo n5901a Silverstone SG-05 
  hide details  
Reply
Footrest
(17 items)
 
server
(17 items)
 
HTPC
(12 items)
 
CPUMotherboardGraphicsRAM
2 x Xeon L5520 Supermicro X8DTL-i HD4550 Samsung 6x4GB ECC 
Hard DriveHard DriveCoolingOS
3 x 2TB WD Green 160GB WD Blue Hyper 212+ and EVO ESXi 
OSOSOSPower
unRaid pfSense Ubuntu CX430v2 
CaseOtherOther
CM Elite 331 TP-Link TG-3269 Sil3512 Sata controller 
CPUMotherboardRAMHard Drive
i3-2100T Asus P8H61-I Geil Black Dragon 1333 2x2GB PXE Network boot 
Optical DriveCoolingOSMonitor
OEM Slim DVD Burner Scythe Big-Shuriken OpenElec Panasonic TX-P42G30E 42" HDTV 
KeyboardCase
Lenovo n5901a Silverstone SG-05 
  hide details  
Reply
post #3 of 12
Quote:
Originally Posted by Aximous View Post
Using integer allows you to use number in a pool, if you use signed (the default) then the pool is halved, half of it is negative and half is positive (positive is one shorter due to zero). If you use unsigned the whole pool goes to positive so you can go to higher number but won't have negatives.

Edit: With actual numbers for signed/unsigned int:
signed min: -32767
signed max: 32767
unsigned min: 0
unsigned max: 65535
Those are actual the values for an unsigned and signed 16-bit value, such as a short.

In C++, unsigned int and int (signed int) are always 32-bit, so the min/max values are.

(signed) int: − 2,147,483,648 to 2,147,483,647
unsigned int: 0 to 4,294,967,295

It's easy to determine the max values mathematically if you know how many bits are in them. For example, and unsigned int is 32 bits, so calculating the max value is (2^32) - 1. For signed it is -((2^31) - 1) to (2^31) - 1. In fact, this is why 32-bit OSes can only address 4.294 Gb of memory, which is why they will only use around 3.2Gb of RAM even if you have more than 4Gb RAM in your hardware. It uses only 3.2 of the 4.2 usually due to the other GB being used to address video memory.


Here are all common values:
http://en.wikipedia.org/wiki/Integer...ral_data_types


As far as usage goes, I tend to use signed int most of the time even when representing values that should always be positive values, like speed. I do this so that I can use -1 or any negative value to signify that the variable is either invalid or hasn't been initialized to a proper speed yet.

If you go past the maximum value it will roll over to the minimum value. For example:

I didn't compile this, so I may have missed something, but you get the idea.
Code:
#include <iostream.h>

using namespace std;

int main()
{
    unsigned int test = 4294967295;
    
    cout << "Test: " << test;     // Output: 4294967295
    
    ++test;  // This increases the value of 'test' by 1.
    cout << "\
Test after increase by 1: " << test;   // Output: 0


    int signedTest = 2147483647;
    
    cout << "\
\
signedTest: " << test;     // Output: 2147483647
    
    ++signedTest;  // This increases the value of 'signedTest' by 1.
    cout << "\
signedTest after increase by 1: " << signedTest;   // Output: -2147483647

    return 1;
}

Edited by lordikon - 8/2/11 at 6:48am
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #4 of 12
Thread Starter 
Alright guys, Now I'm up to constants, But it is really hard, Can someone explain?

http://www.cplusplus.com/doc/tutorial/constants/
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
post #5 of 12
Not sure what to explain, constants are just values that never change.

int myNum = 5; // the '5' is the constant

int sumOfNums = 2 + 2; // the '2's are the constant
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #6 of 12
Quote:
Originally Posted by AMDrocks View Post
Alright guys, Now I'm up to constants, But it is really hard, Can someone explain?

http://www.cplusplus.com/doc/tutorial/constants/
The biggest thing with constants is understanding how annoyingly simple it is. Basically it for something you will be using a lot. Say you need Pi and don't have it in a library, you can just create a constant with the value of Pi named Pi makes it easier to type and deal with.
Herp
(1 item)
 
  
Other
Dolla Billz Yall! 
  hide details  
Reply
Herp
(1 item)
 
  
Other
Dolla Billz Yall! 
  hide details  
Reply
post #7 of 12
Constants are simply things that dont change.

So the number 5 is a constant. The letter 'z' is a constant.

You can also make your own constants 2 different ways, using const and define. These make values that you cant change once made.
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #8 of 12
Thread Starter 
In addition to decimal numbers (those that all of us are used to using every day), C++ allows the use of octal numbers (base 8) and hexadecimal numbers (base 16) as literal constants. If we want to express an octal number we have to precede it with a 0 (a zero character). And in order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:




75 // decimal
0113 // octal
0x4b // hexadecimal



All of these represent the same number: 75 (seventy-five)
expressed as a base-10 numeral, octal numeral and hexadecimal numeral, respectively.

Literal constants, like variables, are considered to have a specific data type. By default, integer literals are of type int. However, we can force them to either be unsigned by appending the u character to it, or long by appending l:




75 // int
75u // unsigned int
75l // long
75ul // unsigned long



In both cases, the suffix can be specified using either upper or lowercase letters.

^ This is the main bit i don't get
I know that constants are things that never change.
Edited by AMDrocks - 8/5/11 at 6:45pm
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
post #9 of 12
Quote:
Originally Posted by lordikon View Post
Not sure what to explain, constants are just values that never change.

int myNum = 5; // the '5' is the constant

int sumOfNums = 2 + 2; // the '2's are the constant

I think he's talking about the const keyword. The const keyword is basically just a failsafe. You're telling the system that under no circumstances can this value be overwritten.

So writing something like

Code:
const int x = 5;
x = 4;
will throw a compiler error.


This differs from the #define keyword. #define is literally nothing more than a find and replace function.

For example, this is a perfectly valid C program:

Code:
#include <stdio.h>
#define LOL {
#define CAT }
#define CHEESE int
#define TUNA char**
#define SNACK main
#define KITTEH_SEZ printf
#define YOU_CAN_HAS return


CHEESE SNACK(CHEESE argc, TUNA argv)
LOL
    KITTEH_SEZ("OMG WHY DOES THIS WORK I AM NOT GOOD WITH COMPUTER\
");
    YOU_CAN_HAS 0;
CAT
(basically what I'm saying is BE CAREFUL WITH #define!)



Edit:


Edited by nathris - 8/5/11 at 10:15pm
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i5 4670k ASUS Maximus VI Gene Gigabyte GTX 460 1GB Kingston Hyper-X 
Hard DriveHard DriveHard DriveHard Drive
Samsung 830 OCZ Vertex 3 WD6401AALS WD5000AAKS 
CoolingOSMonitorMonitor
Noctua NH-D14 elementary OS Dell Ultrasharp U2312HM LG W2442PA-BF 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750W Corsair Graphite 600T Logitech G700 
Audio
ASUS Xonar DG 
  hide details  
Reply
post #10 of 12
Thread Starter 
Hey now i know define! Thanks nathris, +rep.
But look at post #8, Highlighted in bold is what i am truly not getting. Please tell me about octals, hexadecimals and how the highlighted all equals 75

So define is basically using the preprocessor directive so you don't have to use variables?
Edited by AMDrocks - 8/6/11 at 12:53am
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
My system (AMD)
(14 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 955 BE "Deneb" @ 3415Mhz Gigabyte GA-MA790X-DS4 (1x) Gigabyte AMD Radeon 5770 Kingston HyperX DDR2 1066 4GB 
Hard DriveOSMonitorKeyboard
2x 500GB, 1x 1TB external Windows 7 Ultimate Deep Blue 32bit 1x Asus VE248H 24" Monitor Tt eSPORTS Challenger Gaming Keyboard 
PowerCaseMouseMouse Pad
Thermaltake 550w Aero Cool Syclone II Logitech MX518 1800DPI Red Mousepad 
Audio
Creative Headphones 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c++ signed and unsigned