Solved

Making the most of a long

Posted on 2003-11-24
13
316 Views
Last Modified: 2010-04-01
An external library gives me a user-defined field of type long .

Trouble is I need to store flags in it AND i need to store an integer in it.

I have 6 flags and I don't need to store any number greater than 2^24 so I figured this is possible.

I can reserve 1 byte for the flags and use the other 3 to store the integer.

How can I do this?
0
Comment
Question by:Sandra-24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9813482
Do you need to store negative values? If so, it gets a bit more complicated. If not, try the following:

        long l;

        long value = 10000;
        char flags = 0x3f;

        // store the value and the flags in the long variable

        l = flags << 24 | value;

        // extract the flags

        flags = l >> 24;

        // extract the value
        value = l & 0xffffff;

        printf("flags = %x, value = %u\n", flags, value);
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9813511
You can also try storing a pointer to a struct which could contain whatever you want. But again, working with pointers is very implementation specific.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 400 total points
ID: 9813527
long stuffed;
const long flag0 = 0x01;
const long flag1 = 0x02;
const long flag2 = 0x04;
const long flag3 = 0x08;
//etc through 7

int i;

stuffed = i << 8; // moves integer over to the high end of the word

stuffed |= flag1; // set a flag with or
stuffed &= ~flag3; // unset with and and not

if (stuffed & flag2) {
}

i = stuffed >> 8; // shift right maintaining sign.

Ask if anything here is unclear.

Hope this helps, -bcl
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:bcladd
ID: 9813532
Big-endian, little-endian.

You say to-MAY-to, I say to-MAH-to.

-bcl
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 9813570
Use your own 'long':

struct flagged_long {

    typedef unsigned char flags;

    flagged_long () : m_long ( 0) {};
    flagged_long ( long l) : m_long ( l & 0x00ffffff) {};

    long operator long () const { return m_long & 0x00ffffff;}

    flags operator flags () const { return ( m_long & 0xff000000) >> 24;}

    flagged_long operator=(const long& l) { m_long = l & 0x00ffffff; return *this;}

    flagged_long operator=(const flags& l) { m_long |= l; return *this;}
    flagged_long operator=(const flagged_long& l) { m_long = l.m_long; return *this;}

protected:
    long m_long;
};

0
 
LVL 11

Expert Comment

by:bcladd
ID: 9813672
jkr-

One possible downside of your implicit cast to long is that when the item is stored in the database (passed through some interface, whatever), the type of the provided element is most likely a long and your implicit cast will slice off the flag bits silently.  Might want to make the cast explicit OR use an accessor funciton for one or the other versions of the integer value.

-bcl
0
 
LVL 86

Expert Comment

by:jkr
ID: 9813722
>>Might want to make the cast explicit OR use an accessor funciton

Argl, I knew I forgot something :o)

   long get_long () const { return m_long & 0x00ffffff;}

   flags get_flags () const { return ( m_long & 0xff000000) >> 24;}
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9813723
One reason for storing the number in the upper 24 bits of the long is that the >> operator will replicate the sign bit when the number is right-shifted. This makes storing signed numbers straightforward. Anding with 0x00FFFFFF will simply chop off the upper eight bits without regard for the sign of the numeric field. You could always return  m_long << 8 >> 8 but that doesn't look very pretty.

Just thinking out loud here.
-bcl
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9814234
Try this:

struct MyLong
{
     long              value :24;   // 24 bits signed
     unsigned int   flag1 :1;     // 1 bit   0 or 1
     unsigned int   flag2 :1;
     unsigned int   flag3 :1;
     unsigned int   flag4 :1;
     unsigned int   flag5 :1;
     unsigned int   flag6 :1;
};



int main(int argc, char* argv[])
{
     MyLong  ml;

     ml.value = 1234567;
     ml.flag1 = 1;
     ml.flag2 = 0;
     ml.flag3 = 1;
     ml.flag4 = 0;
     ml.flag5 = 1;
     ml.flag6 = 0;

     int size = sizeof(MyLong);

     // copy to long
     long  l;
     memcpy(&l, &ml, sizeof(long));

     // copy to MyLong
     MyLong   ml1;
     memcpy(&ml, &l, sizeof(long));

     return 0;
}


Regards Alex
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9821097
Thanks for all the responses!

jkr - I couldn't get your code to work, here's how I used it:

flagged_long l;
flagged_long::flags f = 128;
long foo = 78452;
l = foo;
l = f;
std::cout << l.get_long() << std::endl;
std::cout << l.get_flags() << std::endl;

bcladd - Your code worked fine, but with one exception.

if you set some flags, then set the int, it erases the flags.

Is there an easy way around that?
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9821123
Sorry jkr, I meant to add that what was happening was that get_long returned 78452+128 = 78580 and get_flags did nothing.

I don't understand enough of this stuff to offer more than that.
0
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 400 total points
ID: 9821245
Yep, my mistake:

stuffed = i << 8 | (stuffed & 0xFF);

 
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9821315
Thanks, that did the trick. I rolled the code into a struct similar to jkr's to make things simple. An elegant solution if I do say so myself. Thanks guys.

-Sandra
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 318
c++ syntax question 9 67
Safe conversion? 4 88
show out valin of json for debugging in visaul c++ 1 54
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question