photo
+ Responder ao Tópico
Resultados de 1 a 1 de 1

Thread: Tipos char, short, int e long char

  1. #1 Fechar a publicação
    Senior Member usuarioforum's Avatar usuarioforum's Avatar
    Data de afiliação
    Aug 2018
    Postagens
    579
    Obrigado
    166
    Agradecimentos 609 Tempos em 367 Postagens
    AssinarAssinar
    subscritar: 0

    Tipos char, short, int e long char

    O tipo char usa 1 byte de memória (8 bits) e permite expressar em notação binária 2^8=256 valores. O tipo char pode conter tanto valores positivos quanto negativos. A faixa de valores é de -128 a 127.

    uchar
    O tipo inteiro uchar também ocupa 1 byte de memória, assim como o tipo char , mas diferente dele uchar é destinado apenas para valores positivos. O valor mínimo é zero, o valor máximo é 255. A primeira letra u no nome do tipo uchar é abreviatura de unsigned (sem sinal).

    short
    O tamanho do tipo short é de 2 bytes (16 bits) e, conseqüentemente, ele permite expressar a faixa de valores igual a 2 elevado a 16: 2^16 = 65 536. Como o tipo short é um tipo com sinal, e contém tanto valores positivos quanto negativos, a faixa de valores é entre -32 768 e 32 767.

    ushort
    O tipo short sem sinal é o tipo ushort, que também tem 2 bytes de tamanho. O valor mínimo é 0, o valor máximo é 65 535.

    int
    O tamanho do tipo int é de 4 bytes (32 bits). O valor mínimo é -2 147 483 648, o valor máximo é 2 147 483 647.

    uint
    O tipo integer sem sinal é uint. Ele usa 4 bytes de memória e permite expressar inteiros de 0 a 4 294 967 295.

    long
    O tamanho do tipo long é de 8 bytes (64 bits). O valor mínimo é -9 223 372 036 854 775 808, o valor máximo é 9 223 372 036 854 775 807.

    ulong
    O tipo ulong também ocupa 8 bytes e pode armazenar valores de 0 a 18 446 744 073 709 551 615.

    Exemplos:

    char ch=12;
    short sh=-5000;
    int in=2445777;

    Como os tipo inteiros sem sinal não são concebidos para armazenar valores negativos, a tentativa de atribuir um valor negativo pode levar a conseqüências inesperadas. Este simples script levará a um loop infinito:

    //--- Loop infinito
    void OnStart()
    {
    uchar u_ch;

    for(char ch=-128;ch<128;ch++)
    {
    u_ch=ch;
    Print("ch = ",ch," u_ch = ",u_ch);
    }
    }

    A variante correta é:

    //--- Variante correta
    void OnStart()
    {
    uchar u_ch;

    for(char ch=-128;ch<=127;ch++)
    {
    u_ch=ch;
    Print("ch = ",ch," u_ch = ",u_ch);
    if(ch==127) break;
    }
    }

    Resultado:

    ch= -128 u_ch= 128
    ch= -127 u_ch= 129
    ch= -126 u_ch= 130
    ch= -125 u_ch= 131
    ch= -124 u_ch= 132
    ch= -123 u_ch= 133
    ch= -122 u_ch= 134
    ch= -121 u_ch= 135
    ch= -120 u_ch= 136
    ch= -119 u_ch= 137
    ch= -118 u_ch= 138
    ch= -117 u_ch= 139
    ch= -116 u_ch= 140
    ch= -115 u_ch= 141
    ch= -114 u_ch= 142
    ch= -113 u_ch= 143
    ch= -112 u_ch= 144
    ch= -111 u_ch= 145
    ...

    Exemplos:

    //--- Valores negativos não podem ser armazenados em tipos sem sinal
    uchar u_ch=-120;
    ushort u_sh=-5000;
    uint u_in=-401280;

    Hexadecimal: números 0-9, as letras a-f ou A-F para os valores de 10-15; começam com 0x ou 0X.

    Exemplos:

    0x0A, 0x12, 0X12, 0x2f, 0xA3, 0Xa3, 0X7C7

    For integer variables, the values can be set in binary form using B prefix. For example, you can encode the working hours of a trading session into int type variable and use information about them according to the required algorithm:

    //+------------------------------------------------------------------+
    //| Script program start function |
    //+------------------------------------------------------------------+
    void OnStart()
    {
    //--- set 1 for working hours and 0 for nonworking ones
    int AsianSession =B'111111111'; // Asian session from 0:00 to 9:00
    int EuropeanSession=B'111111111000000000'; // European session 9:00 - 18:00
    int AmericanSession =B'111111110000000000000011'; // American session 16:00 - 02:00
    //--- derive numerical values of the sessions
    PrintFormat("Asian session hours as value =%d",AsianSession);
    PrintFormat("European session hours as value is %d",EuropeanSession);
    PrintFormat("American session hours as value is %d",AmericanSession);
    //--- and now let's display string representations of the sessions' working hours
    Print("Asian session ",GetHoursForSession(AsianSession));
    Print("European session ",GetHoursForSession(EuropeanSession));
    Print("American session ",GetHoursForSession(AmericanSession));
    //---
    }
    //+------------------------------------------------------------------+
    //| return the session's working hours as a string |
    //+------------------------------------------------------------------+
    string GetHoursForSession(int session)
    {
    //--- in order to check, use AND bit operations and left shift by 1 bit <<=1
    //--- start checking from the lowest bit
    int bit=1;
    string out="working hours: ";
    //--- check all 24 bits starting from the zero one and up to 23 inclusively
    for(int i=0;i<24;i++)
    {
    //--- receive bit state in number
    bool workinghour=(session&bit)==bit;
    //--- add the hour's number to the message
    if(workinghour )out=out+StringFormat("%d ",i);
    //--- shift by one bit to the left to check the value of the next one
    bit<<=1;
    }
    //--- result string
    return out;
    }

    Though trading on financial markets involves high risk, it can still generate extra income in case you apply the right approach. By choosing a reliable broker such as InstaForex you get access to the international financial markets and open your way towards financial independence. You can sign up here.


  2. Os seguintes 2 Usuários Dizem Obrigado o a usuarioforum por este post útil:

    Não registrado (2 )

+ Responder ao Tópico

Permissões de postagens

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts