473,441 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Set all bits to 1 in an array of raw bytes


I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);

but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.

Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?

Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...

Martin

Sep 27 '07 #1
90 6955
On Sep 27, 3:15 pm, Martin Wells <war...@eircom.netwrote:
I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);

but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.

Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?

Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...

Martin
If you got 2's complementary you can pass -1 which is all the bit to
1.
But some says that there is architecture where it's not a 2's
complementary (why, it's strange, have they find a better way to code
the numbers ?).

Sep 27 '07 #2
If you got 2's complementary you can pass -1 which is all the bit to
1.
But some says that there is architecture where it's not a 2's
complementary (why, it's strange, have they find a better way to code
the numbers ?).

Fully-portably please as regards C89. . .

Martin

Sep 27 '07 #3
Martin Wells wrote:
I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);
How about this?

memset(data,~0,sizeof data);
Sep 27 '07 #4
unsigned char all1bit (void)
{
unsigned char a, b, c;
do
{
a = 2 * b + 1;
c = a;
a = b;
b = c;
} while (a != b);
return a;
}

Perhaps this. But I'm not sure.
all1bit () must return a char with all bit set to 1. (You can use it
with every type, not just unsigned char, just rewrite the function).
After you copy it every where in your array.

Sep 27 '07 #5
Mark Bluemel wrote:
How about this?

memset(data,~0,sizeof data);
It must work (except syntax errors), but maybe better, to use:

unsigned char nonzero ~0;
memset(data, nonzero, sizeof(data));
--
s/.../.gotovchits/g for email.
Sep 27 '07 #6
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@y42g2000hsy.googlegr oups.com...
unsigned char all1bit (void)
{
unsigned char a, b, c;
do
{
a = 2 * b + 1;
pardon? double the uninitialized random b and add 1, then assign to a?
c = a;
a = b;
assign the unitialized random value of b to a?
b = c;
} while (a != b);
return a;
}

Perhaps this. But I'm not sure.
all1bit () must return a char with all bit set to 1. (You can use it
with every type, not just unsigned char, just rewrite the function).
After you copy it every where in your array.
Or am I missing the humo(u)r?

Bye, Jojo
Sep 27 '07 #7
Martin Wells <wa****@eircom.netwrites:
I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);

but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.
Why is that? Are you telling us that the standard says that an int can
be smaller than UCHAR? Since the smallest addressable unit of memory is a
byte and memset sets bytes, then surely an int will be bigger or
smaller than a byte and therefore -1 in an int in twos complement will
always be all bits set?
Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?
>
Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...

Martin
Sep 27 '07 #8
Richard:
but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.

Why is that? Are you telling us that the standard says that an int can
be smaller than UCHAR? Since the smallest addressable unit of memory is a
byte and memset sets bytes, then surely an int will be bigger or
smaller than a byte and therefore -1 in an int in twos complement will
always be all bits set?
Here's a perfectly compliant C implementation:

CHAR_BIT = 64

sizeof(char) == sizeof(short) == sizeof(int) == sizeof(long)

(the unsigned counterparts are always the same size).

It's possible for unsigned char to have the range: 0 through 2^64 -1
and at the same time have int with the range: -2^63 through 2^63 - 1
(apologies if I'm off by one :P)

Anyway, it's certainly possible for UCHAR_MAX INT_MAX, and that's
why unsigned char promotes to unsigned int (rather than int) on some
systems. Here's a simple test for it:

#include <limits.h>

#define PROMOTES_TO_UNSIGNED (UCHAR_MAX INT_MAX)

Martin

Sep 27 '07 #9
On Sep 27, 2:36 pm, "ie...@free.fr" <ie...@free.frwrote:
unsigned char all1bit (void)
{
unsigned char a, b, c;
do
{
a = 2 * b + 1;
c = a;
a = b;
b = c;
} while (a != b);
return a;

}

Perhaps this. But I'm not sure.
all1bit () must return a char with all bit set to 1. (You can use it
with every type, not just unsigned char, just rewrite the function).
After you copy it every where in your array.

No comment.

Martin
Sep 27 '07 #10
pardon? double the uninitialized random b and add 1, then assign to a?

No I refuse to assign a variable when it's useless (useless means
useless). In this case it's useless. This work with every values
possible for b (yeah yeah, check if you're not sure, there are 256
cases if sizeof (char) == 1).
Or am I missing the humo(u)r?
That's not humor, I just was proposing my solution.

Sep 27 '07 #11
Mark:
How about this?

memset(data,~0,sizeof data);
Excuse me while I think out loud...

1: 0

Type: int
Value: 0

2: ~0

Type: int
Value: Whatever all bits 1 is on this sytems (most likely -1
though)

3: (char unsigned)~0

Type: int
Value: ...I think we've already gone implementation defined.

Martin
Sep 27 '07 #12
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@k79g2000hse.googlegr oups.com...
>pardon? double the uninitialized random b and add 1, then assign to a?

No I refuse to assign a variable when it's useless (useless means
useless). In this case it's useless.
Useless, like useless? Or did you meant useless?
This work with every valuespossible for b
OK, it makes sure a is a odd.
>(yeah yeah, check if you're not sure, there are 256
cases if sizeof (char) == 1).
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'

Bye, Jojo
Sep 27 '07 #13
Useless, like useless? Or did you meant useless?

??? I don't understand.
This work with every valuespossible for b
In fact I assume that * 2 shift the bit and put a 0 in the new one and
that + 1 put this new one to 1. That's the idea behind this script.

Sep 27 '07 #14
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'

When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

Sep 27 '07 #15
Ivan Gotovchits wrote:
Mark Bluemel wrote:
>How about this?

memset(data,~0,sizeof data);

It must work (except syntax errors),
What syntax errors?
but maybe better, to use:

unsigned char nonzero ~0;
You missed a '='.
memset(data, nonzero, sizeof(data));
Why would this be better?
Sep 27 '07 #16
In article <fd**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
Ivan Gotovchits wrote:
Mark Bluemel wrote:
How about this?

memset(data,~0,sizeof data);
It must work (except syntax errors),

What syntax errors?
"sizeof data" == syntax error, "sizeof(data)" == correct syntax.
but maybe better, to use:

unsigned char nonzero ~0;

You missed a '='.
memset(data, nonzero, sizeof(data));

Why would this be better?
For starters, probably because it's syntactically correct, so it will
actually have a chance of compiling... (after fixing the missing "="
noted above, of course)

--
Don Bruder - da****@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info
Sep 27 '07 #17
"ie***@free.fr" <ie***@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'

When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.
# 6.5.3.4 The sizeof operator
....
# Semantics
....
# 3 When applied to an operand that has type char, unsigned char, or
# signed char, (or a qualified version thereof) the result is 1.

Are you perhaps related to our other regular Frenchman? Your
idiosyncratic view of the language seems a match for his.

Richard
Sep 27 '07 #18
<ie***@free.frschrieb im Newsbeitrag
news:11*********************@n39g2000hsh.googlegro ups.com...
>sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT ==
8'

When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.
You'd better, as it's guaranteed by the Standard

Bye, Jojo
Sep 27 '07 #19
Don Bruder wrote:
In article <fd**********@aioe.org>,
Mark Bluemel <ma**********@pobox.comwrote:
>Ivan Gotovchits wrote:
>>Mark Bluemel wrote:

How about this?

memset(data,~0,sizeof data);
It must work (except syntax errors),
What syntax errors?
"sizeof data" == syntax error, "sizeof(data)" == correct syntax.
Before I posted the question I wrote this program :-
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
unsigned char data[255];
memset(data,~0,sizeof data);
return EXIT_SUCCESS;
}

And compiled it with
gcc -Wall -ansi -pedantic bitset.c -o bitset

I repeat my question - "What syntax errors?".

You and Ivan might wish to read up on the syntax of the sizeof operator
before commenting further - try
http://tigcc.ticalc.org/doc/keywords.html#sizeof
Sep 27 '07 #20
Are you perhaps related to our other regular Frenchman?

I think. It's difficult to hide is natural language when you never
practice other. Is it the "assume" ?

Sep 27 '07 #21
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@w3g2000hsg.googlegro ups.com...
>Useless, like useless? Or did you meant useless?

??? I don't understand.
Sorry, forgot the 8-), thought it to be obvious after you used that word 4
times in a row.
This work with every valuespossible for b

In fact I assume that * 2 shift the bit and put a 0 in the new one and
that + 1 put this new one to 1. That's the idea behind this script.
So here you indeed want to shift, so why don't you just do it?

Bye, Jojo
Sep 27 '07 #22
"ie***@free.fr" <ie***@free.frwrote:
Are you perhaps related to our other regular Frenchman?

I think. It's difficult to hide is natural language when you never
practice other. Is it the "assume" ?
No. It is the assumptions.

Richard
Sep 27 '07 #23
In article <46***********************@news.sonic.net>,
Don Bruder <da****@sonic.netwrote:
> memset(data,~0,sizeof data);

It must work (except syntax errors),
>What syntax errors?
>"sizeof data" == syntax error, "sizeof(data)" == correct syntax.
The parentheses are only required when the argument is a type name.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 27 '07 #24
So here you indeed want to shift, so why don't you just do it?

Because shift is an operator that I never use because of things
explain in another topic. I have never found a doc which is precise
about these.

Sep 27 '07 #25
"Richard Tobin" <ri*****@cogsci.ed.ac.ukschrieb im Newsbeitrag
news:fd***********@pc-news.cogsci.ed.ac.uk...
In article <46***********************@news.sonic.net>,
Don Bruder <da****@sonic.netwrote:
>> memset(data,~0,sizeof data);

It must work (except syntax errors),
>>What syntax errors?
>>"sizeof data" == syntax error, "sizeof(data)" == correct syntax.

The parentheses are only required when the argument is a type name.
.... because, unlike a comon misbelieve, sizeof is not a function, but an
operator.

(not correcting you, Richard, just teaching Don...)

Bye, Jojo
Sep 27 '07 #26
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@y42g2000hsy.googlegr oups.com...
>So here you indeed want to shift, so why don't you just do it?

Because shift is an operator that I never use because of things
explain in another topic. I have never found a doc which is precise
about these.
In the other thread you wanted to use <<1 instead of *2, for performance
reasons (and have been told not to.)

There's at least one doc that is very precise about this: the Standard.
Google for 1256.pdf.

(or take http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf)

Bye, Jojo
Sep 27 '07 #27

"Joachim Schmitz" <no*********@schmitz-digital.deschrieb im Newsbeitrag
news:fd**********@online.de...
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@w3g2000hsg.googlegro ups.com...
>>Useless, like useless? Or did you meant useless?

??? I don't understand.
Sorry, forgot the 8-), thought it to be obvious after you used that word 4
times in a row.
>This work with every valuespossible for b

In fact I assume that * 2 shift the bit and put a 0 in the new one and
that + 1 put this new one to 1. That's the idea behind this script.
So here you indeed want to shift, so why don't you just do it?
Something like:

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++, a<<1 + 1)
;
return a;
}
Sep 27 '07 #28
Joachim Schmitz wrote:
>
Something like:

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++, a<<1 + 1)
;
return a;
}
That returns 1...

The poor guy is confused enough already, don't confuse him further.
(I've killfiled him, his signal to noise ratio was too low).
Sep 27 '07 #29

"Mark Bluemel" <ma**********@pobox.comschrieb im Newsbeitrag
news:fd**********@aioe.org...
Joachim Schmitz wrote:
>>
Something like:

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++, a<<1 + 1)
;
return a;
}

That returns 1...
Oops... a<<2 of course.

Bye, Jojo
Sep 27 '07 #30
Joachim Schmitz wrote:
"Mark Bluemel" <ma**********@pobox.comschrieb im Newsbeitrag
news:fd**********@aioe.org...
>Joachim Schmitz wrote:
>>Something like:

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++, a<<1 + 1)
;
return a;
}
That returns 1...
Oops... a<<2 of course.

Bye, Jojo

Nope. After you initialise a, you don't do anything which alters its
value... That's at least in part due to the attempt to do too much in
the control of the for loop, IMHO.
Sep 27 '07 #31
Martin Wells wrote:
Mark:
>How about this?

memset(data,~0,sizeof data);

Excuse me while I think out loud...

1: 0

Type: int
Value: 0

2: ~0

Type: int
Value: Whatever all bits 1 is on this sytems (most likely -1
though)

3: (char unsigned)~0

Type: int
Value: ...I think we've already gone implementation defined.
I don't think so. I think the section on "Representation of types" in
the standard will cover this.
Sep 27 '07 #32
"Mark Bluemel" <ma**********@pobox.comschrieb im Newsbeitrag
news:fd**********@aioe.org...
Joachim Schmitz wrote:
>"Mark Bluemel" <ma**********@pobox.comschrieb im Newsbeitrag
news:fd**********@aioe.org...
>>Joachim Schmitz wrote:
Something like:

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++, a<<1 + 1)
;
return a;
}
That returns 1...
Oops... a<<2 of course.

Bye, Jojo
Nope. After you initialise a, you don't do anything which alters its
value... That's at least in part due to the attempt to do too much in the
control of the for loop, IMHO.
Outch...

#include <limits.h>
unsigned char all1bit (void)
{
unsigned char i, a;
for (i=a=1; i < CHAR_BIT; i++) {
a<<=1;
a++;
}
return a;
}
Sep 27 '07 #33
On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'

When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.
How silly. How do you manage to code if you don't assume that well-
defined things are well-defined? Do you trust the bahaviour of '==',
or assume that the value of 1 is one? If so, why do you trust them but
not other equally well-defined things?

Sep 27 '07 #34
On Sep 27, 2:36 pm, Ivan Gotovchits <ivan.gotovch...@auriga.ruwrote:
Mark Bluemel wrote:
How about this?
memset(data,~0,sizeof data);

It must work (except syntax errors),
What syntax errors?
but maybe better, to use:

unsigned char nonzero ~0;
memset(data, nonzero, sizeof(data));
After correcting the syntax error this would work, but why do you
think it is better? Needs a lot more typing and parsing. The original
version was concise, clear, and comprehensible.

Sep 27 '07 #35
On Sep 27, 5:59 pm, "J. J. Farrell" <j...@bcs.org.ukwrote:
On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'
When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

How silly. How do you manage to code if you don't assume that well-
defined things are well-defined? Do you trust the bahaviour of '==',
or assume that the value of 1 is one? If so, why do you trust them but
not other equally well-defined things?
But if you make something like that :
a = (char *) malloc (5 * 1)
instead
a = (char *) malloc (5 * sizeof (char))

you know that on some (strange) architectures (yes the standard are
not respected, but...) it doesn't work. It's sad, with the second one
it still work.

Sep 27 '07 #36
In the other thread you wanted to use <<1 instead of *2, for performance
reasons (and have been told not to.)
It was a theorical question. About the meaning of <<.
There's at least one doc that is very precise about this: the Standard.
I never found it free, but if you got the standard ansi (not others)
and give it to me, I'll remember you all my life.

Sep 27 '07 #37
<ie***@free.frschrieb im Newsbeitrag
news:11**********************@22g2000hsm.googlegro ups.com...
>In the other thread you wanted to use <<1 instead of *2, for performance
reasons (and have been told not to.)

It was a theorical question. About the meaning of <<.
>There's at least one doc that is very precise about this: the Standard.

I never found it free, but if you got the standard ansi (not others)
and give it to me, I'll remember you all my life.
Well, guess I can live with that...

I gave you a hint what to search for and also an URL, to ANSI/ISO C99 with
the Technical Corrigenda TC1, TC2 and TC3 being incorporated.

Bye, Jojo
Sep 27 '07 #38

<ie***@free.frschrieb im Newsbeitrag
news:11**********************@50g2000hsm.googlegro ups.com...
On Sep 27, 5:59 pm, "J. J. Farrell" <j...@bcs.org.ukwrote:
>On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT
== 8'
When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

How silly. How do you manage to code if you don't assume that well-
defined things are well-defined? Do you trust the bahaviour of '==',
or assume that the value of 1 is one? If so, why do you trust them but
not other equally well-defined things?

But if you make something like that :
a = (char *) malloc (5 * 1)
instead
a = (char *) malloc (5 * sizeof (char))

you know that on some (strange) architectures (yes the standard are
not respected, but...) it doesn't work. It's sad, with the second one
it still work.
a) a compiler that doesn't have sizeof(char) == 1 is not a C-Compiler, it is
at best C like.
b) drop the cast (char *), when using malloc(), but use #include <stdlib.h>

Bye, Jojo
Sep 27 '07 #39
Martin Wells wrote:
>
I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);

but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.

Can anyone think of a way to set all the bits to 1 (other than looping
through the elements and assigning UCHAR_MAX to them)?

Possibly, what I need is an int value that will convert to UCHAR_MAX
on every implementation... hmm...
memset(data, -1, sizeof data);

((unsigned char)-1) equals UCHAR_MAX.
The int argument value is converted to unsigned char by memset.

N869
7.21.6.1 The memset function
Synopsis
[#1]
#include <string.h>
void *memset(void *s, int c, size_t n);
Description
[#2] The memset function copies the value of c (converted to
an unsigned char) into each of the first n characters of the
object pointed to by s.

--
pete
Sep 27 '07 #40
On Sep 27, 6:01 pm, "ie...@free.fr" <ie...@free.frwrote:
On Sep 27, 5:59 pm, "J. J. Farrell" <j...@bcs.org.ukwrote:
On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if CHAR_BIT == 8'
When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.
How silly. How do you manage to code if you don't assume that well-
defined things are well-defined? Do you trust the bahaviour of '==',
or assume that the value of 1 is one? If so, why do you trust them but
not other equally well-defined things?

But if you make something like that :
a = (char *) malloc (5 * 1)
instead
a = (char *) malloc (5 * sizeof (char))

you know that on some (strange) architectures (yes the standard are
not respected, but...) it doesn't work. It's sad, with the second one
it still work.
How can you possibly say that? If this mythical compiler differs from
a C compiler on something as fundamental to C as the size of char, how
can you assume that '=' does the same as in C, or that "malloc()" does
the same as in C?

Does the language compiled by this mythical compiler require the
result of malloc() to be cast? That's a bad practice in C.

Sep 27 '07 #41
Martin Wells wrote:
>
If you got 2's complementary you can pass -1 which is all the bit to
1.
But some says that there is architecture where it's not a 2's
complementary (why, it's strange,
have they find a better way to code the numbers ?).

Fully-portably please as regards C89. . .
Two's complement doesn't matter in this case.

--
pete
Sep 27 '07 #42
Martin Wells wrote:
>
Mark:
How about this?

memset(data,~0,sizeof data);

Excuse me while I think out loud...

1: 0

Type: int
Value: 0

2: ~0

Type: int
Value: Whatever all bits 1 is on this sytems (most likely -1
though)

3: (char unsigned)~0

Type: int
Value: ...I think we've already gone implementation defined.
(~0) is implementation defined.

--
pete
Sep 27 '07 #43
Martin Wells wrote:
Anyway, it's certainly possible for UCHAR_MAX INT_MAX,
If sizeof(int) equals one,
then UCHAR_MAX must be greater then INT_MAX.

--
pete
Sep 27 '07 #44
"ie***@free.fr" wrote:
>
.... snip ...
>
>There's at least one doc that is very precise about this: the
Standard.

I never found it free, but if you got the standard ansi (not
others) and give it to me, I'll remember you all my life.
bzip2 compressed.
<http://cbfalconer.home.att.net/download/n869_txt.bz2>

Please do not remove attributions for any material you quote.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 27 '07 #45
J. J. Farrell wrote:
>
On Sep 27, 6:01 pm, "ie...@free.fr" <ie...@free.frwrote:
On Sep 27, 5:59 pm, "J. J. Farrell" <j...@bcs.org.ukwrote:
On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always.
When I code I don't assume that sizeof (char) == 1
How can you possibly say that?
Either he doesn't understand, or he doesn't believe.

--
pete
Sep 27 '07 #46
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"ie***@free.fr" <ie***@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if
CHAR_BIT == 8'
When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

# 6.5.3.4 The sizeof operator
...
# Semantics
...
# 3 When applied to an operand that has type char, unsigned char, or
# signed char, (or a qualified version thereof) the result is 1.

Are you perhaps related to our other regular Frenchman? Your
idiosyncratic view of the language seems a match for his.
Note that Richard was referring to your idiosyncratic of C, not of
English.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #47
"ie***@free.fr" <ie***@free.frwrites:
On Sep 27, 5:59 pm, "J. J. Farrell" <j...@bcs.org.ukwrote:
>On Sep 27, 3:10 pm, "ie...@free.fr" <ie...@free.frwrote:
sizeof(char) is 1 by definition, always. Guess you meant 'if
CHAR_BIT == 8'
When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

How silly. How do you manage to code if you don't assume that well-
defined things are well-defined? Do you trust the bahaviour of '==',
or assume that the value of 1 is one? If so, why do you trust them but
not other equally well-defined things?

But if you make something like that :
a = (char *) malloc (5 * 1)
instead
a = (char *) malloc (5 * sizeof (char))

you know that on some (strange) architectures (yes the standard are
not respected, but...) it doesn't work. It's sad, with the second one
it still work.
You are mistaken. The above can be written as

a = malloc(5);

and it's *absolutely guaranteed* to be equivalent to

a = malloc(5 * sizeof(char));

Not all compilers are 100% conforming, but making sizeof(char)
anything other than 1 just isn't an error any compiler write is going
to make. If sizeof(char) != 1, it's not C; it's about as likely as
2 + 2 != 4.

Really.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #48
On Thu, 27 Sep 2007 06:36:38 -0700, "ie***@free.fr" <ie***@free.fr>
wrote:
>unsigned char all1bit (void)
{
unsigned char a, b, c;
do
{
a = 2 * b + 1;
At this point, you have invoked undefined behavior the first time
through the loop.
c = a;
a = b;
b = c;
} while (a != b);
return a;
}

Perhaps this. But I'm not sure.
all1bit () must return a char with all bit set to 1. (You can use it
with every type, not just unsigned char, just rewrite the function).
After you copy it every where in your array.

Remove del for email
Sep 28 '07 #49
On Thu, 27 Sep 2007 15:49:42 +0200, Richard <rg****@gmail.comwrote:
>Martin Wells <wa****@eircom.netwrites:
>I have an array as follows:

char unsigned data[N];

I want to set every single bit in the array to 1. My initial thoughts
were:

memset(data, UCHAR_MAX, sizeof data);

but then when I looked at the declaration for memset, I saw that the
middle parameter was an int rather than an unsigned char. The problem
here is that UCHAR_MAX isn't guaranteed to "fit" in an int.

Why is that? Are you telling us that the standard says that an int can
be smaller than UCHAR? Since the smallest addressable unit of memory is a
byte and memset sets bytes, then surely an int will be bigger or
smaller than a byte and therefore -1 in an int in twos complement will
always be all bits set?
CHAR_BIT is not prohibited from being 16 or larger. On those systems
where sizeof(int) is one, it is possible for UCHAR_MAX to be the same
as UINT_MAX and both could be larger than INT_MAX.
Remove del for email
Sep 28 '07 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1",...
9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
4
by: Lee Crabtree | last post by:
I need to shift all of the values in a byte array by more than 8 bits, meaning that values should flow from one byte to another. Since I don't know in advance how many bits will be shifting, I...
9
by: James Vanns | last post by:
First, Sorry for the cross-post :p OK, in Linux I can obtain the MAC address using socket(), ioctl() etc. and a few data structures. No problem. However, the resulting MAC is stored in a 14 byte...
20
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
2
by: Pete | last post by:
Hi, First, thanks for any time you spend helping me, I'm at a loss. I'm not bit-savvy, so I apologize if this is extremely simple, or I am going about this the wrong way. I am trying to take a...
5
by: Nate.Strack | last post by:
I have a bool array representing the 32 bit dos datetime format and i need to translate those 32 bits into 4 bytes so i can save.
14
by: Francine.Neary | last post by:
Consider the following situation. I have a large static array s of structs, say of size 500. I also need a smaller array of chars, say of size 100, which has nothing to do with the struct. To...
1
by: sriprasanna | last post by:
Hello All, In our project we have to store PDF, word document files in DataBase(SQL SERVER 2005). We have planned to convert the file to array of bytes and store the binary file into...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.