Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does this line mean? 4

Status
Not open for further replies.

fortuneman

Technical User
Oct 27, 2000
10
US
Can someone tell me what the following line is saying? or logically doing? Thanks.

print($number&1<<$c?'+':'-');
 
It's the inline conditional format carried over from C/C++. The following would be an equivilent form:
Code:
if($number & 1 << $c) {
    print '+';
} else {
    print '-';
}
The ? and : start where to go if the conditional is true or false. The conditional itself has bitwise operators. I would guess the left shift(<<) has precedence over the and(&).

So you left shift 1 $c places. For example, if $c is 2, then 1 << 2 becomes 100 (in binary). Shift the bits (just a 1 in this case) over the number of spots specified and fill in the empty ones with zeros.

Then it's a bitwise and with whatever is in $number. Let's make $number = 13. Think binary, so 13 is 1101, and from the bit shifting above we had 100. Make it four places to easily match, so 1101 & 0100 = 0100, in decimal is 8.

Since the value of the conditional is 8, perl takes anything non-zero or non-empty to be true. 8 is true, and it would print a '+'.

Been awhile since I've done anything like that. Hope it helps. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks for the response. It makes much more sense now. I can see why people say perl is hard to read. I spent time trying to find out what the ? and the : were doing. Writing it this way:

if($number & 1 << $c) {
print '+';
} else {
print '-';
}

Makes it much easier to read. Thanks again.
 
The cool part about print($number&1<<$c?'+':'-');
is that it accomplishes the same thing in much less typing.

I remember that from 'C' as well. The compile time is faster.

That is an example of where programmers hone there code. This was especially important in the old days when entire companies ran on a 20 MEG hard drive... :)

-pd
 
Fortuneman, isn't it so much more elegant writing a complex piece of code into only a single line though? I mean, if you understand what it does, then being able to write so few characters to achieve it is very cool. It's like writing a whole novel into one paragraph. Just look at what it took icrf to explain it ;)
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Definitely adds the &quot;cool&quot; factor into your programming :). Now that I understand what it does it'll be easier for me to read it. Although for small programs and as a &quot;courtesy&quot; to other who will need to look at it I might be benificial to write it the long way. My lack of C experience is showing here though :).
 
I think it would suffice to simply comment it well.
Code:
# output a &quot;+&quot; if..., and a &quot;-&quot; if...
print($number&1<<$c?'+':'-');
Then you don't even have to be able to read it as long as you know the function that it performs. There's no need to make the code larger and more unwieldy for those who don't understand the more condensed form. Once you understand it, the shorter the code, the easier it is to read and debug because you don't have to scroll multiple pages to see your entire subroutine.

BTW, icrf, 0100 is 4, not 8 :)
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Oops! Thank god for checks and balances. It was close, gotta count for something, right? A typo's one thing, but I think I repeated it at least twice. *smacks self* ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Gday all,

Don't want to start a religious war here, but saying &quot;perl is hard to read&quot; is not really correct (IMHO): anyone can write a program that is hard to read in any language! Just try supporting some of them!!

But the way I read the ternary operator ( .. ? .. : .. ) for

$result = $cond ? $a : $b;

is

&quot;check $cond. If it is non-zero, assign $a to $result, otherwise assign $b to result&quot;.

I think it is a brilliant operator, when used correctly. (Some spaces in the statement in the original question probably wouldn't hurt).

My 2c.

Regards,
Pat
 
Have never used it, never ever.

Can't imagine a situation where I would. (hanging over burning coals - maybey, but not otherwise....) obfuscation at its worst. :) Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Mike, just look at the alternatives...
Code:
# $result is $a if $cond, or $b otherwise
my $result = $cond ? $a : $b;
vs
Code:
# $result is $a if $cond, or $b otherwise
my $result;
if ($cond)
{
  $result = $a;
}
else
{
  $result = $b;
}

I think the first one is actually easier to read since it is all on one line. Far more elegant. As long as you include a comment line (this goes for both), then it is very easy to read.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top