Thread: 1 byte int, not using char

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    82

    1 byte int, not using char

    Anyone know of a library which defines a 1 byte int? The problem I have with using char is that the output (cout << charvar) is interpreted as a character. I don't like having to cast unsigned/signed chars as an int or short int.

    There has to be a way to do arithmetic with 1 byte data types and display the integer values (cout << onebyteint) without needing to cast the char to a 2 or 4 byte data type.

    Anyone?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you just use an unsigned char then?

    EDIT:
    Oh, I see the point about your dislike for a cast. Just do it. Alternatively, you write a function that does the printing for you, with the cast.
    Last edited by laserlight; 07-27-2010 at 09:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Instead of getting hung-up over your own personal opinions or principles, please consider this an intellectual exercise as you read my questions. I'd appreciate it!

    Keep in mind, I'm not looking for an alternative solution, I'm asking sincerely about the state of the C++ programming language itself, so it's important to not dance around the issue but instead to be direct!

    -
    So what you're telling me is that in C++ there is no such thing as a 1 Byte data type which, by default, will display as an integer?

    Is that a fact?
    -

    Thanks!


    Also; if that is the case I might just use short int's with an 0xFF mask, but is that more efficient than casting? I guess I can test that out for myself, just thinking out loud.
    Last edited by since; 07-27-2010 at 09:47 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > consider this an intellectual exercise as you read my questions.
    OK, I thought of a couple of ways, now what?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Salem,

    Share the ideas, instead of being an elitist jerk?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by since View Post
    -
    So what you're telling me is that in C++ there is no such thing as a 1 Byte data type which, by default, will display as an integer?

    Is that a fact?
    -

    Thanks!
    You'll probably be hard pressed to find a library that does. Because char does exactly that.
    The C++ standard doesn't guarantee any specific sizes, only that
    char <= short <= int <= long <= long long

    Also; if that is the case I might just use short int's with an 0xFF mask, but is that more efficient than casting?
    Why bother with the mask? If you're only using 8 bits, then the high bits are 0 and you'll get the result you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Elysia;

    char a = 96;
    cout << a << endl;

    What do you think the output is? I want it to be the integer 96, but it just won't be.

    I bother with masking so I don't have to do this.
    cout << (short)a << endl;
    Last edited by since; 07-27-2010 at 09:56 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by since
    Instead of getting hung-up over your own personal opinions or principles, please consider this an intellectual exercise as you read my questions. I'd appreciate it!

    Keep in mind, I'm not looking for an alternative solution, I'm asking sincerely about the state of the C++ programming language itself, so it's important to not dance around the issue but instead to be direct!
    Oh, I see. I am afraid that in the core language, there is no functionality in standard C++ for output.

    Quote Originally Posted by since
    So what you're telling me is that in C++ there is no such thing as a 1 Byte data type which, by default, will display as an integer?
    There is no such thing in C++ as a data type which, by default, will display as an integer.

    Quote Originally Posted by since
    Also; if that is the case I might just use short int's with an 0xFF mask, but is that more efficient than casting?
    It will be more obfuscated than casting. Oh sorry, I forgot that you are not interested in my own personal opinions or principles.

    Quote Originally Posted by Elysia
    The C++ standard doesn't guarantee any specific sizes, only that
    char <= short <= int <= long <= long long
    It also guarantees that sizeof(char) == 1, that CHAR_BIT will be at least 8, as well as minimum ranges for the built-in integer types.
    Last edited by laserlight; 07-27-2010 at 09:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by since View Post
    Elysia;

    char a = 96;
    cout << a << endl;

    What do you think the output is? I want it to be the integer 96, but it just won't be.

    I bother with masking so I don't have to do this.
    cout << (short)a << endl;


    I'll just treat short int's as paired, distinct, bytes.
    Why can't you just do?
    short a = 96;
    cout << a << endl;
    No masking. No cast. Yes, it's a wasted byte, but unless you're really hurting for memory, that probably won't matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Share the ideas, instead of being an elitist jerk?
    With motivational skills like that, you'll make a fine manager one day

    I'm just content to watch all the floundering going on at the moment - lol
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    It will be more obfuscated than casting. Oh sorry, I forgot that you are not interested in my own personal opinions or principles.
    Computational Complexity is a formal field of mathematics, I am determined to demonstrate your elitism to you; can you argue that the complexity of the operations for masking a short int versus casting a character to a short int cannot be known?

    Because, if it can be, then there is an objective statement to be made about efficiency, which has nothing to do with opinion, but is a matter of pure science. >:]

    As for why, well, I just have an obsession for minimalism, I wanted the perfect size and allocation for what I'm doing, or at least ... as close as I can get to that, because I want to! (Whether you like it or not.)

    Not even setting formatting flags on cout can modify how it represents chars, it seems to only work for integer datatypes which is frustrating even more!

    /me sighs another frustrated sigh. I'll do some debugging to see if casting or masking short int's is faster.
    Last edited by since; 07-27-2010 at 10:26 AM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Whether or not masking or casting is faster is not something you should care about. It's called premature optimization and it the root of much evil.
    Let it go. Use shorts. Be happy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I doubt the speed of either the mask or the cast will be significant compared to the actual i/o speed.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Computational Complexity is a formal field of mathematics, I am determined to demonstrate your elitism to you; can you argue that the complexity of the operations for masking a short int versus casting a character to a short int cannot be known?
    The complexity is O(1) in both cases. Duh! Bringing up complexity in this discussion is ridiculous. We're not talking about complexity here, we're talking about compiler technology and hardware. Both are so complex that an isolated test of masking vs casting will tell you exactly nothing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by since
    Computational Complexity is a formal field of mathematics, I am determined to demonstrate your elitism to you; can you argue that the complexity of the operations for masking a short int versus casting a character to a short int cannot be known?

    Because, if it can be, then there is an objective statement to be made about efficiency, which has nothing to do with opinion, but is a matter of pure science. >:]
    The fact that you bring up computational complexity makes me think that you are mainly citing the term to try and intimidate me. Nice try, I am also formally trained in computer science.

    The truth is that the cost of these operations depend on their implementation and how they are translated into assembly/machine code. When I talked about "personal opinions or principles", it is in relation to my opinion that the use of a cast will make your intentions more explicit, and my principle of prefering to do the more readable thing unless it turns out that I must do the more obfuscated thing because efficiency is a concern.

    In fact, note that if you use bit masking, the usual arithmetic conversions will be performed: there will be a conversion from char to int. It will effectively be as if you cast the char to an int, and then performed the bit mask.

    Quote Originally Posted by since
    I'll do some debugging to see if casting or masking short int's is faster.
    Yes, you should measure if you are really interested. (We do not typically call this debugging, though arguably trying to meet certain timing requirements could be construed as debugging.)

    EDIT:
    Quote Originally Posted by pianorain
    I doubt the speed of either the mask or the cast will be significant compared to the actual i/o speed.
    Agreed, hence you should go for what better documents your intentions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM