Thread: [Dev-C++] Character variables
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Giligun <too...@ya...> - 2003-03-04 22:01:43
|
How does one get a char variable to hold more than one character? Or what other type should be used? __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com |
|
From: Per W. <pw...@ia...> - 2003-03-04 22:25:20
|
short s = 'ab';
int i = 'abcd';
given that int is 4 byte.
But a lot of compilers will give you warnings, since it you can't be sure
if memcpy(buf,&s,sizeof(s)) will give you
buf[0] = 'a', buf[1] = 'b';
or
buf[0] = 'b', buf[1] = 'a';
In short - be very careful about using this feature. One place where it's
elegant to use is if you are reading a tagged file, where each tag is
4-byte long:
char *buf;
int tag = 0;
for (i = 0; i < 4; i++) {
tag = (tag << 8) | *buf++;
}
switch (tag) {
case 'STRT': ...; break;
case 'HEDR': ...; break;
case 'DATA': ...; break;
case 'ENDF': ...; break;
}
If there are a lot of tags, the result is a very fast matching of the
tags.
/Per W
On Tue, 4 Mar 2003, Giligun wrote:
> How does one get a char variable to hold more than one
> character? Or what other type should be used?
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger
> for complex code. Debugging C/C++ programs can leave you feeling lost and
> disoriented. TotalView can help you find your way. Available on major UNIX
> and Linux platforms. Try it free. www.etnus.com
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <no...@ho...> - 2003-03-04 22:53:10
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0=EC=
=0D
To: Giligun=0D
Cc: C++=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
short s =3D 'ab';=0D
int i =3D 'abcd';=0D
=0D
given that int is 4 byte.=0D
=0D
=0D
=3D=3D> That isn't valid C++. And where it gets compiled you can't consid=
er it
as safe.=0D
=0D
=0D
=0D
But a lot of compilers will give you warnings, since it you can't be sure=
=0D
if memcpy(buf,&s,sizeof(s)) will give you=0D
buf[0] =3D 'a', buf[1] =3D 'b';=0D
or=0D
buf[0] =3D 'b', buf[1] =3D 'a';=0D
=0D
In short - be very careful about using this feature. One place where it's=
=0D
elegant to use is if you are reading a tagged file, where each tag is=0D
4-byte long:=0D
char *buf;=0D
=0D
int tag =3D 0;=0D
for (i =3D 0; i < 4; i++) {=0D
tag =3D (tag << 8) | *buf++;=0D
}=0D
=0D
=0D
=3D=3D> Too system dependent. Why don't you use wchar_t instead? But sinc=
e he
wants to store characters (assuming in the range 0-127) he can use a char
sequence nicely and easily. =0D
=0D
switch (tag) {=0D
case 'STRT': ...; break;=0D
case 'HEDR': ...; break;=0D
case 'DATA': ...; break;=0D
case 'ENDF': ...; break;=0D
}=0D
=0D
If there are a lot of tags, the result is a very fast matching of the=0D
tags.=0D
=0D
=0D
=3D=3D> By the way i would do something like the above using std::bitset.=
=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Ioannis V. <no...@ho...> - 2003-03-04 22:59:39
|
Some adjustments:=0D
=0D
=0D
=0D
-------Original Message-------=0D
=0D
From: Ioannis Vranos=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:55:26 =F0=EC=
=0D
To: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0=EC=
=0D
To: Giligun=0D
Cc: C++=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
short s =3D 'ab';=0D
int i =3D 'abcd';=0D
=0D
given that int is 4 byte.=0D
=0D
=0D
=3D=3D> That isn't valid C++. And where it gets compiled you can't consid=
er it
as safe.=0D
=0D
=0D
=0D
But a lot of compilers will give you warnings, since it you can't be sure=
=0D
if memcpy(buf,&s,sizeof(s)) will give you=0D
buf[0] =3D 'a', buf[1] =3D 'b';=0D
or=0D
buf[0] =3D 'b', buf[1] =3D 'a';=0D
=0D
In short - be very careful about using this feature. One place where it's=
=0D
elegant to use is if you are reading a tagged file, where each tag is=0D
4-byte long:=0D
char *buf;=0D
=0D
int tag =3D 0;=0D
for (i =3D 0; i < 4; i++) {=0D
tag =3D (tag << 8) | *buf++;=0D
}=0D
=0D
=0D
=3D=3D> Too system dependent. Additionally bitwise operators are to be us=
ed in
unsigned integrals only.=0D
=0D
=3D=3D> He could use wchar_t instead. But since he wants to store charact=
ers
(assuming in the range 0-127) he can use a char sequence nicely and easil=
y. =0D
=0D
=0D
switch (tag) {=0D
case 'STRT': ...; break;=0D
case 'HEDR': ...; break;=0D
case 'DATA': ...; break;=0D
case 'ENDF': ...; break;=0D
}=0D
=0D
If there are a lot of tags, the result is a very fast matching of the=0D
tags.=0D
=0D
=0D
=3D=3D> By the way i would do something like the above using std::bitset.=
=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-03-05 12:05:58
|
The shift operators are of course defiend for both signed and unsigned
data types.
It's just that for >> you may not assume if it will shift in a '0' or a
'1' in the most significant bit fir signed integers, i.e.
-3 (0xfffd) >> 1 =3D 32766 (0x7ffe)
-3 (0xfffd) >> 1 =3D -2 (0xfffe)
depending on implementation.
However, when shifting in characters into a 32-bit integer, I don't have
to care about this, since << always shifts in 0 in the least significant
bit.
/Per W
On Wed, 5 Mar 2003, Ioannis Vranos wrote:
> Some adjustments:
>
>
>
> -------Original Message-------
>
> From: Ioannis Vranos
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:55:26 =F0=EC
> To: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Character variables
>
> -------Original Message-------
>
> From: Per Westermark
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0=EC
> To: Giligun
> Cc: C++
> Subject: Re: [Dev-C++] Character variables
>
> short s =3D 'ab';
> int i =3D 'abcd';
>
> given that int is 4 byte.
>
>
> =3D=3D> That isn't valid C++. And where it gets compiled you can't consid=
er it
> as safe.
>
>
>
> But a lot of compilers will give you warnings, since it you can't be sure
> if memcpy(buf,&s,sizeof(s)) will give you
> buf[0] =3D 'a', buf[1] =3D 'b';
> or
> buf[0] =3D 'b', buf[1] =3D 'a';
>
> In short - be very careful about using this feature. One place where it's
> elegant to use is if you are reading a tagged file, where each tag is
> 4-byte long:
> char *buf;
>
> int tag =3D 0;
> for (i =3D 0; i < 4; i++) {
> tag =3D (tag << 8) | *buf++;
> }
>
>
> =3D=3D> Too system dependent. Additionally bitwise operators are to be us=
ed in
> unsigned integrals only.
>
> =3D=3D> He could use wchar_t instead. But since he wants to store charact=
ers
> (assuming in the range 0-127) he can use a char sequence nicely and easil=
y.
>
>
> switch (tag) {
> case 'STRT': ...; break;
> case 'HEDR': ...; break;
> case 'DATA': ...; break;
> case 'ENDF': ...; break;
> }
>
> If there are a lot of tags, the result is a very fast matching of the
> tags.
>
>
> =3D=3D> By the way i would do something like the above using std::bitset.
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2003-03-05 12:22:08
|
-------Original Message-------=0D =0D From: Per Westermark=0D Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 2:04:36 =EC=EC=0D To: Ioannis Vranos=0D Cc: Dev-C++ Mailing List=0D Subject: Re: [Dev-C++] Character variables=0D =0D The shift operators are of course defiend for both signed and unsigned=0D data types.=0D =0D It's just that for >> you may not assume if it will shift in a '0' or a=0D '1' in the most significant bit fir signed integers, i.e.=0D =0D -3 (0xfffd) >> 1 =3D 32766 (0x7ffe)=0D -3 (0xfffd) >> 1 =3D -2 (0xfffe)=0D =0D depending on implementation.=0D =0D However, when shifting in characters into a 32-bit integer, I don't have=0D to care about this, since << always shifts in 0 in the least significant=0D bit.=0D =0D =0D =3D=3D> Yes bitwise operators can be applied to integral types signed and unsigned however in signed types the behaviour is implementation dependent/undefined. This is explicitly mentioned in the C99 standard:=0D =0D =3D=3D> "Some operators (the unary operator ~, and the binary operators <= <, >>, &, ^, and |,=0D collectively described as bitwise operators) are required to have operand= s that have=0D integer type. These operators return values that depend on the internal representations of=0D integers, and have implementation-defined and undefined aspects for signe= d types."=0D =0D =0D =3D=3D> The above is not mentioned explicitly in the C++98 standard, but = it is common sense. That's why i spoke about bad magic. :)=0D =0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-03-05 12:53:30
|
And if you check closer, for shift operations it's the >> that is implementation specific, i.e. if it will do an arithmetic or logic shift right for signed numbers. The << is always ok, i.e. will always shift in a zero. Is's only when you try to use it to multiply with 2, you have to note that because of overflow, you might shift away your sign bit, resulting in a bit of confusion, i.e. [16-bit integers]: -20000 (0xB1E0) << 1 results in 25536 (0x63c0) but then all operators will give more or less strange results when operated outside the available numeric range. About compatibility with strange architectures, I don't expect us to have to see too many machines with 9-bit characters and 36-bit integers anymore, as existed for some big old iron. Such a computer manufacturer would now have too hard a time trying to attract both compiler vendors and actual end users. /Per W On Wed, 5 Mar 2003, Ioannis Vranos wrote: > -------Original Message------- > > From: Per Westermark > Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 2:04:36 =EC=EC > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: Re: [Dev-C++] Character variables > > The shift operators are of course defiend for both signed and unsigned > data types. > > It's just that for >> you may not assume if it will shift in a '0' or a > '1' in the most significant bit fir signed integers, i.e. > > -3 (0xfffd) >> 1 =3D 32766 (0x7ffe) > -3 (0xfffd) >> 1 =3D -2 (0xfffe) > > depending on implementation. > > However, when shifting in characters into a 32-bit integer, I don't have > to care about this, since << always shifts in 0 in the least significant > bit. > > > =3D=3D> Yes bitwise operators can be applied to integral types signed and > unsigned however in signed types the behaviour is implementation > dependent/undefined. This is explicitly mentioned in the C99 standard: > > =3D=3D> "Some operators (the unary operator ~, and the binary operators <= <, >>, > &, ^, and |, > collectively described as bitwise operators) are required to have operand= s > that have > integer type. These operators return values that depend on the internal > representations of > integers, and have implementation-defined and undefined aspects for signe= d > types." > > > =3D=3D> The above is not mentioned explicitly in the C++98 standard, but = it is > common sense. That's why i spoke about bad magic. :) > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys |
|
From: Per W. <pw...@ia...> - 2003-03-05 07:40:31
|
The next step of from the 32-bit case statement below would be to use a
hash table.
However, how would you replace the switch statement with std::bitset?
/Per W
On Wed, 5 Mar 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: Per Westermark
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0=EC
> To: Giligun
> Cc: C++
> Subject: Re: [Dev-C++] Character variables
>
> short s =3D 'ab';
> int i =3D 'abcd';
>
> given that int is 4 byte.
>
>
> =3D=3D> That isn't valid C++. And where it gets compiled you can't consid=
er it
> as safe.
>
>
>
> But a lot of compilers will give you warnings, since it you can't be sure
> if memcpy(buf,&s,sizeof(s)) will give you
> buf[0] =3D 'a', buf[1] =3D 'b';
> or
> buf[0] =3D 'b', buf[1] =3D 'a';
>
> In short - be very careful about using this feature. One place where it's
> elegant to use is if you are reading a tagged file, where each tag is
> 4-byte long:
> char *buf;
>
> int tag =3D 0;
> for (i =3D 0; i < 4; i++) {
> tag =3D (tag << 8) | *buf++;
> }
>
>
> =3D=3D> Too system dependent. Why don't you use wchar_t instead? But sinc=
e he
> wants to store characters (assuming in the range 0-127) he can use a char
> sequence nicely and easily.
>
> switch (tag) {
> case 'STRT': ...; break;
> case 'HEDR': ...; break;
> case 'DATA': ...; break;
> case 'ENDF': ...; break;
> }
>
> If there are a lot of tags, the result is a very fast matching of the
> tags.
>
>
> =3D=3D> By the way i would do something like the above using std::bitset.
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Per W. <pw...@ia...> - 2003-03-05 08:04:41
|
One more thing. I haven't a standard available right now, and a preliminar google only picked up an old -97 preliminary C++ standard. There you have your multicharacter literals in chapter 2 lexical conventions. http://anubis.dkuug.dk/jtc1/sc22/open/n2356/ /Per W On Wed, 5 Mar 2003, Per Westermark wrote: > The next step of from the 32-bit case statement below would be to use a > hash table. > > However, how would you replace the switch statement with std::bitset? > > /Per W > > On Wed, 5 Mar 2003, Ioannis Vranos wrote: > > > -------Original Message------- > > > > From: Per Westermark > > Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0= =EC > > To: Giligun > > Cc: C++ > > Subject: Re: [Dev-C++] Character variables > > > > short s =3D 'ab'; > > int i =3D 'abcd'; > > > > given that int is 4 byte. > > > > > > =3D=3D> That isn't valid C++. And where it gets compiled you can't cons= ider it > > as safe. > > > > > > > > But a lot of compilers will give you warnings, since it you can't be su= re > > if memcpy(buf,&s,sizeof(s)) will give you > > buf[0] =3D 'a', buf[1] =3D 'b'; > > or > > buf[0] =3D 'b', buf[1] =3D 'a'; > > > > In short - be very careful about using this feature. One place where it= 's > > elegant to use is if you are reading a tagged file, where each tag is > > 4-byte long: > > char *buf; > > > > int tag =3D 0; > > for (i =3D 0; i < 4; i++) { > > tag =3D (tag << 8) | *buf++; > > } > > > > > > =3D=3D> Too system dependent. Why don't you use wchar_t instead? But si= nce he > > wants to store characters (assuming in the range 0-127) he can use a ch= ar > > sequence nicely and easily. > > > > switch (tag) { > > case 'STRT': ...; break; > > case 'HEDR': ...; break; > > case 'DATA': ...; break; > > case 'ENDF': ...; break; > > } > > > > If there are a lot of tags, the result is a very fast matching of the > > tags. > > > > > > =3D=3D> By the way i would do something like the above using std::bitse= t. > > > > > > > > Ioannis Vranos > > > > * Programming pages: http://www.noicys.freeurl.com > > * Alternative URL: http://run.to/noicys > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Etnus, makers of TotalView, The debugg= er > for complex code. Debugging C/C++ programs can leave you feeling lost and > disoriented. TotalView can help you find your way. Available on major UNI= X > and Linux platforms. Try it free. www.etnus.com > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <no...@ho...> - 2003-03-05 09:52:16
|
-------Original Message-------=0D =0D From: Per Westermark=0D Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 10:03:30 =F0=EC= =0D To: Ioannis Vranos=0D Cc: Dev-C++ Mailing List=0D Subject: Re: [Dev-C++] Character variables=0D =0D One more thing. I haven't a standard available right now, and a prelimina= r=0D google only picked up an old -97 preliminary C++ standard.=0D =0D There you have your multicharacter literals in chapter 2 lexical=0D conventions.=0D =0D =0D =3D=3D> Yes but they are intended for wchar_t.=0D =0D =0D http://anubis.dkuug.dk/jtc1/sc22/open/n2356/=0D =0D =0D =3D=3D> Squeezing google for maximum efficiency reveals many sites. :) On= e of them is this:=0D =0D http://www.rgai.hu/~beszedes/munka/=0D =0D =0D However i do not know if downloading the pdf is legal. Damn, life is full= of temptations...=0D =0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |
|
From: Ioannis V. <no...@ho...> - 2003-03-05 10:00:54
|
=0D -------Original Message-------=0D =0D From: Ioannis Vranos=0D Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 11:54:32 =F0=EC= =0D To: Dev-C++ Mailing List=0D Subject: Re: [Dev-C++] Character variables=0D =0D =0D =0D =3D=3D> Squeezing google for maximum efficiency reveals many sites. :) On= e of them is this:=0D =0D http://www.rgai.hu/~beszedes/munka/=0D =0D =0D However i do not know if downloading the pdf is legal. Damn, life is full= of temptations...=0D =0D =0D =3D=3D> For example another site Google reveals is:=0D =0D http://www.fh-worms.de/~progbau/Vorlesungen/SW-Entwicklung/C++/Standard/=0D =0D =0D =3D=3D> As i said though, downloading the pdf is probably illegal...=0D =0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-03-05 11:30:29
|
No, multicharacter literals they are not specifically indented for
wchar_t.
They are defined to be of type int, and if you want store multiple char
characters, you write:
int i =3D 'abcd'
and for wchar_t
int i =3D L'ab'
where a 32-bit integer only have room for two wide characters.
The problematic non-standard issue with multicharacter literals is that
you are not guaranteed to know byte order of the machine, and the order
the multiple characters are stored in the integer, so you have to do some
condintional compilation if you want to save/load them.
You might have to do something like
#if '1234' =3D=3D 0x31323334L
// init alternative 1
#elseif '1234' =3D=3D 0x34333231L
// init alternative 2
#else
// no known byte order
#endif
Even if you use bitset, you still have to check which character goes into
the high bits, and which character goes to the low bits of the integer.
/Per W
PS - thx for the pdf reference, I haven't had access to a
computer-readable edition before :-)
On Wed, 5 Mar 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: Per Westermark
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 10:03:30 =F0=EC
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Character variables
>
> One more thing. I haven't a standard available right now, and a prelimina=
r
> google only picked up an old -97 preliminary C++ standard.
>
> There you have your multicharacter literals in chapter 2 lexical
> conventions.
>
>
> =3D=3D> Yes but they are intended for wchar_t.
>
>
> http://anubis.dkuug.dk/jtc1/sc22/open/n2356/
>
>
> =3D=3D> Squeezing google for maximum efficiency reveals many sites. :) On=
e of
> them is this:
>
> http://www.rgai.hu/~beszedes/munka/
>
>
> However i do not know if downloading the pdf is legal. Damn, life is full=
of
> temptations...
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2003-03-05 12:14:47
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 1:29:06 =EC=EC=0D
To: Ioannis Vranos=0D
Cc: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
No, multicharacter literals they are not specifically indented for=0D
wchar_t.=0D
=0D
They are defined to be of type int, and if you want store multiple char=0D
characters, you write:=0D
int i =3D 'abcd'=0D
and for wchar_t=0D
int i =3D L'ab'=0D
where a 32-bit integer only have room for two wide characters.=0D
=0D
=0D
=3D=3D> Yes but their meaning is implementation defined. I do not know mu=
ch for
them, and have only met them in one paragraph in TC++PL up to now that i =
am
on chapter 19.=0D
=0D
=0D
=0D
=0D
The problematic non-standard issue with multicharacter literals is that=0D
you are not guaranteed to know byte order of the machine, and the order=0D
the multiple characters are stored in the integer, so you have to do some=
=0D
condintional compilation if you want to save/load them.=0D
=0D
You might have to do something like=0D
#if '1234' =3D=3D 0x31323334L=0D
// init alternative 1=0D
#elseif '1234' =3D=3D 0x34333231L=0D
// init alternative 2=0D
#else=0D
// no known byte order=0D
#endif=0D
=0D
Even if you use bitset, you still have to check which character goes into=
=0D
the high bits, and which character goes to the low bits of the integer.=0D
=0D
=0D
=3D=3D> Personally i can't find any use of them, if you want to add them =
in
specific order in a bitset you can do:=0D
=0D
=0D
bitset<numeric_limits<char>::digits10 *2>tag('a');=0D
=0D
tag<<=3Dnumeric_limits<char>::digits10;=0D
=0D
tag&=3Dbitset<numeric_limits<char>::digits10 *2>('b');=0D
=0D
=0D
(i made a mistake in a previous email where i combined 2 different bitset
types (bitset<32> with bitset<4>).=0D
=0D
=0D
PS - thx for the pdf reference, I haven't had access to a=0D
computer-readable edition before :-)=0D
=0D
=0D
=3D=3D> Yes, all standards now come in .pdf form. It's the same .pdf as s=
old
from ISO web site.=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-03-05 12:40:33
|
If I need a set of 100 bits, the bitset implementation is nice, but for
multicharacter manipulation it doesn't give an improved portability, while
it is more code than
(unsigned('a') << 8) | unsigned('b')
The problem is still that depending on machine and compiler I might have
to do
(unsigned('b') << 8) | unsigned('a')
or
(unsigned('a') << 24) | unsigned('b')
or
(unsigned('b') << 24) | unsigned('a')
or
...
instead, because there isn't a defined standard how the individual
characters should be stored in the integer.
/Per W
On Wed, 5 Mar 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: Per Westermark
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 1:29:06 =EC=EC
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Character variables
>
> No, multicharacter literals they are not specifically indented for
> wchar_t.
>
> They are defined to be of type int, and if you want store multiple char
> characters, you write:
> int i =3D 'abcd'
> and for wchar_t
> int i =3D L'ab'
> where a 32-bit integer only have room for two wide characters.
>
>
> =3D=3D> Yes but their meaning is implementation defined. I do not know mu=
ch for
> them, and have only met them in one paragraph in TC++PL up to now that i =
am
> on chapter 19.
>
>
>
>
> The problematic non-standard issue with multicharacter literals is that
> you are not guaranteed to know byte order of the machine, and the order
> the multiple characters are stored in the integer, so you have to do some
> condintional compilation if you want to save/load them.
>
> You might have to do something like
> #if '1234' =3D=3D 0x31323334L
> // init alternative 1
> #elseif '1234' =3D=3D 0x34333231L
> // init alternative 2
> #else
> // no known byte order
> #endif
>
> Even if you use bitset, you still have to check which character goes into
> the high bits, and which character goes to the low bits of the integer.
>
>
> =3D=3D> Personally i can't find any use of them, if you want to add them =
in
> specific order in a bitset you can do:
>
>
> bitset<numeric_limits<char>::digits10 *2>tag('a');
>
> tag<<=3Dnumeric_limits<char>::digits10;
>
> tag&=3Dbitset<numeric_limits<char>::digits10 *2>('b');
>
>
> (i made a mistake in a previous email where i combined 2 different bitset
> types (bitset<32> with bitset<4>).
>
>
> PS - thx for the pdf reference, I haven't had access to a
> computer-readable edition before :-)
>
>
> =3D=3D> Yes, all standards now come in .pdf form. It's the same .pdf as s=
old
> from ISO web site.
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2003-03-05 12:45:12
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 2:39:12 =EC=EC=0D
To: Ioannis Vranos=0D
Cc: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
If I need a set of 100 bits, the bitset implementation is nice, but for=0D
multicharacter manipulation it doesn't give an improved portability, whil=
e=0D
it is more code than=0D
=0D
(unsigned('a') << 8) | unsigned('b')=0D
=0D
=0D
=3D=3D> Why isn't my code portable?=0D
=0D
=0D
bitset<numeric_limits<char>::digits10 *2>tag('a');=0D
=0D
tag<<=3Dnumeric_limits<char>::digits10;=0D
=0D
tag&=3Dbitset<numeric_limits<char>::digits10 *2>('b');=0D
=0D
=0D
=3D=3D> It is completely portable.=0D
=0D
=0D
=3D=3D> Yes it is more code than your code above because my code was made=
to be
portable. The equivalent would be:=0D
=0D
=0D
(bitset<sizeof(unsigned)>('a')<<8) | bitset<sizeof(unsigned)>('b')=0D
=0D
=0D
=0D
=0D
The problem is still that depending on machine and compiler I might have=0D
to do=0D
(unsigned('b') << 8) | unsigned('a')=0D
or=0D
(unsigned('a') << 24) | unsigned('b')=0D
or=0D
(unsigned('b') << 24) | unsigned('a')=0D
or=0D
=2E..=0D
instead, because there isn't a defined standard how the individual=0D
characters should be stored in the integer.=0D
=0D
=0D
=3D=3D> ?=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-03-05 13:19:22
|
I didn't say your code wasn't portable, I just said that it didn't solve
the real problem, which is where the different characters that are stored
where in the integer.
Since the standard doesn't define it, a portable solutions have to perform
an analysis and adapt to compiler and architecture used. We don't know if
the characters are aligned to least and/or most significant bit of the
integer, and we don't know if the first character of the multicharacter
literal is in higher or lower bits relative the second character of the
literal.
/Per W
On Wed, 5 Mar 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: Per Westermark
> Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 2:39:12 =EC=EC
> To: Ioannis Vranos
> Cc: Dev-C++ Mailing List
> Subject: Re: [Dev-C++] Character variables
>
> If I need a set of 100 bits, the bitset implementation is nice, but for
> multicharacter manipulation it doesn't give an improved portability, whil=
e
> it is more code than
>
> (unsigned('a') << 8) | unsigned('b')
>
>
> =3D=3D> Why isn't my code portable?
>
>
> bitset<numeric_limits<char>::digits10 *2>tag('a');
>
> tag<<=3Dnumeric_limits<char>::digits10;
>
> tag&=3Dbitset<numeric_limits<char>::digits10 *2>('b');
>
>
> =3D=3D> It is completely portable.
>
>
> =3D=3D> Yes it is more code than your code above because my code was made=
to be
> portable. The equivalent would be:
>
>
> (bitset<sizeof(unsigned)>('a')<<8) | bitset<sizeof(unsigned)>('b')
>
>
>
>
> The problem is still that depending on machine and compiler I might have
> to do
> (unsigned('b') << 8) | unsigned('a')
> or
> (unsigned('a') << 24) | unsigned('b')
> or
> (unsigned('b') << 24) | unsigned('a')
> or
> ...
> instead, because there isn't a defined standard how the individual
> characters should be stored in the integer.
>
>
> =3D=3D> ?
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2003-03-06 01:18:10
|
A fix: =0D
=0D
bitset<numeric_limits<char>::digits *2>tag('a');=0D
tag<<=3Dnumeric_limits<char>::digits;=0D
tag&=3Dbitset<numeric_limits<char>::digits *2>('b');=0D
=0D
It is completely portable.=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Ioannis V. <no...@ho...> - 2003-03-05 09:39:44
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 9:39:19 =F0=EC=0D
To: Ioannis Vranos=0D
Cc: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
The next step of from the 32-bit case statement below would be to use a=0D
hash table.=0D
=0D
However, how would you replace the switch statement with std::bitset?=0D
=0D
=0D
=3D=3D> At first as i said applying the bitwise operators on a signed int=
eger is
bad magic. They are intended for unsigned integrals only. Violation of th=
is
can lead to overflows and open the door of our Universe to dragons, elves
and other bad-meaning creatures.=0D
=0D
=0D
How i would replace the switch statement with bitset:=0D
=0D
Let's assume we had:=0D
=0D
bitset<32>tag(0);=0D
=0D
// I assume you wanted this:=0D
bitset<4>buf('a'+'b');=0D
=0D
for (size_t i =3D 0; i < 4; i++)=0D
tag =3D (tag << 8) | buf[i++];=0D
=0D
=0D
switch (tag.to_ulong())=0D
{=0D
case 'STRT': ...; break;=0D
case 'HEDR': ...; break;=0D
case 'DATA': ...; break;=0D
case 'ENDF': ...; break;=0D
}=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Daniel K. O. <dan...@ya...> - 2003-03-04 22:34:18
|
Why are you trying to learn C from scratch asking this mailing list? Isn't more effective to buy/download a book/tutorial on the internet? Something like http://www.geocities.com/tom_torfs/c.html Daniel K. O. It's OK to be ignorant; it's not OK to play stupid. http://www.catb.org/~esr/faqs/smart-questions.html > -----Mensagem original----- > De: dev...@li... > [mailto:dev...@li...]Em nome de Giligun > Enviada em: terça-feira, 4 de março de 2003 19:02 > Para: C++ > Assunto: [Dev-C++] Character variables > > > How does one get a char variable to hold more than one > character? Or what other type should be used? > _______________________________________________________________________ Busca Yahoo! O serviço de busca mais completo da Internet. O que você pensar o Yahoo! encontra. http://br.busca.yahoo.com/ |
|
From: Ioannis V. <no...@ho...> - 2003-03-04 22:48:32
|
-------Original Message-------=0D =0D From: too...@ya...=0D Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:05:20 =F0=EC= =0D To: C++=0D Subject: [Dev-C++] Character variables=0D =0D How does one get a char variable to hold more than one=0D character? Or what other type should be used?=0D =0D =0D =3D=3D> You can't fit more than one characters in one char. You can use a= char sequence (array or whatever) to do that.=0D =0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |