Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #150513 > unrolled thread
| Started by | Francis Assissi <invalid@invalid.invalid> |
|---|---|
| First post | 2020-03-12 20:18 +0000 |
| Last post | 2020-04-07 09:35 +0200 |
| Articles | 20 on this page of 74 — 20 participants |
Back to article view | Back to comp.lang.c
How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-12 20:18 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-12 20:26 +0000
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-12 20:30 +0000
Re: How to div only once to get remainder and quotient? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-12 20:40 -0700
Re: How to div only once to get remainder and quotient? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-12 16:38 -0400
Re: How to div only once to get remainder and quotient? richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-03-12 20:44 +0000
Re: How to div only once to get remainder and quotient? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-12 17:00 -0400
Re: How to div only once to get remainder and quotient? richard@cogsci.ed.ac.uk (Richard Tobin) - 2020-03-13 09:35 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 11:12 +0100
Re: How to div only once to get remainder and quotient? scott@slp53.sl.home (Scott Lurndal) - 2020-03-12 22:16 +0000
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-12 22:39 +0000
Re: How to div only once to get remainder and quotient? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-12 19:51 -0400
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 00:47 +0000
Re: How to div only once to get remainder and quotient? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-03-12 23:02 -0400
Re: How to div only once to get remainder and quotient? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-12 20:39 -0700
Re: How to div only once to get remainder and quotient? scott@slp53.sl.home (Scott Lurndal) - 2020-03-13 15:00 +0000
Re: How to div only once to get remainder and quotient? Spiros Bousbouras <spibou@gmail.com> - 2020-03-14 08:35 +0000
Re: How to div only once to get remainder and quotient? jak <nospam@please.ty> - 2020-03-12 21:55 +0100
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-12 21:56 +0000
Re: How to div only once to get remainder and quotient? jak <nospam@please.ty> - 2020-03-12 23:26 +0100
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-12 21:11 +0000
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-12 21:57 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-12 22:23 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 01:05 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 11:33 +0100
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 11:03 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 14:31 +0100
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 15:12 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 18:03 +0100
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 18:01 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 19:06 +0100
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 19:12 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 13:34 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 15:00 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 20:34 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-13 21:41 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-14 01:47 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-14 12:24 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-14 16:35 +0000
Re: How to div only once to get remainder and quotient? Bart <bc@freeuk.com> - 2020-03-14 18:05 +0000
Re: How to div only once to get remainder and quotient? already5chosen@yahoo.com - 2020-03-13 03:52 -0700
Re: How to div only once to get remainder and quotient? Guillaume <message@bottle.org> - 2020-03-13 16:41 +0100
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-13 16:07 +0000
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-13 18:10 +0100
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-13 19:32 +0000
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-13 16:08 -0400
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-13 20:50 +0000
Re: How to div only once to get remainder and quotient? jameskuyper@stellarscience.com - 2020-03-13 16:05 -0700
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-13 23:42 +0000
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-13 19:59 -0400
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-14 00:07 +0000
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-13 20:27 -0400
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-14 01:37 +0000
Re: How to div only once to get remainder and quotient? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 07:45 -0700
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-02 08:09 -0700
Re: How to div only once to get remainder and quotient? gazelle@shell.xmission.com (Kenny McCormack) - 2020-04-02 16:03 +0000
Re: How to div only once to get remainder and quotient? Öö Tiib <ootiib@hot.ee> - 2020-04-04 04:31 -0700
Invitations (Was: How to div only once to get remainder and quotient?) gazelle@shell.xmission.com (Kenny McCormack) - 2020-04-04 17:04 +0000
Re: Invitations (Was: How to div only once to get remainder and quotient?) Öö Tiib <ootiib@hot.ee> - 2020-04-07 00:10 -0700
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-13 14:30 -0400
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-13 20:54 +0000
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 23:10 +0000
Re: How to div only once to get remainder and quotient? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-13 20:18 -0400
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-14 00:34 +0000
Re: How to div only once to get remainder and quotient? Francis Assissi <invalid@invalid.invalid> - 2020-03-14 01:47 +0000
Re: How to div only once to get remainder and quotient? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-03-13 14:39 -0600
Re: How to div only once to get remainder and quotient? felix@palmen-it.de (Felix Palmen) - 2020-03-13 22:10 +0100
Re: How to div only once to get remainder and quotient? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-03-13 16:26 -0600
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-14 16:41 +0100
Re: How to div only once to get remainder and quotient? felix@palmen-it.de (Felix Palmen) - 2020-03-14 17:00 +0100
Re: How to div only once to get remainder and quotient? David Brown <david.brown@hesbynett.no> - 2020-03-15 13:39 +0100
Re: How to div only once to get remainder and quotient? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-03-13 23:07 +0000
Re: How to div only once to get remainder and quotient? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2020-03-14 12:13 -0600
Re: How to div only once to get remainder and quotient? Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-07 09:35 +0200
Page 1 of 4 [1] 2 3 4 Next page →
| From | Francis Assissi <invalid@invalid.invalid> |
|---|---|
| Date | 2020-03-12 20:18 +0000 |
| Subject | How to div only once to get remainder and quotient? |
| Message-ID | <r4e5ff$t02$1@dont-email.me> |
The following code serves to convert a decimal number to binary,
but this conversion requires dividing twice: once to get quotient,
another to get the remainder. Is there a function to get both in a
single operation like it is possible with Assembly?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 32
int main(void)
{
int number = 1234567890;
char bin_string[LENGTH] = {0};
char *ptr = bin_string + LENGTH - 1;
memset(bin_string, '0', LENGTH);
*ptr = '\0';
--ptr;
while(number > 0 && ptr > bin_string) {
*ptr = '0' + number % 2;
number /= 2;
--ptr;
}
puts(bin_string);
exit(EXIT_SUCCESS);
}
[toc] | [next] | [standalone]
| From | Bart <bc@freeuk.com> |
|---|---|
| Date | 2020-03-12 20:26 +0000 |
| Message-ID | <jWwaG.513769$5X1.255300@fx06.am4> |
| In reply to | #150513 |
On 12/03/2020 20:18, Francis Assissi wrote:
> The following code serves to convert a decimal number to binary,
> but this conversion requires dividing twice: once to get quotient,
> another to get the remainder. Is there a function to get both in a
> single operation like it is possible with Assembly?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define LENGTH 32
>
> int main(void)
> {
> int number = 1234567890;
> char bin_string[LENGTH] = {0};
> char *ptr = bin_string + LENGTH - 1;
>
> memset(bin_string, '0', LENGTH);
> *ptr = '\0';
> --ptr;
> while(number > 0 && ptr > bin_string) {
> *ptr = '0' + number % 2;
> number /= 2;
> --ptr;
> }
> puts(bin_string);
> exit(EXIT_SUCCESS);
> }
>
Look at the functions div(), ldiv() and lldiv() (eg. google for 'msdn
ldiv'), which do exactly that (via a function call but some compilers
may be able to inline it).
[toc] | [prev] | [next] | [standalone]
| From | Francis Assissi <invalid@invalid.invalid> |
|---|---|
| Date | 2020-03-12 20:30 +0000 |
| Message-ID | <r4e648$t02$2@dont-email.me> |
| In reply to | #150514 |
Impressive, I thought I would find that in math.h but I was wrong.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2020-03-12 20:40 -0700 |
| Message-ID | <8736acsz0u.fsf@nosuchdomain.example.com> |
| In reply to | #150515 |
Francis Assissi <invalid@invalid.invalid> writes:
> Impressive, I thought I would find that in math.h but I was wrong.
Just about everything in math.h operates on floating-point.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2020-03-12 16:38 -0400 |
| Message-ID | <r4e6kb$fk8$1@dont-email.me> |
| In reply to | #150514 |
On March 12, 2020 16:26, Bart wrote:
> On 12/03/2020 20:18, Francis Assissi wrote:
>> The following code serves to convert a decimal number to binary,
>> but this conversion requires dividing twice: once to get quotient,
>> another to get the remainder. Is there a function to get both in a
>> single operation like it is possible with Assembly?
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> #define LENGTH 32
>>
>> int main(void)
>> {
>> int number = 1234567890;
>> char bin_string[LENGTH] = {0};
>> char *ptr = bin_string + LENGTH - 1;
>>
>> memset(bin_string, '0', LENGTH);
>> *ptr = '\0';
>> --ptr;
>> while(number > 0 && ptr > bin_string) {
>> *ptr = '0' + number % 2;
>> number /= 2;
>> --ptr;
>> }
>> puts(bin_string);
>> exit(EXIT_SUCCESS);
>> }
>>
>
> Look at the functions div(), ldiv() and lldiv() (eg. google for 'msdn
> ldiv'), which do exactly that (via a function call but some compilers
> may be able to inline it).
Be aware that these functions may not optimize their calculations, but
simply perform the division and modulo internally.
For instance, this is div(), from one implementation of the standard
library...
#include <stdlib.h>
/* Return the `div_t' representation of NUMER over DENOM. */
div_t
div (int numer, int denom)
{
div_t result;
result.quot = numer / denom;
result.rem = numer % denom;
return result;
}
YMMV.
--
Lew Pitcher
"In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | richard@cogsci.ed.ac.uk (Richard Tobin) |
|---|---|
| Date | 2020-03-12 20:44 +0000 |
| Message-ID | <r4e6vt$2pdn$1@macpro.inf.ed.ac.uk> |
| In reply to | #150517 |
In article <r4e6kb$fk8$1@dont-email.me>,
Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
>Be aware that these functions may not optimize their calculations, but
>simply perform the division and modulo internally.
>
>For instance, this is div(), from one implementation of the standard
>library...
>
> #include <stdlib.h>
> /* Return the `div_t' representation of NUMER over DENOM. */
> div_t
> div (int numer, int denom)
> {
> div_t result;
> result.quot = numer / denom;
> result.rem = numer % denom;
> return result;
> }
They are relying on the compiler doing the optimization.
-- Richard
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2020-03-12 17:00 -0400 |
| Message-ID | <r4e7tm$npo$1@dont-email.me> |
| In reply to | #150518 |
On March 12, 2020 16:44, Richard Tobin wrote:
> In article <r4e6kb$fk8$1@dont-email.me>,
> Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
>
>>Be aware that these functions may not optimize their calculations, but
>>simply perform the division and modulo internally.
>>
>>For instance, this is div(), from one implementation of the standard
>>library...
>>
>> #include <stdlib.h>
>> /* Return the `div_t' representation of NUMER over DENOM. */
>> div_t
>> div (int numer, int denom)
>> {
>> div_t result;
>> result.quot = numer / denom;
>> result.rem = numer % denom;
>> return result;
>> }
>
> They are relying on the compiler doing the optimization.
Which the OP could also do, without the added overhead of a function call.
Remember, the code he wanted to optimize was...
>> *ptr = '0' + number % 2;
>> number /= 2;
--
Lew Pitcher
"In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | richard@cogsci.ed.ac.uk (Richard Tobin) |
|---|---|
| Date | 2020-03-13 09:35 +0000 |
| Message-ID | <r4fk4r$fot$1@macpro.inf.ed.ac.uk> |
| In reply to | #150520 |
In article <r4e7tm$npo$1@dont-email.me>, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote: >> They are relying on the compiler doing the optimization. >Which the OP could also do ... if they knew that it would always be compiled with a compiler that does that optimization. The library implementer is more likely to know what compiler it's intended to be used with, and what optimizations it provides. >without the added overhead of a function call. Yes, that could be significant if the function is not inlined. -- Richard
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2020-03-13 11:12 +0100 |
| Message-ID | <r4fm9h$teb$1@dont-email.me> |
| In reply to | #150537 |
On 13/03/2020 10:35, Richard Tobin wrote: > In article <r4e7tm$npo$1@dont-email.me>, > Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote: > >>> They are relying on the compiler doing the optimization. > >> Which the OP could also do > > ... if they knew that it would always be compiled with a compiler > that does that optimization. > > The library implementer is more likely to know what compiler it's > intended to be used with, and what optimizations it provides. > >> without the added overhead of a function call. > > Yes, that could be significant if the function is not inlined. > The "function call overhead" in cases like this is the lost opportunity for constant propagation and strength reduction. A compiler can turn "x % 2" into an "and" instruction, and a "x / 2" into a shift. It can't do that with a function call where the function definition is in a different unit (unless you are using link-time optimisation - or the compiler uses builtins for standard library functions).
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2020-03-12 22:16 +0000 |
| Message-ID | <mxyaG.967120$Gh7.962352@fx45.iad> |
| In reply to | #150517 |
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>On March 12, 2020 16:26, Bart wrote:
>
>> On 12/03/2020 20:18, Francis Assissi wrote:
>>> The following code serves to convert a decimal number to binary,
>>> but this conversion requires dividing twice: once to get quotient,
>>> another to get the remainder. Is there a function to get both in a
>>> single operation like it is possible with Assembly?
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <string.h>
>>>
>>> #define LENGTH 32
>>>
>>> int main(void)
>>> {
>>> int number = 1234567890;
>>> char bin_string[LENGTH] = {0};
>>> char *ptr = bin_string + LENGTH - 1;
>>>
>>> memset(bin_string, '0', LENGTH);
>>> *ptr = '\0';
>>> --ptr;
>>> while(number > 0 && ptr > bin_string) {
>>> *ptr = '0' + number % 2;
>>> number /= 2;
*ptr = '0' + (number & 1);
number >>= 1;
avoids division entirely. It's likely the compiler would
peephole optimize this anyway.
[toc] | [prev] | [next] | [standalone]
| From | Francis Assissi <invalid@invalid.invalid> |
|---|---|
| Date | 2020-03-12 22:39 +0000 |
| Message-ID | <r4edml$t02$6@dont-email.me> |
| In reply to | #150526 |
> *ptr = '0' + (number & 1); *ptr = '0' ^ number & 1;
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2020-03-12 19:51 -0400 |
| Message-ID | <r4ehuh$r40$1@dont-email.me> |
| In reply to | #150529 |
On March 12, 2020 18:39, Francis Assissi wrote: >> *ptr = '0' + (number & 1); > *ptr = '0' ^ number & 1; While both statements generate the same results, I find the first statement >> *ptr = '0' + (number & 1); clearer as to it's intent. This statement says "*ptr shall contain either the character '0', or the character '0'+1". On the other hand > *ptr = '0' ^ number & 1; says, to me, "*ptr shall contain either an arbitrary set of bits in one pattern or an arbitrary set of bits in a different pattern". The statement implies, to me, that it is inconsequential that the patterns of bits are the same as '0' and '0'+1. If, as your earlier code indicated, you intend that *ptr point to a printable character, then the first statement is clearer than the second. And, I've found that it pays to make your code as clear (as to its intent) as possible. Just my 2 cents worth. -- Lew Pitcher "In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2020-03-13 00:47 +0000 |
| Message-ID | <877dzprsg4.fsf@bsb.me.uk> |
| In reply to | #150530 |
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes: > On March 12, 2020 18:39, Francis Assissi wrote: > >>> *ptr = '0' + (number & 1); >> *ptr = '0' ^ number & 1; > > While both statements generate the same results, I find the first statement >>> *ptr = '0' + (number & 1); > clearer as to it's intent. This statement says "*ptr shall contain either > the character '0', or the character '0'+1". I might write *ptr = "01"[number & 1]; -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2020-03-12 23:02 -0400 |
| Message-ID | <r4et3a$m5j$1@dont-email.me> |
| In reply to | #150532 |
On March 12, 2020 20:47, Ben Bacarisse wrote: > Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes: > >> On March 12, 2020 18:39, Francis Assissi wrote: >> >>>> *ptr = '0' + (number & 1); >>> *ptr = '0' ^ number & 1; >> >> While both statements generate the same results, I find the first >> statement >>>> *ptr = '0' + (number & 1); >> clearer as to it's intent. This statement says "*ptr shall contain either >> the character '0', or the character '0'+1". > > I might write > > *ptr = "01"[number & 1]; Oh. That's /good/. -- Lew Pitcher "In Skills, We Trust"
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2020-03-12 20:39 -0700 |
| Message-ID | <877dzosz28.fsf@nosuchdomain.example.com> |
| In reply to | #150532 |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>
>> On March 12, 2020 18:39, Francis Assissi wrote:
>>
>>>> *ptr = '0' + (number & 1);
>>> *ptr = '0' ^ number & 1;
>>
>> While both statements generate the same results, I find the first statement
>>>> *ptr = '0' + (number & 1);
>> clearer as to it's intent. This statement says "*ptr shall contain either
>> the character '0', or the character '0'+1".
>
> I might write
>
> *ptr = "01"[number & 1];
I might write
*ptr = (number&1)["01"];
if I were in a particularly perverse mood.
(I kid, I kid.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2020-03-13 15:00 +0000 |
| Message-ID | <2eNaG.99390$Bz7.74765@fx24.iad> |
| In reply to | #150530 |
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes: >On March 12, 2020 18:39, Francis Assissi wrote: > >>> *ptr = '0' + (number & 1); >> *ptr = '0' ^ number & 1; > >While both statements generate the same results, I find the first statement >>> *ptr = '0' + (number & 1); >clearer as to it's intent. This statement says "*ptr shall contain either >the character '0', or the character '0'+1". And on all modern processors, addition and exclusive or are both single cycle instructions. Go with readability over cleverness.
[toc] | [prev] | [next] | [standalone]
| From | Spiros Bousbouras <spibou@gmail.com> |
|---|---|
| Date | 2020-03-14 08:35 +0000 |
| Message-ID | <f2yg2fy9e3dU1p2f1R@bongo-ra.co> |
| In reply to | #150530 |
On Thu, 12 Mar 2020 19:51:44 -0400 Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote: > On March 12, 2020 18:39, Francis Assissi wrote: > > >> *ptr = '0' + (number & 1); > > *ptr = '0' ^ number & 1; > > While both statements generate the same results, They generate the same results iff '0' is an even number and the standard does not guarantee this , it only guarantees that '1' == '0' + 1 > I find the first statement > >> *ptr = '0' + (number & 1); > clearer as to it's intent. This statement says "*ptr shall contain either > the character '0', or the character '0'+1". > > On the other hand > > *ptr = '0' ^ number & 1; > says, to me, "*ptr shall contain either an arbitrary set of bits in one > pattern or an arbitrary set of bits in a different pattern". The statement > implies, to me, that it is inconsequential that the patterns of bits are the > same as '0' and '0'+1. Indeed if '0' is odd and number is odd then it would be '0' - 1 .
[toc] | [prev] | [next] | [standalone]
| From | jak <nospam@please.ty> |
|---|---|
| Date | 2020-03-12 21:55 +0100 |
| Message-ID | <r4e7ko$g4d$1@gioia.aioe.org> |
| In reply to | #150513 |
Il 12/03/2020 21:18, Francis Assissi ha scritto:
> The following code serves to convert a decimal number to binary,
> but this conversion requires dividing twice: once to get quotient,
> another to get the remainder. Is there a function to get both in a
> single operation like it is possible with Assembly?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define LENGTH 32
>
> int main(void)
> {
> int number = 1234567890;
> char bin_string[LENGTH] = {0};
> char *ptr = bin_string + LENGTH - 1;
>
> memset(bin_string, '0', LENGTH);
> *ptr = '\0';
> --ptr;
> while(number > 0 && ptr > bin_string) {
> *ptr = '0' + number % 2;
> number /= 2;
> --ptr;
> }
> puts(bin_string);
> exit(EXIT_SUCCESS);
> }
>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 32
int main(void)
{
int number = 1234567890;
char bin_string[LENGTH + 1] = {0};
for(int i = 0; i < LENGTH; i++)
bin_string[(LENGTH - 1)- i] = (number & (1 << i)) ? '1' : '0';
puts(bin_string);
exit(EXIT_SUCCESS);
}
[toc] | [prev] | [next] | [standalone]
| From | Francis Assissi <invalid@invalid.invalid> |
|---|---|
| Date | 2020-03-12 21:56 +0000 |
| Message-ID | <r4eb60$t02$3@dont-email.me> |
| In reply to | #150519 |
Both musl and glibc appears to do two operations like in the OP > (number & (1 << i)) ? '1' : '0'; This is pretty good, jak.
[toc] | [prev] | [next] | [standalone]
| From | jak <nospam@please.ty> |
|---|---|
| Date | 2020-03-12 23:26 +0100 |
| Message-ID | <r4ectu$19ts$1@gioia.aioe.org> |
| In reply to | #150524 |
Il 12/03/2020 22:56, Francis Assissi ha scritto: > Both musl and glibc appears to do two operations like in the OP >> (number & (1 << i)) ? '1' : '0'; > This is pretty good, jak. > Almost certainly, but I think the clock ticks are down.
[toc] | [prev] | [next] | [standalone]
Page 1 of 4 [1] 2 3 4 Next page →
Back to top | Article view | comp.lang.c
csiph-web