GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jan-2008, 14:00
aedelectric aedelectric is offline
New Member
 
Join Date: Jan 2008
Posts: 2
aedelectric is on a distinguished road

Complex assignments (mask and highbit)


Hello,

I have two complex assignments (mask and highbit) that I need explained. My experience in C++ is from my college days 10 + years ago.
The following code is just a small piece of the code that controlled a machine and now is being converted to a Programmable Automation Controller (PAC) based system.

CPP / C++ / C Code:

#include <string.h>
#include <stdio.h>

const int order = 32;

mask = ((((unsigned long)1<<(order-1))-1)<<|1;
highbit = (unsigned long)1<<(order-1);


Thanks in advance!
  #2  
Old 02-Jan-2008, 14:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: complex assignment


Quote:
Originally Posted by aedelectric
I have...


Most current C compilers, other than small processors used in some embedded systems, have 32-bit ints.

Some compilers for processors used in some embedded systems have 16-bit ints, so, using long int data types is probably a way to make sure that 32-bit variables are used. Some systems (64-bit compilers on 64-bit systems) have 64-bit long ints, otherwise long ints are "usually" 32-bit quantities.


All compilers that you are likely to run across these days use two's complement representation for integer data types.

So, with the assumptions that the long int data types are 32-bits wide and two's complement representation is used, we continue:


The expression for highbit just gives something with a 1 in bit position 31 (32nd bit counting from bit position zero on the right) of a long int data type. So, if it is a 32-bit data type, it has a one in the upper bit and zeros in the other bit positions. (Thus, the name "highbit.")

The "mask" statement won't compile. My guess would be that they might have meant something more like:
CPP / C++ / C Code:
    mask = ((((unsigned long)1 << (order - 1)) - 1)<< 1) | 1;

This simply gives something with 1's in the lowest 32 bits of a long int data variable.


If the expressions seem too complex, then break them down into steps.

CPP / C++ / C Code:
#include <string.h>
#include <stdio.h>

int main()
{
    const int order = 32;
    unsigned long highbit;
    unsigned long x;
    unsigned long mask;

    printf("sizeof(unsigned long) = %d\n\n", sizeof(unsigned long));

    printf("highbit is 1 shifted left 31 times:\n");
    highbit = (unsigned long)1 << (order - 1);
    printf("    highbit = 0x%08lx\n\n", highbit);

    /*mask = ((((unsigned long)1<<(order-1))-1)<<|1; */ /* Huh??? */
    mask = ((((unsigned long)1 << (order -1)) - 1)<< 1) | 1; /* my guess */
    printf("mask = 0x%08lx\n", mask);

    printf("Now, let x = highbit - 1:\n");
    x = highbit - 1;
    printf("     x       = 0x%08lx\n\n", x);

    printf("Now, shift x left by one bit:\n");
    x <<= 1;
    printf("     x       = 0x%08lx\n\n", x);

    printf("Now, or a '1' into the lowest bit:\n");
    x |= 1;
    printf("     x       = 0x%08lx\n\n", x);

    printf("Of course you could have done it a couple of \"easier\" ways.\n\n");

    printf("For example, just let x = (unsigned)(-1):\n");
    x = (unsigned)(-1);
    printf("     x       = 0x%08lx\n\n", x);

    printf("Or, just use x = ~0:\n");
    x = ~0;
    printf("     x       = 0x%08lx\n", x);
    return 0;
}

Output:
Code:
sizeof(unsigned long) = 4 highbit is 1 shifted left 31 times: highbit = 0x80000000 mask = 0xffffffff Now, let x = highbit - 1: x = 0x7fffffff Now, shift x left by one bit: x = 0xfffffffe Now, or a '1' into the lowest bit: x = 0xffffffff Of course you could have done it a couple of "easier" ways. For example, just let x = (unsigned)(-1): x = 0xffffffff Or, just use x = ~0: x = 0xffffffff

Regards,

Dave

Footnote: Why did they write
CPP / C++ / C Code:
    highbit = (unsigned long)1 << (order - 1);

Why not just
CPP / C++ / C Code:
    highbit = 1 << (order - 1);

Well, if the system has 16-bit ints, then without the cast the expression would have evaluated to zero. (The right-hand side evaluated as a 16-bit int: before the assignment: A 1 shifted left 32 bits truncated to an unsigned 16-bit int would have a value of zero.)

P.S.

If they wanted a 32-bit quantity with 1 in the upper bit and zeros everywhere else, instead of those silly constant expressions, whose values are known at compile time, why didn't they just write
CPP / C++ / C Code:
    highbit = 0x80000000;

Similarly for the mask; if they wanted a 32-bit quantity with 1's in all bit positions, why (oh, why...) didn't they just write
CPP / C++ / C Code:
    mask = 0xffffffff;

I can't imagine...

Now, if order were not a constant, but something that would be assigned at run-time, then maybe (just maybe) I might go for some kind of computed values for mask and highbit, but, I mean, really...


People used to refer to a deliberately obfuscatory coding style, as "job security" coding. (Maybe jokingly; maybe not.) I call it "don't let the door hit you in the butt on your way out" coding.
Last edited by davekw7x : 02-Jan-2008 at 15:49.
  #3  
Old 03-Jan-2008, 12:39
aedelectric aedelectric is offline
New Member
 
Join Date: Jan 2008
Posts: 2
aedelectric is on a distinguished road

Re: Complex assignments (mask and highbit)


Dave,

Thanks
And by the way. I totally agree with your last statement.

Thanks again
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Am I reading this assignment correctly? earachefl C++ Forum 2 09-May-2006 09:39
complex number raised to a complex Darth Predator C++ Forum 6 06-Nov-2005 20:20
Circular Linked Queue Copy Constructor and Assignment Operator Not Working? wc3promet C++ Forum 0 17-Oct-2004 07:55
complex number tinzi C++ Forum 2 01-Jun-2004 14:47
help with c++ object assignment Mjkramer21 C++ Forum 31 14-Apr-2004 18:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.