reset password
Author Message
jpascua
Posts: 197
Posted 13:36 Feb 27, 2015 |

Take your Set program and re-implement it using an unsigned long long type integer to store the values in the set. Each bit of the number will represent a value in your set. A long long type is 8 bytes or 64 bits long. This means you can use one long long integer to represent a set of the values from [0, 63]. The most significant bit of the integer will represent number 0, and the least significant bit will represent 63.

00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

So we have 64 bits (above). I don't quite understand what is meant by "each bit of the number will represent a value in your set." Can someone explain this in a different way?

Thanks.

Last edited by jpascua at 13:38 Feb 27, 2015.
jwarren6
Posts: 56
Posted 14:14 Feb 27, 2015 |

Looking at a long long integer, the leftmost digit represents 0 and the rightmost digit represents 63. So, say my set contains the numbers 1, 5, 17, 25, 55, 63. Then, the representation of that set would be:

01000100 00000000 01000000 10000000 00000000 00000000 00000001 00000001

Do you see that from left to right above, the values of the set are represented by the 1 in the proper places?

jpascua
Posts: 197
Posted 14:28 Feb 27, 2015 |

Got it. :)

jpascua
Posts: 197
Posted 17:29 Feb 27, 2015 |

Edit: Solved.

Last edited by jpascua at 21:54 Feb 27, 2015.
jpascua
Posts: 197
Posted 23:48 Feb 27, 2015 |

No need to implement the function output() since we are overloading the << operator, right?

jwarren6
Posts: 56
Posted 19:37 Mar 01, 2015 |

Right.

jwarren6
Posts: 56
Posted 15:06 Mar 02, 2015 |

Note: When setting up and shifting a mask that is bigger than one byte (I'm assuming that your mask is an unsigned long long, right?), you must initialize/assign and shift the mask variable in two steps (or more). For example:
        unsigned long long mask = 1;                                                        unsigned long long mask;
        mask <<= (63 - x);                                                                          mask = 1;
                                                                                                               mask <<= (63 - x);

If you don't do this in two steps (or more), your mask will contain the wrong value when shifting more than 7 places. For example:
       unsigned long long mask = 1 << (63 - x); // Bad!                            unsigned long long mask;
                                                                                                               mask = 1 << (63 - x); // Bad!

I don't know why this happens, I googled it but couldn't find anything about this behavior. I suspect this has to do with endian, but I'm not sure.

Edit: Also, you can use Jenny's technique below that involves typecasting.

Last edited by jwarren6 at 19:50 Mar 02, 2015.
jpascua
Posts: 197
Posted 19:44 Mar 02, 2015 |

Oh yeah, this happened to me! I spent a lot of time just figuring out why. lol But, it's because 1 << (63 - x) is processed as an int THEN implicitly converted into an unsigned long long. So instead of working with 8 bytes, you're working with 4 bytes.

What I did to resolve this issue is to cast 1 as follows: (unsigned long long)1 << (63 - x) or 1ULL << (63 - x). 

jwarren6
Posts: 56
Posted 19:48 Mar 02, 2015 |

Ah! Thanks.