![]() |
|
#1
|
|||
|
|||
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:
Thanks in advance! |
|
#2
|
|||
|
|||
Re: complex assignmentQuote:
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:
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:
Output: Code:
Regards, Dave Footnote: Why did they write CPP / C++ / C Code:
Why not just CPP / C++ / C Code:
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:
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:
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
|
|||
|
|||
Re: Complex assignments (mask and highbit)Dave,
Thanks And by the way. I totally agree with your last statement. Thanks again |
Recent GIDBlog
More photos on Flickr by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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