IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

Comparison between signed and unsigned integer expression


Sujet :

C

  1. #1
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut Comparison between signed and unsigned integer expression
    Bonjour,

    Il y a un warning qui se manifeste sur ce type de ligne de code : for (i=0;i<BINW_SZ-2;i++)
    BINW_SZ est une variable globable de type unsigned short, déclarée dans le header. Elle permet de définir la taille des mots binaires. C'est une fonction qu'il faut appeler en première instance qui permet d'initialiser sa valeur.
    i est une variable de type unsigned long long int. Dans mon cas, il s'agit ni plus ni moins que d'une variable de parcours de boucle.

    Typiquement, le warning se manifeste dès que j'ai l'audace de comparer BINW_SZ à i lors d'une boucle.

    Le warning indique une comparaison fortuite entre une variable signée et non signée, or d'après la façon dont je déclare mes variables, elles sont toutes les deux non signées.
    Soit, je manipule de manière incorrecte mes variables, soit je ne regarde pas au bon endroit bien que cette ligne soit celle pointée par le compilateur.
    J'imagine que le problème est lié à BINW_SZ et peut-être du fait qu'elle soit une variable globale.

    Si cela peut aider, voici le code pour remettre les choses dans le contexte.
    Merci d'avance.

    PS : une question subsidiaire, quelqu'un sait-il comment est traduit en anglais "complément à 1" ? Merci.

    bin.c
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <time.h>
    #include <limits.h>
    
    #include "ptrop.h"
    #include "logger.h"
    #include "str.h"
    #include "file.h"
    #include "bin.h"
    
    unsigned short binw_set_sz()
    {
        BINW_SZ = 8*sizeof(long long int);
        fprintf(logger, "bin.c::binw_set_sz -> The binary word size is %d Byte(s) long \n",sizeof(long long int));
        return sizeof(long long int);
    }
    
    void binw_destroy(t_binw *p)
    {
        if (p)
            free(p), p=NULL;
    
        else fprintf(logger, "bin.c::binw_destroy -> %s\n", strerror(errno));
    }
    
    t_binw *binw_create()
    {
        unsigned long long int i;
        t_bit *p=NULL;
        if ( (p=(t_binw*)malloc(BINW_SZ*sizeof(t_bit))) )
            for(i=0 ; i<BINW_SZ ; i++)
                p[i]=0;
    
        else fprintf(logger, "bin.c::bit_create -> %s\n", strerror(errno));
        return p;
    }
    
    t_binw *binw_create_dyn(unsigned long long int x)
    {
        unsigned long long int i;
        t_bit *p=NULL;
        if ( (p=(t_binw*)malloc(x*sizeof(t_bit))) )
            for(i=0 ; i<x ; i++)
                p[i]=0;
    
        else fprintf(logger, "bin.c::bit_create -> %s\n", strerror(errno));
        return p;
    }
    
    void binw_display(t_binw *p, const char *title)
    {
        if (p&&title)
        {
            printf("\n");
            unsigned long long int i;
            if(title)
                printf("%s : ",title);
            for(i=BINW_SZ-1 ; i>0 ; i--)            printf("%d", p[i]);
    
            printf("%d", p[i]);
        }
        else fprintf(logger, "bin.c::binw_display -> %s\n", strerror(errno));
    }
    
    void binw_display_dyn(t_binw *p, unsigned long long int l, const char *title)
    {
        if (p)
        {
            printf("\n");
            unsigned long long int i;
            if(title)
                printf("%s : ",title);
    
            for(i=l-1 ; i>0 ; i--)
                printf("%d", p[i]);
    
            printf("%d", p[i]);
        }
        else fprintf(logger, "bin.c::binw_display -> %s\n", strerror(errno));
    }
    
    t_binw *binw_create_random()
    {
        srand(time(NULL));
        unsigned long long int i;
        t_bit *p=NULL;
        if ( (p=binw_create()) )
            for(i=0 ; i<BINW_SZ ; i++)
                p[i]=rand()%2;
    
        else fprintf(logger, "bin.c::binw_create_random -> %s\n", strerror(errno));
        return p;
    }
    
    t_bit binw_check_sign(t_binw *a)
    {
        t_bit sign=2;
        if (a)
         sign=a[BINW_SZ-1];
    
        else fprintf(logger, "bin.c::binw_check_sign -> %s\n", strerror(errno));
        return sign;
    }
    
    t_binw *binw_fill_zero( t_binw *b_in, unsigned long long int offset )
    {
        t_bit *b_out = NULL;
        if (b_in)
        {
            if ( (b_out=binw_create()) )
            {
                unsigned long long int i;
                for (i=0 ; i<BINW_SZ ; i++)
                {
                    if (i<offset)
                        b_out[i]=b_in[i];
    
                    else b_out[i]=0;
                }
            }
            else fprintf(logger, "bin.c::binw_fill_zero -> %s\n", strerror(errno));
        }
        else fprintf(logger, "bin.c::binw_fill_zero -> %s\n", strerror(errno));
        return b_out;
    }
    
    t_binw *binw_fill_one( t_binw *b_in, unsigned long long int offset )
    {
        t_bit *b_out = NULL;
        if (b_in)
        {
            if ( (b_out=binw_create()) )
            {
                unsigned long long int i;
                for (i=0 ; i<BINW_SZ ; i++)
                {
                    if (i<offset)
                        b_out[i]=b_in[i];
    
                    else b_out[i]=1;
                }
            }
            else fprintf(logger, "bin.c::binw_fill_one -> %s\n", strerror(errno));
        }
        else fprintf(logger, "bin.c::binw_fill_one -> %s\n", strerror(errno));
        return b_out;
    }
    
    t_binw *binw_not(t_binw *b_in)
    {
        unsigned long long int i;
        t_binw *b_out;
        if ( (b_out=binw_create()) )
            for (i=0 ; i<BINW_SZ ; i++)
            {
                if (b_in[i]==0){b_out[i]=1;}
                if (b_in[i]==1){b_out[i]=0;}
            }
    
        else fprintf(logger, "crypt.c::bit_not -> %s\n", strerror(errno));
        return b_out;
    }
    
    t_binw *binw_1_complement(t_binw *b_in)
    {
        t_bit *b_out=NULL;
        if (b_in)
        {
            t_bit *b_not=binw_not(b_in);
            t_bit *one = binw_create();
            one[0]=1;
    
            unsigned long long int i,limit=1;
            for (i=0;i<BINW_SZ-2;i++)
                if (b_in[i]!=0)
                    limit=0;
    
            if (!limit)
                b_out=binw_add(b_not, one);
    
            else b_out=binw_add_limit(b_not, one);
            binw_destroy(one);
            binw_destroy(b_not);
        }
        return b_out;
    }
    
    t_binw *binw_get_absolute_value(t_binw *b)
    {
        t_bit *p=NULL;
        if (b)
        {
            if(b[BINW_SZ-1]==1)
            {
                t_bit *b_not=binw_not(b);
                t_bit *one = binw_create();
                one[0]=1;
                unsigned long long int i,limit=1;
                for (i=0;i<BINW_SZ-2;i++)
                    if (b[i]!=0)
                        limit=0;
    
                if (!limit)
                    p=binw_add(b_not, one);
    
                else p=binw_add_limit(b_not, one);
                binw_destroy(one);
                binw_destroy(b_not);
            }
            else return b;
        }
        return p;
    }
    
    void binw_1_complement_void(t_binw *b)
    {
        t_bit *p=NULL;
        if (b)
        {
            t_bit *b_not=binw_not(b);
            t_bit *one = binw_create();
            one[0]=1;
            unsigned long long int i,limit=1;
            for (i=0;i<BINW_SZ-2;i++)
                if (b[i]!=0)
                    limit=0;
    
            if (!limit)
                p=binw_add(b_not, one);
    
            else p=binw_add_limit(b_not, one);
            binw_destroy(one);
            binw_destroy(b_not);
            for (i=0;i<BINW_SZ;i++)
                b[i]=p[i];
    
            binw_destroy(p);
        }
    }
    
    t_binw *bit_add(t_bit a, t_bit b, t_bit r)
    {
        t_binw *s=NULL;
        if((s=binw_create()))
        {
            if ( a==0 && b==0 && r==0)                                                  {s[0]=0; s[1]=0;}
            if ( a==1 && b==1 && r==1)                                                  {s[0]=1; s[1]=1;}
            if ((a==0 && b==0 && r==1)||(a==0 && b==1 && r==0)||(a==1 && b==0 && r==0)) {s[0]=1; s[1]=0;}
            if ((a==0 && b==1 && r==1)||(a==1 && b==0 && r==1)||(a==1 && b==1 && r==0)) {s[0]=0; s[1]=1;}
        }
        else fprintf(logger,"bin.c::bit_add -> %s\n", strerror(errno));
        return s;
    }
    
    t_bit binw_get_effective_sz(t_binw *b_in)
    {
        t_bit sz=0;
        if(b_in)
        {
            unsigned long long int i=0;
            for (i=0;i<BINW_SZ-1;i++)
                if (b_in[i]==1)
                    sz=i+1;
        }
        return sz;
    }
    
    t_bit binw_cmp_effective_sz(t_binw *a, t_binw *b)
    {
        t_bit sz=0;
        if(a&&b)
            sz=binw_get_effective_sz(a)-binw_get_effective_sz(b);
    
        else fprintf(logger, "bin.c::binw_cmp_effective_sz-> %s\n", strerror(errno));
        return sz;
    }
    
    unsigned short binw_multiply_get_new_sz(t_binw *a, t_binw *b)
    {
        t_bit shift=0;
        t_bit max_sz=0;
        if(a&&b)
        {
            t_bit a_sz = binw_get_effective_sz(a);
            t_bit b_sz = binw_get_effective_sz(b);
            if (binw_cmp_effective_sz(a,b)<=0)
                shift=a_sz,max_sz=b_sz;
    
            else shift = b_sz, max_sz=a_sz;
        }
        else fprintf(logger, "bin.c::binw_multiply_get_new_sz-> %s\n", strerror(errno));
        return shift+max_sz;
    }
    
    unsigned short binw_multiply_check_carry_overflow(t_binw *a, t_binw *b)
    {
        unsigned short carry_overflow=0;
        if (a&&b)
        {
            if(binw_multiply_get_new_sz(a,b)>BINW_SZ)
                carry_overflow=1;
        }
        else fprintf(logger, "bin.c::binw_multiply_check_carry_overflow_sz-> %s\n", strerror(errno));
        return carry_overflow;
    }
    
    t_binw *binw_add_recursive(t_binw **matrix, unsigned short matrix_sz, unsigned short index_sz, t_binw *partial_sum)
    {
        if (matrix&&partial_sum)
        {
            if(index_sz<matrix_sz)
            {
                index_sz++;
                binw_add_recursive(matrix, matrix_sz, index_sz, binw_add(partial_sum, matrix[index_sz-1]));
            }
            else return partial_sum;
        }
        else fprintf(logger, "bin.c::binw_add_recursive-> %s\n", strerror(errno));
    }
    
    t_binw **binw_get_matrix(t_binw *a, t_binw *b, unsigned long long int *binw_array_sz)
    {
        t_binw **binw_array=NULL;
        if (a&&b)
        {
            *binw_array_sz=binw_get_effective_sz(a);
            if ((binw_array=allocate_2D_us(*binw_array_sz,BINW_SZ)))
            {
                unsigned long long int i,j;
                for (i=0;i<*binw_array_sz;i++)
                     for(j=0;j<BINW_SZ;j++)
                        if (j+i<BINW_SZ)
                            binw_array[i][j+i]=a[i]&b[j];
            }
            else fprintf(logger, "bin.c::binw_get_matrix-> %s\n", strerror(errno));
        }
        else fprintf(logger, "bin.c::binw_get_matrix-> %s\n", strerror(errno));
        return binw_array;
    }
    
    t_bit binw_multiply_get_sign( t_binw *a, t_binw *b )
    {
        t_bit sign=0;
        if(a&&b)
        {
            if      ( a[BINW_SZ-1] == b[BINW_SZ-1] ){sign=0;}
            else if ( a[BINW_SZ-1] != b[BINW_SZ-1] ){sign=1;}
            else fprintf(logger, "bin.c::binw_multiply_get_sign -> %s\n", strerror(errno));
        }
        return sign;
    }
    
    t_bit binw_zero_compare(t_binw *b_in)
    {
        t_bit cmp=0;
        if ( b_in )
        {
            if (b_in[BINW_SZ]!=1)
                cmp=1;
    
            unsigned long long int i;
            for(i=0;i<BINW_SZ-1;i++)
                    if (b_in[i]!=0)
                        cmp=1;
        }
        else fprintf(logger, "bin.c::binw_zero_compare -> %s\n", strerror(errno));
        return cmp;
    }
    
    t_binw *binw_multiply(t_binw *a, t_binw *b)
    {
        t_binw *binw_multiplied=NULL;
        if(a&&b)
        {
            /* If none of the factors are equals to zero, continue the process */
            if ( (binw_zero_compare(a)==1 && binw_zero_compare(b)==1) )
            {
                t_bit a_sign, b_sign;
                /* Determine the sign of the result */
                t_bit binw_multiplied_sign = binw_multiply_get_sign(a,b);
                /* If factors are negatives, to complete the process, we must work with positive values, the sign will be applied at the end of the process */
                if((a_sign=binw_check_sign(a))==1)
                    binw_1_complement_void(a);
    
                if((b_sign=binw_check_sign(b))==1)
                    binw_1_complement_void(b);
    
                /* Determines if the multiplication will lead to a carry overflow */
                if(!binw_multiply_check_carry_overflow(a,b))
                {
                    /* This array will stock each sub-factors of the multiplication process */
                    t_binw **binw_array=NULL;
                    unsigned long long int binw_array_sz=0;
    
                    /* Determines which factor is the greater, this allows to reduce the number of sub-factors */
                    if (binw_cmp_effective_sz(a,b)<=0)
                        binw_array=binw_get_matrix(a, b, &binw_array_sz);
    
                    else binw_array=binw_get_matrix(b, a, &binw_array_sz);
    
                    unsigned short index_sz=0;
                    t_binw *binw_sum=binw_create(); /* This will stock the sum of the sub factors */
                    binw_multiplied=binw_add_recursive(binw_array, binw_array_sz, index_sz, binw_sum); /* Add all the sub-factors */
                    binw_destroy(binw_sum);
                    deallocate_2D_us(binw_array,binw_array_sz);
                    /* If the sign of the result was a negative one, then we get the negative representation of this positive value */
                    if (binw_multiplied_sign==1)
                        binw_1_complement_void(binw_multiplied);
    
                    /* If the resulting sign was supposed to be positive and the sign bit is set to negative value, then this is case of a carry overflow */
                    if (binw_multiplied_sign==0 && binw_multiplied[BINW_SZ-1]==1)
                        fprintf(logger, "bin.c::binw_multiply -> Carry overflow\n"), binw_destroy(binw_multiplied);
                }
                else fprintf(logger, "bin.c::binw_multiply -> Carry overflow\n");
            }
            /* Else there is no need to continue since we know the result is zero  */
            else binw_multiplied=binw_create();
        }
        else fprintf(logger,"bin.c::binw_multiply -> %s\n", strerror(errno));
        return binw_multiplied;
    }
    
    t_bit binw_cmp(t_binw *a, t_binw *b)
    {
        t_bit arg=0;
        if (a&&b)
        {
            unsigned long long int i;
            for (i=BINW_SZ-1;i>0;i--)
            {
                if(a[i]!=b[i])
                {
                    if (a[i]==1) /* a is greater than b */
                        arg=1,i=1;
    
                    else arg=2,i=1;  /* b is greater than a */
                }
            }
            if (arg==0)
                arg=3;  /* a is equal to b */
        }
        else fprintf(logger,"bin.c::binw_cmp -> %s\n", strerror(errno));
        return arg;
    }
    
    t_bit binw_add_get_sign(t_binw *a, t_binw *b)
    {
        t_bit sign=2;
        if(a&&b)
        {
            /* If the the sign of both binary words are the same, then the resulting one will be the same too */
            if(a[BINW_SZ-1]==b[BINW_SZ-1])
               sign=a[BINW_SZ-1];
    
            /* Else we have to determine which binary word is the greater so we will able to get the resulting sign of the addition */
            else
            {
                t_bit cmp;
                if ((cmp=binw_cmp((binw_get_absolute_value(a)),binw_get_absolute_value(b)))!=0)
                {
                    if (cmp==3) /* a and b are equals */
                    {
                        if (a[BINW_SZ-1]==1 && b[BINW_SZ-1]==1) /* Both are negatives means sign will be negative */
                            sign=1;
    
                        else sign=0; /* Else sign will be positive */
                    }
                    else  /*a or b is greater than the other one*/
                    {
                        if ((a[BINW_SZ-1]==0 && cmp==1) || (a[BINW_SZ-1]==1 && cmp==2)) {sign=0;} /* Resulting sign will be positive */
                        if ((a[BINW_SZ-1]==0 && cmp==2) || (a[BINW_SZ-1]==1 && cmp==1)) {sign=1;} /* Resulting sign will be negative */
                    }
                }
                else fprintf(logger,"bin.c::binw_add_get_sign -> The function returned a value that it should never reach\n");
            }
        }
        else fprintf(logger,"bin.c::binw_add_get_sign -> %s\n", strerror(errno));
        return sign;
    }
    
    t_binw *binw_add(t_binw *a, t_binw *b)
    {
        t_binw *r=NULL; /* Array to stock the carries */
        t_binw *s=NULL; /* Array to stock the result of the addition */
        t_bit sign=2;
        if (a&&b)
        {
            if ((sign=binw_add_get_sign(a,b))!=2) /* Getting the supposed resulting sign of the addition */
            {
                if ( (r=binw_create()) && (s=binw_create()) )
                {
                    unsigned long long int i;
                    for (i=0 ; i<BINW_SZ ; i++)
                    {
                        t_binw *c=NULL; /* To stock the result of the addition and the carry */
                        if ( (c=bit_add(a[i],b[i],r[i])) ) /* adding bit a[i] and b[i] giving c[0]=a&b and c[1]=carry for the next addition */
                        {
                            s[i]=c[0];
                            if (i<BINW_SZ-1)
                                r[i+1]=c[1];
                        }
                        else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
                        binw_destroy(c);
                    }
                    if (sign != s[BINW_SZ-1]) /* Checking if the computed sign is the supposed one, else it means carry overflow statement occurred */
                        fprintf(logger, "bin.c::binw_add -> Carry overflow\n");
                }
                else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
            }
            else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
            binw_destroy(r);
        }
        else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
        return s;
    }
    
    t_binw *binw_add_limit(t_binw *a, t_binw *b)
    {
        t_binw *r=NULL; /* Array to stock the carries */
        t_binw *s=NULL; /* Array to stock the result of the addition */
        t_bit sign=2;
        if (a&&b)
        {
            if ((sign=binw_add_get_sign(a,b))!=2) /* Getting the supposed resulting sign of the addition */
            {
                if ( (r=binw_create()) && (s=binw_create()) )
                {
                    unsigned long long int i;
                    for (i=0 ; i<BINW_SZ ; i++)
                    {
                        t_binw *c=NULL; /* To stock the result of the addition and the carry */
                        if ( (c=bit_add(a[i],b[i],r[i])) ) /* adding bit a[i] and b[i] giving c[0]=a&b and c[1]=carry for the next addition */
                        {
                            s[i]=c[0];
                            if (i<BINW_SZ-1)
                                r[i+1]=c[1];
                        }
                        else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
                        binw_destroy(c);
                    }
                }
                else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
            }
            else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
            binw_destroy(r);
        }
        else fprintf(logger, "bin.c::binw_add -> %s\n", strerror(errno));
        return s;
    }
    
    t_binw *dec_to_binw( long long int x )
    {
        unsigned long long bit = 1;
        unsigned long long int i;
        t_bit *b_out = NULL;
        if (x>=0)
        {
            if ( (b_out=binw_create()) )
                for (i=0 ; i<BINW_SZ ; i++)
                    b_out[i] = (x & bit)>>i, bit <<= 1;
    
            else fprintf(logger, "bin.c::dec_to_binw -> %s\n", strerror(errno));
        }
        else
        {
            if (x==LLONG_MIN)
            {
                t_binw *t=dec_to_binw(-x-1);
                t_binw *one=binw_create(); one[0]=1;
                b_out=binw_add_limit(t,one);
            }
            else b_out=binw_1_complement(dec_to_binw(-x));
        }
        return b_out;
    }
    
    long long int binw_to_dec(t_binw *b_in)
    {
        unsigned long long int bit = 1;
        long long int d=0;
        if(b_in)
        {
            unsigned long long int i;
            if (b_in[BINW_SZ-1]==0)
                for(i=0 ; i<BINW_SZ ; i++)
                    d += b_in[i]*(bit<<i);
    
            else
            {
                t_bit *r=binw_1_complement(b_in);
                for(i=0 ; i<BINW_SZ ; i++)
                    d -= r[i]*(bit<<i);
    
                binw_destroy(r);
            }
        }
        else fprintf(logger, "bin.c::binw_to_dec -> %s\n", strerror(errno));
        return d;
    }
    
    t_binw *str_to_bin(const char *str, unsigned long long int *binw_sz)
    {
        unsigned long long int str_sz = str_length(str);
        *binw_sz=str_sz*BINW_SZ;
        t_binw *b_out=NULL;
        if ((b_out=binw_create_dyn(*binw_sz)))
        {
            unsigned long long int i,j;
            for (i=0;i<str_sz;i++)
            {
                t_binw *c = NULL;
                if ((c=dec_to_binw(str[i])))
                {
                    for(j=0;j<BINW_SZ;j++)
                        b_out[i*BINW_SZ+j]=c[j];
                }
                else fprintf(logger, "bin.c::str_to_bin -> %s\n", strerror(errno));
                binw_destroy(c);
            }
        }
        else fprintf(logger, "bin.c::str_to_bin -> %s\n", strerror(errno));
        return b_out;
    }

    bin.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifndef BIN_H
    #define BIN_H
     
    typedef unsigned short t_bit;
    typedef unsigned short t_binw;
    unsigned short BINW_SZ;
    unsigned short binw_set_sz();
     
    void     binw_destroy(t_binw *p);
    t_binw  *binw_create();
    t_binw  *binw_create_dyn(unsigned long long int x);
    t_binw  *binw_create_random();
    void     binw_display(t_binw *p, const char *title);
     
    t_bit binw_zero_compare(t_binw *b_in);
    t_binw  *binw_fill_zero( t_binw *b, unsigned long long int offset );
    t_binw  *binw_fill_one( t_binw *b, unsigned long long int offset );
    t_bit    binw_check_sign(t_binw *a);
    t_binw  *binw_not(t_binw *b);
    t_binw  *binw_1_complement(t_binw *b);
    void     binw_1_complement_void(t_binw *b);
    t_binw  *binw_get_absolute_value(t_binw *b);
    t_binw **binw_get_matrix(t_binw *a, t_binw *b, unsigned long long int *binw_array_sz);
    t_bit    binw_cmp(t_binw *a, t_binw *b);
    t_bit    binw_get_effective_sz(t_binw *p);
    t_bit    binw_cmp_effective_sz(t_binw *a, t_binw *b);
     
    t_bit           binw_multiply_get_sign( t_binw *a, t_binw *b );
    unsigned short  binw_multiply_get_new_sz(t_binw *a, t_binw *b);
    unsigned short  binw_multiply_check_carry_overflow(t_binw *a, t_binw *b);
    t_binw         *binw_multiply(t_binw *a, t_binw *b);
     
    t_binw  *bit_add(t_bit a, t_bit b, t_bit r);
    t_bit    binw_add_get_sign(t_binw *a, t_binw *b);
    t_binw  *binw_add(t_binw *a, t_binw *b);
    t_binw  *binw_add_limit(t_binw *a, t_binw *b);
    t_binw  *binw_add_recursive(t_binw **matrix, unsigned short matrix_sz, unsigned short index_sz, t_binw *partial_sum);
     
    t_binw  *dec_to_binw( long long int x );
    long long int binw_to_dec(t_binw *b);
    t_binw *str_to_bin(const char *str, unsigned long long int *binw_sz);
     
    # endif

  2. #2
    Membre expérimenté

    Homme Profil pro
    Collégien
    Inscrit en
    Juillet 2010
    Messages
    556
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Afghanistan

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Juillet 2010
    Messages : 556
    Points : 1 428
    Points
    1 428
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (i=0;i<BINW_SZ-2;i++)
    2 est par défaut un int. Donc BINW_SZ-2 est un unsigned shor - int = int. Donc BINW_SZ-2 est un int.
    Donc ton compare un unsigned long long int avec un int.
    Plusieurs solutions:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for (int i =0;i<BINW_SZ-2;i++)
    for (unsigned short i  =0;i<((unsigned short)(BINW_SZ-2));i++)

    Ones' complement...

  3. #3
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    -.-
    Oui , en effet, je n'y avais pas pensé... Merci, la solution du cast me convient.
    Pour la traduction, j'aurai cru qu'il y avait peut être une expression particulière. C'est déjà ce que j'utilise comme traduction, ça confirme ma version des faits.
    Merci, problème résolu.

  4. #4
    Membre éclairé
    Inscrit en
    Juillet 2012
    Messages
    231
    Détails du profil
    Informations forums :
    Inscription : Juillet 2012
    Messages : 231
    Points : 870
    Points
    870
    Par défaut
    Citation Envoyé par mith06
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (i=0;i<BINW_SZ-2;i++)
    2 est par défaut un int. Donc BINW_SZ-2 est un unsigned shor - int = int. Donc BINW_SZ-2 est un int.
    Donc ton compare un unsigned long long int avec un int.
    Plusieurs solutions:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for (int i =0;i<BINW_SZ-2;i++)
    for (unsigned short i  =0;i<((unsigned short)(BINW_SZ-2));i++)
    Ou plus simplement :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (i = 0; i < BINW_SZ-2u; i++)

  5. #5
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    Encore mieux ! Merci je ne connaissais l astuce.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. comparison between pointer and integer
    Par sculpteur dans le forum Objective-C
    Réponses: 1
    Dernier message: 03/12/2010, 17h31
  2. Réponses: 5
    Dernier message: 18/09/2009, 18h56
  3. Réponses: 2
    Dernier message: 15/02/2009, 14h20
  4. Réponses: 12
    Dernier message: 22/12/2006, 00h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo