Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

the smaller prog C++

88 views
Skip to first unread message

asetof...@gmail.com

unread,
Apr 9, 2015, 8:12:45 AM4/9/15
to
In this link:
http://codegolf.stackexchange.com/questions/12103/generate-a-universal-binary-function-lookup-table
There is a problem: How to print
using less text possible the matrix
0101010101010101
0011001100110011
0000111100001111
0000000011111111
What would be one C++ solution?

asetof...@gmail.com

unread,
Apr 9, 2015, 9:48:53 AM4/9/15
to
So this is my first try
#include<iostream.h>
#include<bitset.h>
int x;
main(){bitset<16>a[]={21845,13107,3855,255};while(x<4)cout<<a[x++]<<"\n";}

Victor Bazarov

unread,
Apr 9, 2015, 9:50:33 AM4/9/15
to
Does your compiler accept that? How old are the tools you're using?..
Never mind, if they work for you, it's fine. Do you get the result you
expect?

V
--
I do not respond to top-posted replies, please don't ask

asetof...@gmail.com

unread,
Apr 9, 2015, 10:13:47 AM4/9/15
to
main(){char z="\n";bitset<16>a=21845,b=13107,c=3855,r=255;cout<<a<<z<<b<<z<<c<<z<<r<<z;}

Victor Bazarov

unread,
Apr 9, 2015, 10:16:30 AM4/9/15
to
On 4/9/2015 10:13 AM, asetof...@gmail.com wrote:
> main(){char z="\n";bitset<16>a=21845,b=13107,c=3855,r=255;cout<<a<<z<<b<<z<<c<<z<<r<<z;}

That's not a valid C++ program. You ought to see it if you run it
through a C++ compiler.

asetof...@gmail.com

unread,
Apr 9, 2015, 11:59:38 AM4/9/15
to
#include<iostream.h>
#include<bitset.h>
#define b(x) bitset<16> x << "\n"
main(){cout<<b(21845)<<b(13107)<<b(3855)<<b(255);}
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Stefan Ram

unread,
Apr 10, 2015, 9:59:53 AM4/10/15
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:
>r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>>(Below, I use »N« for an end-of-line symbol in the source code.)

#include<iostream.h>N#include<bitset.h>Nint x;main(){bitset<16>a[]={21845,13107,3855,255};while(x<4)cout<<a[x++]<<"\n";}
#include<iostream>Nint main(){std::cout<<"0101010101010101\n0011001100110011\n0000111100001111\n0000000011111111\n";}
#include<iostream>Nint main(){for(int i=0;i<4;++i){for(int j=0;j<16;++j)std::cout<<j/(1<<i)%2;std::cout<<'\n';}}
#include<stdio.h>Nint main(){for(int i=0;i<4;++i){for(int j=0;j<16;++j)putchar('0'+j/(1<<i)%2);puts("");}}
#include<stdio.h>Nint main(){int i,j;for(i=0;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}

(Nonportably, »'0'« might be replaced by »48, and often »stdio.h« can be replaced by »cstdio«.)

Message has been deleted
Message has been deleted

Martijn van Buul

unread,
Apr 10, 2015, 10:36:23 AM4/10/15
to
* Stefan Ram:
> #include<stdio.h>Nint main(){int
> i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}
>
> (Nonportably, »'0'« might be replaced by »48«, and often »stdio.h« can be
> replaced by »cstdio«.)

If you're going to be pedantic, assuming '1' equals '0'+1 isn't portable
either.

--
Martijn van Buul - pi...@dohd.org
Message has been deleted

Victor Bazarov

unread,
Apr 10, 2015, 10:43:46 AM4/10/15
to
On 4/10/2015 10:36 AM, Martijn van Buul wrote:
> * Stefan Ram:
>> #include<stdio.h>Nint main(){int
>> i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}
>>
>> (Nonportably, »'0'« might be replaced by »48«, and often »stdio.h« can be
>> replaced by »cstdio«.)
>
> If you're going to be pedantic, assuming '1' equals '0'+1 isn't portable
> either.

Yes, it most certainly is. See [lex.charset]/3, "...In both the source
and execution basic character sets, the value of each character after 0
in the above list of decimal digits shall be one greater than the value
of the previous. "

In my understanding it means that '1' == '0' + 1, and so on.

JiiPee

unread,
Apr 10, 2015, 11:10:14 AM4/10/15
to
yes thats my understanding as well. I remember reading from somewhere
that is it a rule.

Scott Lurndal

unread,
Apr 10, 2015, 11:12:08 AM4/10/15
to
Martijn van Buul <pi...@dohd.org> writes:
>* Stefan Ram:
>> #include<stdio.h>Nint main(){int
>> i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}
>>
>> (Nonportably, »'0'« might be replaced by »48«, and often »stdio.h« can be
>> replaced by »cstdio«.)
>
>If you're going to be pedantic, assuming '1' equals '0'+1 isn't portable
>either.

Although it works in all of the BCD, EBCDIC, UTF-8 (ASCII subset) and USASCII codesets.

