Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > integers to binary

Reply
Thread Tools

integers to binary

 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-14-2005
On 14 Dec 2005 03:51:59 -0800, in comp.lang.c , Tim Rentsch
<(E-Mail Removed)> wrote:

(on the topic of the 'niceness' of)
> >>> buffer[i] = (n & j) == j ? '1' : '0';

>
>The original single assignment is precisely the sort of situation
>where using the ?: operator makes sense.


Perhaps, once suitably parenthesised so that future maintenance drones
can understand it easily and w/o recourse to a manual of operator
precedence.
And it'd be a bit different if it were

foobar = (banana(12,34,pip(),333, printf("hello"))&&z)==blabla(bl,pl,
ypr)?pfngetarraycnt(yaddayadda,4) : atoi(getstringfrom(ww));

which is technically also a single assignment.

>If Mr. McIntyre says he
>finds the if/else form with two assignments more readable, I'm expect
>that's so, but his reaction isn't shared by everyone.


Of course. Some people /like/ obfuscated code

>IMO the
>single assignment using ?: is both more readable and more clear
>than using if/else and two assignments.


YMMV.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      12-14-2005
Pieter Droogendijk wrote:
> On Tue, 13 Dec 2005 15:42:49 +0000, rayw wrote:
>> "Pieter Droogendijk" <(E-Mail Removed)> wrote:

>

.... snip ...
>>>
>>> This expression could be nicer. How's this:
>>>
>>> if ( (n & j) == j ) {
>>> buffer[i] = '1';
>>> }
>>> else {
>>> buffer[i] = '0';
>>> }

>>
>> I agree that to some it's more readable, but as it's more lines
>> of code, and duplicates the assignment etc, I think it's more
>> prone to errors later - like if someone changes it.

>
> Sure it's six lines for what could be one assignment, but it's
> as clear as it's going to get. That's what I aim for, Unless
> obfuscation is what I want.


How about:

if ((n & j) == j) buffer[i] = '1';
else buffer[i] = '0';

Just a matter of style. Fairly hard to misread.

More important is the meaning of "(n & j) == j", which expresses
the assertion that all bits set in j are set in n. Thus another
alternative could be:

buffer[i] = '0' + ((n & j) == j);

All this exposes some possible portability problems, dependent on
the types of n and j.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
 
Reply With Quote
 
 
 
 
Dave Thompson
Guest
Posts: n/a
 
      12-19-2005
On Tue, 13 Dec 2005 19:06:40 +0000, Mark McIntyre
<(E-Mail Removed)> wrote:
<snip>
> IME readable code is considerably easier to maintain than unreadable
> code. Write for readability first. Consider clever tricks only if
> trying to obfuscate code.
>
> if ( (n & j) == j )
> buffer[i]='1';
> else
> buffer[i]='0';
>

<snip: rather than conditional aka 'ternary' expression>

Agree on the principle, disagree strongly on the result.

The conditional operator in C is not obfuscation when it matches the
operation you want to perform, and here that fact that we want to put
one or another digit value in the _same_ output location is important.

What about (slightly sanitized from code I've worked on):
if( (n & j) == j )
currentOutputPtr->outputControl.workingbuffer[1].byte[i] = '1';
else
currentOutputPtr->outputControl.workingbuffer[1].byte[i] = '0';

Or suppose it were octal:
if( ( inputField.workingvalue4 >> shiftthisdigit & 7) == 0 )
currentOutputPtr->outputControl.workingbuffer[1].byte[i] = '0';
else if( ( inputField.workingvalue4 >> shiftthisdigit & 7) == 1 )
currentOutputPtr->outputControl.workingbuffer[1].byte[i] = '1';
and so on up to 7

and during maintenance you need to change to use workingbuffer[3]
will you get it right -- or if not, easily see, or test, the error?

Do you consider some_long[and].complicated->expression += 1
to be obfuscated for some_long[and].complicated->expression
= some_long[and].complicated->expression + 1? AFAIK compound
assignments are unique to the C family among HLLs.

In fact, what I would consider obfuscated is the (n & j) == j part. In
the example upthread j is always one bit and to me the natural way of
expressing "the/any bit on" is (n & j) != 0 -- or in a boolean context
like this just (n & j), but that's a whole 'nother argument.
To me (n & j) == j implies that we are at least sometimes looking for
multiple bits, and then I find it at least as clear to deMorganize to
"no masked bits off" and write !(~n & j) .

DG(N?)D.

- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-19-2005
On Mon, 19 Dec 2005 03:20:49 GMT, in comp.lang.c , Dave Thompson
<(E-Mail Removed)> wrote:

>On Tue, 13 Dec 2005 19:06:40 +0000, Mark McIntyre
><(E-Mail Removed)> wrote:
><snip>
>> IME readable code is considerably easier to maintain than unreadable
>> code. Write for readability first. Consider clever tricks only if
>> trying to obfuscate code.

>
>Agree on the principle, disagree strongly on the result.


Thats fine - I was advocating readability rather than removal of the
ternary operator.

>The conditional operator in C is not obfuscation when it matches the
>operation you want to perform,


Cautiously agree, with caveat that examples the other way are easy to
find too.

(snip examples of where ternary would be considerably clearer)


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      12-19-2005
In article <dnmq5p$ibi$(E-Mail Removed)> "rayw" <(E-Mail Removed)> writes:
> "Pieter Droogendijk" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed) OR.ukME...
> > On Tue, 13 Dec 2005 10:21:09 +0000, rayw wrote:

....
> >> buffer[i] = (n & j) == j ? '1' : '0';

> >
> > This expression could be nicer. How's this:
> >
> > if ( (n & j) == j ) {
> > buffer[i] = '1';
> > }
> > else {
> > buffer[i] = '0';
> > }

>
> I agree that to some it's more readable, but as it's more lines of code, and
> duplicates the assignment etc, I think it's more prone to errors later -
> like if someone changes it.


Why not just: buffer[i] = (n & j) == j;
?
> >> // itoa() not a standard ISO function.
> >> //
> >> itoa(n, buffer, 2);

> >
> > Indeed it isn't. I don't have it.
> >
> > Though according to http://www.mkssoftware.com/docs/man3/itoa.3.asp this
> > should work, unless I've gone blind.

>
> Thanks for the ref and the comments.


I think itoa expects an int and you pass it a (probably) signed char.
You ought to know how a signed char with a negative value is passed
in a position where an int is expected.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      12-19-2005
In article <(E-Mail Removed)> "Dik T. Winter" <(E-Mail Removed)> writes:
> In article <dnmq5p$ibi$(E-Mail Removed)> "rayw" <(E-Mail Removed)> writes:
> > "Pieter Droogendijk" <(E-Mail Removed)> wrote in message
> > news(E-Mail Removed) OR.ukME...
> > > On Tue, 13 Dec 2005 10:21:09 +0000, rayw wrote:

> ...
> > >> buffer[i] = (n & j) == j ? '1' : '0';
> > >
> > > This expression could be nicer. How's this:
> > >
> > > if ( (n & j) == j ) {
> > > buffer[i] = '1';
> > > }
> > > else {
> > > buffer[i] = '0';
> > > }

> >
> > I agree that to some it's more readable, but as it's more lines of code, and
> > duplicates the assignment etc, I think it's more prone to errors later -
> > like if someone changes it.

>
> Why not just: buffer[i] = (n & j) == j;
> ?


Sorry, an error. Should be: buffer[i] = ((n & j) == j) + '0';
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-19-2005
Dik T. Winter wrote:
>
> In article <dnmq5p$ibi$(E-Mail Removed)> "rayw"
> <(E-Mail Removed)> writes:
> > "Pieter Droogendijk" <(E-Mail Removed)>
> > wrote in message
> > news(E-Mail Removed) OR.ukME...
> > > On Tue, 13 Dec 2005 10:21:09 +0000, rayw wrote:

> ...
> > >> buffer[i] = (n & j) == j ? '1' : '0';
> > >
> > > This expression could be nicer. How's this:
> > >
> > > if ( (n & j) == j ) {
> > > buffer[i] = '1';
> > > }
> > > else {
> > > buffer[i] = '0';
> > > }

> >
> > I agree that to some it's more readable,
> > but as it's more lines of code, and
> > duplicates the assignment etc,
> > I think it's more prone to errors later -
> > like if someone changes it.

>
> Why not just: buffer[i] = (n & j) == j;
> ?


The above examples indicate that he wants
either a '1' or '0' in buffer[i],
rather than either a 1 or 0.

--
pete
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      12-28-2005
Mark McIntyre <(E-Mail Removed)> writes:

> On 14 Dec 2005 03:51:59 -0800, in comp.lang.c , Tim Rentsch
> <(E-Mail Removed)> wrote:
>
> (comparison of using if/else or ?: for assigning to buffer[i])
>
> >If Mr. McIntyre says he
> >finds the if/else form with two assignments more readable, I'm expect
> >that's so, but his reaction isn't shared by everyone.

>
> Of course. Some people /like/ obfuscated code


Everyone's entitled to an opinion. IME, however, those
who express opinions and little else, and who don't bother
to differentiate between what is opinion and what is fact,
rarely say anything worth listening to.
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-28-2005
Tim Rentsch wrote:
> Mark McIntyre <(E-Mail Removed)> writes:
>> Tim Rentsch <(E-Mail Removed)> wrote:
>>
>> (comparison of using if/else or ?: for assigning to buffer[i])
>>
>>> If Mr. McIntyre says he finds the if/else form with two
>>> assignments more readable, I'm expect that's so, but his
>>> reaction isn't shared by everyone.

>>
>> Of course. Some people /like/ obfuscated code

>
> Everyone's entitled to an opinion. IME, however, those who
> express opinions and little else, and who don't bother to
> differentiate between what is opinion and what is fact, rarely
> say anything worth listening to.


In these style matters, all is opinion. The fact is that the
opinions vary in close correlation with the language flavor on
which one has been brought up. Users of languages without esoteric
symbol reuse (such as Pascal) will almost always eschew funny
statements involving question marks and colons. In the long run it
shouldn't matter, since a moderately intelligent compiler will
produce the same object code for either style.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-28-2005
On Wed, 28 Dec 2005 15:52:58 -0500, in comp.lang.c , "Chuck F. "
<(E-Mail Removed)> wrote:

>Tim Rentsch wrote:
>> Mark McIntyre <(E-Mail Removed)> writes:
>>> Of course. Some people /like/ obfuscated code

>>
>> Everyone's entitled to an opinion.

>
>In these style matters, all is opinion.

....
>In the long run it
>shouldn't matter, since a moderately intelligent compiler will
>produce the same object code for either style.


I guess my /opinion/ is that you don't write code to be legible by
the compiler, but by the maintenance droid in 3 years time. And for
anyone who says maintenance droids ought to be smart enough to figure
out your code, I direct your attention to the sig below.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
Picking out integers from a binary character string rusttree@gmail.com C++ 1 06-12-2005 03:19 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments