Thread: Bad Practice - Mix Int and Long Int

  1. #1
    Banned
    Join Date
    Jul 2022
    Posts
    112

    Bad Practice - Mix Int and Long Int

    Is it a bad idea to mix integer types ?
    Does it affect speed ?
    Is it considered a "bad practice" ?
    Also, is it terrible to use 64-bit integers only ?

    Code:
    int64_t a = 514915891581; // some large value
    int32_t b = 5488;
    a = a / b; // implicit conversion - b is converted to int64_t
    Last edited by kodax; 08-21-2022 at 07:50 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Is it a bad idea to mix integer types ?
    It can be. As your example shows, when you mix integer types in arithmetic expressions, the language applies conversion rules, some of which are hard to remember.

    Does it affect speed ?
    I don't know, but I'm willing to bet you'd be hard-pressed to measure any difference.

    Is it considered a "bad practice" ?
    The Go programming language forbids it, but only so that they didn't have to define the implicit conversion rules. My approach is to do whatever -Wall -Wextra -pedantic -Werror will allow.

    Also, is it terrible to use 64-bit integers only ?
    No, it is not terrible. Your 64-bit machine sees the world mostly as 64-bit integers.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    The point is, we will always have to deal with a small value,
    which results in a mix of 32-bit, 64-bit, and sometimes even 8-bit integers.

    Sometimes this helps because it is readable, and in that aspect,
    it is easy to know why an 8-bit integer is used.

    However this also creates a lack of uniformity, and this can become very ugly,
    with 1000+ lines of mixing integers.

    Also mixing many integers types, can lead to overflows that can be hard to catch, but does it make sense to have 10 000 lines with 64-bit integers only ?

    What is your take ??
    Last edited by kodax; 08-21-2022 at 08:38 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by kodax View Post
    The point is, we will always have to deal with a small value,
    which results in a mix of 32-bit, 64-bit, and sometimes even 8-bit integers.
    I don't consider a 64 bit integer, a "small value".

    If you know the range of values you will be dealing with, then choose the integer size that will handle all. Look at limits.h.

    Are the values all unsigned, or a mix of signed and unsigned?

    I would avoid mixing integer sizes without a legitimate reason to do so. If you do, I would avoid downsizing to avoid possible integer overflow without the need for extra code.

  5. #5
    Banned
    Join Date
    Jul 2022
    Posts
    112
    I use only signed integers.

    The idea was to use 64-bit integers when needed and to stick to the regular 32-bit integers.
    It is not a problem and it is not wrong, but it can look quite uncommon to use only 64-bit integers.
    Last edited by kodax; 08-22-2022 at 10:49 AM.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by kodax View Post
    I use only signed integers.

    The idea was to use 64-bit integers when needed and stick to the regular 32-bit integers.
    It is not a problem and it is not wrong, but it can look quite uncommon to use only 64-bit integers.
    Do any of your signed integers exceed 2,147,483,647? (32 bit INT_MAX) If not, then not need to use 64 bit integers.

    What do these values represent?

  7. #7
    Banned
    Join Date
    Jul 2022
    Posts
    112
    It does not exceed, it's just that there is a need to use 64-bit integers.

    The point was that more than 1000 lines of codes would be mixed with 32-bit and 64-bit integers.
    In that case, I wanted to use only 64-bit integers to make things more uniformed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-05-2018, 09:59 AM
  2. Replies: 16
    Last Post: 06-07-2015, 03:28 PM
  3. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  4. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  5. Replies: 1
    Last Post: 10-11-2010, 01:53 AM

Tags for this Thread