Storing several characters in an int

This is a discussion on Storing several characters in an int within the C++ Programming forums, part of the General Programming Boards category; Code: char x = 'x', y = 'y', z = 'z'; int chars = x << 16; chars |= y ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Storing several characters in an int

    Code:
    char x = 'x', y = 'y', z = 'z';
    int chars = x << 16; chars |= y << 8; chars |= z;
    
    char x_new = chars >> 16, y_new = chars >> 8, z_new = chars;
    cout << x_new << " " << y_new << " " << z_new;
    This code allows me to store several characters in one int. I wonder if it can be useful, in the sense that it will take up less space than an array of chars. Please let me know if it can be used to advantage or if it's totally useless
    Last edited by Zewu; 07-08-2002 at 08:11 AM.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    actually, you are learning some of what makes C/C++ useful. To answer your question, no it does not take up less memory. An array of chars means an array of bytes. An int is multiple bytes. You are using the bytes regardless of how you declare it. But.... through clever recasting and such, you should see nice patterns for manipulating memory as if it were bytes. Ahh the glory of C!

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Yes, it should take up less space then a character array, as a character array includes a pointer (4 Bytes) whereas an integer does not.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    oh, that's an angle I had not considered. Although in practice you are probably talking about an array of integers vs array of char's OR a list of chars vs a single int. In the array case, the pointer exists in both cases. So I stand by my first statement.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    First, what are you planning on doing if your values somehow become negative? Bit shifting should only be done on unsigned values. Second, how do you know the size of an int? You could be over-running the int on one machine and under using it on another. This is a very non-portable construct.

    >Yes, it should take up less space then a character array
    Have you tested this? On my system, an array of three char is smaller than an int containing three char by one byte.

    This is a clever way of handling chars, but hardly practical except in extreme cases where storage is at a premium. And even then it has to be very scarce for this to be of any real use since the readability of the program is drastically decreased and you spend so many operations dealing with it. By profiling your method and then the same thing with an array of char (create and then reference each element), the result is that the array is quicker by .03 milliseconds.

    And yet another argument against it. Are you sure that it's safe? Did you have to look twice to be sure? What about with this:

    char array[3] = { 'x', 'y', 'z' };

    I'll bet your results are the same as mine, it's easier to check the correctness of the array since it's so common. With bit twiddling you have to go through everything and make sure that it's all kosher before relaxing your guard.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    agreed. I always consider buffers of unsigned char to be the most reliable way of doing things from one system to another. An unsigned char is a byte on any system. This allows the best use of memory. You can't really get all paranoid about the use of a pointer. yes, it's the same as a long (in win32 at least), but who cares! a single long is nothing to complain about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21