Thread: Design a struct to represent a set?

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    3

    Design a struct to represent a set?

    Im trying to design a struct to represent a set. The struct will hold the maximum set size and a pointer to an array holding 1 bit per possible element of the set.

    But How do I create a pointer to an array holding 1 bit per possible element of the set. Seems impossible?How can this be one in c?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,055
    Well you would use an array of unsigned integers.

    The number of bits you have in each unsigned int is
    n = CHAR_BIT*sizeof(unsigned int);

    So you
    - use /n to get the array element.
    - use %n to get the bit position within an element.
    - use << >> & | to manipulate that bit.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    3
    Im still confused. Can you show an example of say, getting array element 3?
    I dont understand these two
    - use /n to get the array element.
    - use %n to get the bit position within an element.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,055
    What do you want to know about division and modulus.
    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
    Aug 2018
    Posts
    3
    Im familiar with div and mod but using them to get say position 3 i dont understand.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,055
    Well if you have 32 bits in each integer, and an array of integers, then

    The array index is given by 3 / 32
    The bit position within that integer is given by 3 % 32

    The rest is just bit twiddling.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to represent velocity vector ?
    By shivagonal in forum C Programming
    Replies: 6
    Last Post: 04-13-2015, 10:14 AM
  2. Design problem: Initializing const struct
    By MOS-6581 in forum C++ Programming
    Replies: 7
    Last Post: 02-24-2015, 07:36 AM
  3. How to represent a map
    By gavra in forum Game Programming
    Replies: 10
    Last Post: 09-12-2011, 04:44 PM
  4. Represent Maze
    By iamnew in forum C++ Programming
    Replies: 37
    Last Post: 04-28-2010, 08:47 PM
  5. shorter way to represent alphabet
    By cdkiller in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 12:38 PM

Tags for this Thread