Luca Risolia

unread,
Apr 10, 2015, 11:35:28 AM4/10/15
to
On 10/04/2015 16:33, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>>>>> (Below, I use »N« for an end-of-line symbol in the source code.)
>
> #include<iostream.h>N#include<bitset.h>Nint x;main(){bitset<16>a[]={21845,13107,3855,255};while(x<4)cout<<a[x++]<<"\n";}
> #include<iostream>Nint main(){std::cout<<"0101010101010101\n0011001100110011\n0000111100001111\n0000000011111111\n";}
> #include<iostream>Nint main(){for(int i=0;i<4;++i){for(int j=0;j<16;++j)std::cout<<j/(1<<i)%2;std::cout<<'\n';}}
> #include<stdio.h>Nint main(){for(int i=0;i<4;++i){for(int j=0;j<16;++j)putchar('0'+j/(1<<i)%2);puts("");}}
> #include<stdio.h>Nint main(){int i,j;for(i=0;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}
> #include<stdio.h>Nint main(){int i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;++j)putchar('0'+j/(1<<i)%2);}
> #include<stdio.h>Nint main(){int i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;)putchar('0'+j++/(1<<i)%2);}

maybe short, as required, but not really interesting in C++. Try to
produce the whole result in a generic way at compile time instead ;)

Marcel Mueller

unread,
Apr 10, 2015, 12:16:32 PM4/10/15
to
On 10.04.15 16.33, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>>>>> (Below, I use »N« for an end-of-line symbol in the source code.)
>
> #include<iostream.h>N#include<bitset.h>Nint x;main(){bitset<16>a[]={21845,13107,3855,255};while(x<4)cout<<a[x++]<<"\n";}
> #include<stdio.h>Nint main(){int i=0,j;for(;i<4;++i,puts(""))for(j=0;j<16;)putchar('0'+j++/(1<<i)%2);}
#include<stdio.h>Nint main(){int
i=4,j;for(;i--;puts(""))for(j=16;j--;putchar('1'-(j>>3-i)%2));}
#include<stdio.h>Nint
main(){printf("%016b\n%016b\n%016b\n%016b\n",21845,13107,3855,255);}

But %b is AFAIK not part of the standard.


Marcel

asetof...@gmail.com

unread,
Apr 10, 2015, 2:32:32 PM4/10/15
to
int i,r;main(k){for(;k<16;r%16?0:(puts(""),k=i))for(i=0;i<2*k;++i,++r)putchar('1'-(i<k));}
Message has been deleted

asetof...@gmail.com

unread,
Apr 11, 2015, 6:37:40 AM4/11/15
to
#define P printf
#define G(a,b) if(a)goto b
#define u8 unsigned int
#define R Return

u8 i,k,r;main(){++k;m:i=0;v:P(i++<k?"0":"1");++r;G(i<2*k,v);G(r%16,m);P("\n");k=i;G(r<64,m);}

There is someone know why this:
u8 i,k,r;main(){++k;m:i=0;v:P(i<k?"0":"1");++r;G(i++<2*k,v);G(r%16,m);P("\n");k=i;G(r<64,m);}
is not ok?

asetof...@gmail.com

unread,
Apr 11, 2015, 10:12:49 AM4/11/15
to
I wrote:
"u8 i,k,r;main(){++k;m:i=0;v:P(i<k?"0":"1");++r;G(i++<2*k,v);G(r%16,m);P("\n");k=i;G(r<64,m);}
is not ok?"

I make one error in believe macro
expansion was not ok; instead was
one my programming error it would be ++i<2*k

u8 i,k,r;main(){++k;m:i=0;v:P(i<k?"0":"1");++r;G(++i<2*k,v);G(r%16,m);P("\n");k=i;G(r<64,m);}

asetof...@gmail.com

unread,
Apr 11, 2015, 10:36:52 AM4/11/15
to
Stefan Ram wrote:
"#include<stdio.h>Nint main(){for(int i=0;i<64;i%16?0:puts(""))putchar('0'+i++/(1<<(i>>4))%2);}"

#include<stdio.h>Nint i;int main(){m:putchar('0'+i++/(1<<(i>>4))%2);
G(i%16,m);puts("");G(i<64,m)}

The code that use macro
#define G(a,b) if(a)goto b
Would be 4 char more
but it would be + clear to me

asetof...@gmail.com

unread,
Apr 11, 2015, 12:42:11 PM4/11/15
to
#include<iostream.h>
#include<bitset.h>
#define b(x) cout<<bitset<16> x << "\n";
main(){b(21845)b(13107)b(3855)b(255)}
I don't know if it compile...

Öö Tiib

unread,
Apr 12, 2015, 8:08:23 AM4/12/15
to
It is also required by standard to work both in C ([5.2.1]/2) and in
C++ ([lex.charset]/3).

Martijn van Buul

unread,
Apr 13, 2015, 3:39:30 AM4/13/15
to
* Victor Bazarov:
I stand corrected :)
0 new messages