Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Lesson 3: Integ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 4106 of 4217
Post > Topic >>

Lesson 3: Integer Assignments, Signed to Unsigned with Mixed Sizes.

by "TheDevil" <TheDevil@[EMAIL PROTECTED] > Apr 12, 2008 at 08:06 AM

Hello,

First of all I already found one or two IDE bugs depending on how one
looks 
at it:

1. Freezing of IDE
2. Unable to rename folder because IDE hanging in tasklist.

Anyway.

Lesson 3: Integer Assignments, Signed to Unsigned with Mixed Sizes.

This lesson is pretty interesting.

It surprised me a couple of times.

See interesting resulting numbers.

This is awesome test to Kill Students during Exams.

Alternative/better way to get the source below:

http://members.home.nl/hbthouppermans/VS2008Analysis/Index.htm

// *** Begin of Main.cpp ***

#include <stdio.h>

typedef char int8;
typedef short int int16;
typedef int int32;
typedef long long int64;

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;

/*

12 april 2008

Back to School:

Lesson 3, Integer Assignments, Signed to Unsigned with Mixed Sizes.

Conclusions:

Bad: No warnings for extreme negative int-to-uint conversions.

39 out of 48 tests OK
9 out of 48 tests BAD

Score:

8.125 out of 10

10 out of 48 tests INTERESTING

Interesting Resulting Numbers:
128,
65408,
4294967168,
4294934528,
4294967295,
18446744073709551488,
18446744073709518848,
18446744071562067968

*/

int main()
{
    printf("Program Started \n\n");

    int8 vMinInt8;
    int8 vMaxInt8;

    uint8 vMinUint8;
    uint8 vMaxUint8;

    int16 vMinInt16;
    int16 vMaxInt16;

    uint16 vMinUint16;
    uint16 vMaxUint16;

    int32 vMinInt32;
    int32 vMaxInt32;

    uint32 vMinUint32;
    uint32 vMaxUint32;

    int64 vMinInt64;
    int64 vMaxInt64;

    uint64 vMinUint64;
    uint64 vMaxUint64;

    int8 vInt8;
    uint8 vUint8;

    int16 vInt16;
    uint16 vUint16;

    int32 vInt32;
    uint32 vUint32;

    int64 vInt64;
    uint64 vUint64;

    // setup min and max for int8
    vMinInt8 = -128;
    vMaxInt8 = 127;

    // setup min and max for uint8
    vMinUint8 = 0;
    vMaxUint8 = 255;

    // setup min and max for int16
    vMinInt16 = -32768;
    vMaxInt16 = 32767;

    // setup min and max for uint16
    vMinUint16 = 0;
    vMaxUint16 = 65535;

    // setup min and max for int32
    vMinInt32 = 2147483648; // same as -2147483648, solves warning, <- 
weakness in language.
    vMaxInt32 = 2147483647;

    // setup min and max for uint32
    vMinUint32 = 0;
    vMaxUint32 = 4294967295;

    // setup min and max for int64
    vMinInt64 = 9223372036854775808; // same as -9223372036854775808,
solves 
warning, <- weakness in language
    vMaxInt64 = 9223372036854775807; // 9223372036854775808 would give no 
warning ?!?!, BAD

    // test min and max for uint64
    vMinUint64 = 0;
    vMaxUint64 = 18446744073709551615;

    // test doubtfull assignments, like mixing types and/or sizes.

    // unsigned 8 bit = negative signed variable bit
    // TEST 1
//    vUint8 = vMinInt8;    // no warning, BAD.
                            // result: 128, OK, INTERESTING

    // TEST 2
//    vUint8 = -100;  // warning: conversion from 'int' to 'uint8', 
signed/unsigned mismatch, OK
                      // BUT INCONSISTENT with above

    // TEST 3
//    vUint8 = vMaxInt8;  // no warning, OK
//                        // result: 127, OK

    // TEST 4
//    vUint8 = vMinInt16;   // warning: conversion from 'int16' to
'uint8', 
possible loss of data, OK
                            // result: 0, OK

    // TEST 5
//    vUint8 = -10000;        // warning:  conversion from 'int' to
'uint8', 
signed/unsigned mismatch, OK

    // TEST 6
//    vUint8 = vMaxInt16;     // warning: conversion from 'int16' to 
'uint8', possible loss of data, OK
//                            // result: 255, OK

    // TEST 7
//    vUint8 = vMinInt32; // warning: conversion from 'int32' to 'uint8', 
possible loss of data, OK
//                        // result: 0, OK

    // TEST 8
//    vUint8 = -1000000;      // warning: conversion from 'int' to
'uint8', 
signed/unsigned mismatch, OK

    // TEST 9
//    vUint8 = vMaxInt32; // warning: conversion from 'int32' to 'uint8', 
possible loss of data, OK
                        // result: 255, OK

    // TEST 10
//    vUint8 = vMinInt64; // warning: conversion from 'int64' to 'uint8', 
possible loss of data, OK
//                        // result: 0, OK

    // TEST 11
//    vUint8 = -100000000000; // warning: conversion from '__int64' to 
'uint8', signed/unsigned mismatch, OK

    // TEST 12
//    vUint8 = vMaxInt64; // warning: conversion from 'int64' to 'uint8', 
possible loss of data, OK
                        // result: 255, OK

//    printf( "vUint8: %u \n", vUint8 );

    // unsigned 16 bit = negative signed variable bit

    // TEST 13
//    vUint16 = vMinInt8;  // no warning, BAD
                         // result: 65408, OK, INTERESTING

    // TEST 14
//    vUint16 = -100;      // warning: conversion from 'int' to 'uint16', 
signed/unsigned mismatch, OK

    // TEST 15
//    vUint16 = vMaxInt8; // no warning, OK
                        // result: 127, OK

    // TEST 16
//    vUint16 = vMinInt16; // no warning, BAD
//                         // result: 32768, OK

    // TEST 17
//    vUint16 = -1000;      // warning: conversion from 'int' to 'uint16',

signed/unsigned mismatch, OK

    // TEST 18
//    vUint16 = vMaxInt16;    // no warning, OK
                            // result: 32767, OK

    // TEST 19
//    vUint16 = vMinInt32; // warning: conversion from 'int32' to
'uint16', 
possible loss of data, OK
                         // result: 0, OK

    // TEST 20
//    vUint16 = -1000000;   // warning: conversion from 'int' to 'uint16',

signed/unsigned mismatch, OK

    // TEST 21
//    vUint16 = vMaxInt32; // warning: conversion from 'int32' to
'uint16', 
possible loss of data, OK.
                         // result: 65535, OK

    // TEST 22
//    vUint16 = vMinInt64; // warning: conversion from 'int64' to
'uint16', 
possible loss of data, OK
                         // result: 0, OK

    // TEST 23
//    vUint16 = -100000000000; // warning: conversion from '__int64' to 
'uint16', signed/unsigned mismatch, OK

    // TEST 24
//    vUint16 = vMaxInt64; // warning: conversion from 'int64' to
'uint16', 
possible loss of data, OK
                         // result: 65535, OK

//    printf( "vUint16: %u \n", vUint16 );

    // unsigned 32 bit = negative signed variable bit

    // TEST 25
//    vUint32 = vMinInt8; // no warning, BAD
//                        // result: 4294967168, OK, INTERESTING

    // TEST 26
//    vUint32 = -100; // conversion from 'int' to 'uint32',
signed/unsigned 
mismatch, OK

    // TEST 27
//    vUint32 = vMaxInt8; // no warning, OK
                        // result: 127, OK

    // TEST 28
//    vUint32 = vMinInt16; //  no warning, BAD
                        // result: 4294934528, OK, INTERESTING

    // TEST 29
//    vUint32 = -10000; // conversion from 'int' to 'uint32', 
signed/unsigned mismatch, OK

    // TEST 30
//    vUint32 = vMaxInt16; // no warning, OK
                         // result: 32767, OK

    // TEST 31
//    vUint32 = vMinInt32; // no warning, BAD
                         // result: 2147483648, OK

    // TEST 32
//    vUint32 = -1000000; // warning: conversion from 'int' to 'uint32', 
signed/unsigned mismatch, OK

    // TEST 33
//    vUint32 = vMaxInt32; // no warning, OK
                        // result: 2147483647, OK

    // TEST 34
//    vUint32 = vMinInt64; // warning: conversion from 'int64' to
'uint32', 
possible loss of data, OK
                        // result: 0, OK

    // TEST 35
//    vUint32 = -100000000000; // warning: conversion from '__int64' to 
'uint32', signed/unsigned mismatch, OK

    // TEST 36
//    vUint32 = vMaxInt64; // warning: conversion from 'int64' to
'uint32', 
possible loss of data, OK
                        // result: 4294967295, OK, AMAZINGLY INTERESTING

//    printf( "vUint32: %u \n", vUint32 );

    // unsigned 64 bit = negative signed variable bit

    // TEST 37
//    vUint64 = vMinInt8; // no warning, BAD
                        // result:  18446744073709551488, OK, INTERESTING

    // TEST 38
//    vUint64 = -100; // warning: conversion from 'int' to 'uint64', 
signed/unsigned mismatch

    // TEST 39
//    vUint64 = vMaxInt8; // no warning, OK
                        // result: 127, OK

    // TEST 40
//    vUint64 = vMinInt16;    // no warning, BAD
                            // result: 18446744073709518848, OK,
INTERESTING

    // TEST 41
//    vUint64 = -10000;       // warning: conversion from 'int' to
'uint64', 
signed/unsigned mismatch, OK

    // TEST 42
//    vUint64 = vMaxInt16;    // no warning, OK
                            // result: 32767, OK, AMAZINGLY INTERESTING

    // TEST 43
//    vUint64 = vMinInt32;    // no warning, BAD
                            // result: 18446744071562067968, OK,
INTERESTING

    // TEST 44
//    vUint64 = -1000000;     // warning: conversion from 'int' to
'uint64', 
signed/unsigned mismatch, OK

    // TEST 45
//    vUint64 = vMaxInt32;    // no warning: OK
                            // result: 2147483647, OK

    // TEST 46
//    vUint64 = vMinInt64;    // no warning, BAD
                            // result: 9223372036854775808, OK

    // TEST 47
//    vUint64 = -100000000000; // warning: conversion from '__int64' to 
'uint64', signed/unsigned mismatch, OK

    // TEST 48
//    vUint64 = vMaxInt64;    // no warning, OK, INTERESTING
                            // result: 9223372036854775807, OK

//    printf("vUint64: %llu \n", vUint64 );

    printf("Program finished \n");
}

// *** End of Main.cpp ***

Bye,
  The Devil.
 




 1 Posts in Topic:
Lesson 3: Integer Assignments, Signed to Unsigned with Mixed Siz
"TheDevil" <  2008-04-12 08:06:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 13:43:16 CDT 2008.