C++ Tutorial/Data Types/unsigned short

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Adding Too Large a Number to a signed short Integer

<source lang="cpp">#include <iostream> int main() {

  short int smallNumber;
  smallNumber = 32767;
  std::cout << "small number:" << smallNumber << std::endl;
  smallNumber++;
  std::cout << "small number:" << smallNumber << std::endl;
  smallNumber++;
  std::cout << "small number:" << smallNumber << std::endl;
  return 0;

}</source>

Create an unsigned short and initialize with result of multiplying two unsigned short variables

<source lang="cpp">#include <iostream>

int main()
{
    unsigned short int Width = 5, Length;
    Length = 10;

    unsigned short int Area = Width * Length;

    std::cout << "Width: " << Width << "\n";
    std::cout << "Length: "  << Length << std::endl;
    std::cout << "Area: " << Area << std::endl;
    return 0;
}</source>
Width: 5
Length: 10
Area: 50

Create an unsigned short and initialize with result of multiplying Width by Length

<source lang="cpp">#include <iostream> int main() {

  using std::cout;
  using std::endl;
  unsigned short int Width = 5, Length;
  Length = 10;
  unsigned short int Area  = (Width * Length);
  cout << "Width:" << Width << endl;
  cout << "Length: "  << Length << endl;
  cout << "Area: " << Area << endl;
  return 0;

}</source>

Demonstrates declaring and initializing short variables

<source lang="cpp">#include <iostream> using namespace std; int main() {

     int score;
     double distance;
     char playAgain;
     bool shieldsUp;
     short lives, aliensKilled;
     score = 0;
     distance = 1200.76;
     playAgain = "y";
     shieldsUp = true;
     lives = 3;
     aliensKilled = 10;
     double engineTemp = 6572.89;
     cout << "\nscore: "           << score << endl;
     cout << "distance: "      << distance << endl;
     cout << "playAgain: "      << playAgain << endl;
     cout << "lives: "           << lives << endl;
     cout << "aliensKilled: "<< aliensKilled << endl;
     cout << "engineTemp: "     << engineTemp << endl;
     int fuel;
     cout << "\nHow much fuel? ";
     cin >> fuel;
     cout << "fuel: " << fuel << endl;
     typedef unsigned short int ushort;
     ushort bonus = 10;
     cout << "\nbonus: " << bonus << endl;
     return 0;

}</source>

Putting Too Large a Value in an unsigned short Integer

<source lang="cpp">#include <iostream> int main() {

  using std::cout;
  using std::endl;
  unsigned short int smallNumber;
  smallNumber = 65535;
  cout << "small number:" << smallNumber << endl;
  smallNumber++;
  cout << "small number:" << smallNumber << endl;
  smallNumber++;
  cout << "small number:" << smallNumber << endl;
  return 0;

}</source>

unsigned short int: overflow

<source lang="cpp">#include <iostream>

int main()
{
    unsigned short int smallNumber;
    smallNumber = 65535;
    std::cout << "small number:" << smallNumber << std::endl;
    smallNumber++;
    std::cout << "small number:" << smallNumber << std::endl;
    smallNumber++;
    std::cout << "small number:" << smallNumber << std::endl;
    short int smallNumber1;
    smallNumber1 = 32767;
    std::cout << "small number:" << smallNumber1 << std::endl;
    smallNumber1++;
    std::cout << "small number:" << smallNumber1 << std::endl;
    smallNumber1++;
    std::cout << "small number:" << smallNumber1 << std::endl;
    return 0;
}</source>
small number:65535
small number:0
small number:1
small number:32767
small number:-32768
small number:-32767

Use typedef to define new type for unsigned short

<source lang="cpp">#include <iostream> typedef unsigned short int USHORT; int main() {

  using std::cout;
  using std::endl;
  USHORT  Width = 5;
  USHORT Length;
  Length = 10;
  USHORT Area  = Width * Length;
  cout << "Width:" << Width << endl;
  cout << "Length: "  << Length << endl;
  cout << "Area: " << Area <<endl;
  return 0;

}</source>