bits like int x:4

This is a discussion on bits like int x:4 within the C Programming forums, part of the General Programming Boards category; hi i want to create an unsigned integer array with 10 members each of them is 4 bit. for example ...

  1. #1
    esler
    Guest

    bits like int x:4

    hi

    i want to create an unsigned integer array with 10 members each of them is 4 bit.
    for example
    unsigned int x:4; create an int element with 4 bit ,so 0 to 15 can be assgined t ox

    how can i create an array like i explain above

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    As far as I know, you cannot create your own sized datatypes. What you could do is use bitshifting and masking to store several of your 4bitIntegers in one larger datatype.

    ie: a char is 8bit (which means you can store 2 4bitInts in it)

    To get the first int:

    ((CharVar & 0xF0) >> 4)

    To get the second int4 (4-bit int):

    (CharVar & 0x0F)

    To store the first int4:

    CharVar = (CharVar & 0x0F) | ((Int4 << 4) & 0xF0)

    To store the second int4:

    CharVar = (CharVar & 0xF0) | (Int4 & 0x0F)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    The format of the code that you are using looks a bit like bit fields.
    EG:
    Code:
    struct foo 
    {
       unsigned int x     : 1;
       unsigned int y     : 4;
    };
    ..but this doesn't work with arrays. What is it that you need to do exactly?

  4. #4
    esler
    Guest
    foniks munkee said that
    "..but this doesn't work with arrays. What is it that you need to do exactly?"

    for example 10000 students at a university . i want to manipulate their grades . a student can take 5 lesson ,and each lesson have 2 midterms,2quiz and a final. the range for mid quiz and finals are 0-100 ,so if i use int for hold grades it wastes the memory so much . For 6 bit is enough to hold mid grades...

    i think it is more clear

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >so if i use int for hold grades it wastes the memory so much

    For an application like you're working on, I wouldn't care about memory. I guess you're working on a PC which has I guess, lots of memory.

    Instead of an unsigned int, you could use an unsigned char.

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Well, if you have to store the students name with their score you could use an array of structures and use bit fields to store the scores.

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, 11:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

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