Thread: Bit operations - problem with rereading the data

  1. #1
    Registered User Quentin028's Avatar
    Join Date
    Sep 2012
    Posts
    5

    Bit operations - problem with rereading the data

    I have a problem with bit operations. After packing a couple of numbers into one variable and trying to reread those values later, the output of the info function differs from the one of main function.

    Code:
    #include <iostream>using namespace std;
    
    
    unsigned long long extract(unsigned long long value, int begin, int end)
    {
        unsigned long long mask = (1 << (end - begin)) - 1;
        return (value >> begin) & mask;
    }
    
    
    unsigned long long encode(int caller, int caller_zone, int callee, int callee_zone, int duration, int tariff)
    {
        unsigned long long encoded = 0;
    
    
        encoded |= caller;
        encoded <<= 17;
    
    
        encoded |= caller_zone;
        encoded <<= 7;
    
    
        encoded |= callee;
        encoded <<= 17;
    
    
        encoded |= callee_zone;
        encoded <<= 7;
    
    
        encoded |= duration;
        encoded <<= 13;
    
    
        encoded |= tariff;
    
    
        return encoded;
    }
    
    
    void info(unsigned long long u)
    {
        int caller = extract(u, 47, 64);
        int caller_zone = extract(u, 40, 47);
        int callee = extract(u, 23, 40);
        int callee_zone = extract(u, 16, 23);
        int duration = extract(u, 3, 16);
        int tariff = extract(u, 0, 3);
    
    
        cout << "caller:        " << caller << endl
             << "caller_zone:   " << caller_zone << endl
             << "callee:        " << callee << endl
             << "callee_zone:   " << callee_zone << endl
             << "duration:      " << duration << endl
             << "tariff:        " << tariff << endl;
    }
    
    
    int main()
    {
        int caller = 130999;    //17-bit number
        int caller_zone = 101;  //7-bit number
        int callee = 7777;      //17-bit number
        int callee_zone = 99;   //7-bit number
        int duration = 7000;    //13-bit number
        int tariff = 6;         //3-bit number
    
    
        cout << " FROM MAIN" << endl;
        cout << "caller:        " << caller << endl
             << "caller_zone:   " << caller_zone << endl
             << "callee:        " << callee << endl
             << "callee_zone:   " << callee_zone << endl
             << "duration:      " << duration << endl
             << "tariff:        " << tariff << endl;
    
    
        unsigned long long u = encode(caller, caller_zone, callee, callee_zone, duration, tariff);
    
    
        cout << "\n FROM INFO" <<endl;
        info(u);
    }
    Do you know what am I missing here?
    Last edited by Quentin028; 11-11-2015 at 11:50 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your encode looks incorrect to me. You should start by making encoded equal to caller, then shift the number left 7 bits to accommodate caller_zone, then add caller_zone to encoded. Right now your making encoded equal to caller then shifting 17 bits.

    Code:
    130999 == 1 1111 1111 1011 0111
    101      == 110 0101
    
    After combining these two numbers you should end up with:
    1111 1111 1101 1011 1110 0101
    
    But you end up with:
    11111111110110111000000000011001010000000
    I also think your decode is incorrect as well, but until you get the encode correct there is no reason to worry about decoding.

    Jim

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    With all those magic numbers and strange things going on I gave up trying to understand and just said "Hodor!" (and, no, Hodor is not scared of bit/bitwise operations and/or manipulation)
    Last edited by Hodor; 11-12-2015 at 09:50 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know if you'd be interested, but C++ provides a built-in method for just that, namely bit fields. For example:
    Code:
    struct myBitField
    {
        unsigned long long caller : 17;
        unsigned long long caller_zone : 7;
        unsigned long long callee : 17;
        unsigned long long callee_zone : 7;
        unsigned long long duration : 13;
        unsigned long long tariff : 3;
    };
    You can use the members as plain struct ones, and the whole struct has "sizeof(unsigned long long)". If you want to manipulate the whole value, you can create a union, like so:

    Code:
    union myBundle
    {
        myBitField bitField;
        unsigned long long llvalue;
    };
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmaps I/O operations - bitmap data order
    By barracuda in forum C Programming
    Replies: 22
    Last Post: 02-23-2015, 09:43 AM
  2. Replies: 4
    Last Post: 10-29-2014, 04:02 AM
  3. Rereading a data file
    By tayexdrums in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 10:27 PM
  4. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  5. Input Operations Problem
    By 1rwhites in forum C Programming
    Replies: 3
    Last Post: 10-03-2005, 05:43 PM