Set all bits to 1 in an array of raw bytes

M

Mark Bluemel

Don said:
"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
 
I

iesvs

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" ?
 
J

Joachim Schmitz

??? I don't understand.
Sorry, forgot the :cool:, thought it to be obvious after you used that word 4
times in a row.
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
 
I

iesvs

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.
 
J

Joachim Schmitz

"sizeof data" == syntax error, "sizeof(data)" == correct syntax.

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

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

Bye, Jojo
 
J

Joachim Schmitz

Joachim Schmitz said:
Sorry, forgot the :cool:, thought it to be obvious after you used that word 4
times in a row.

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;
}
 
M

Mark Bluemel

Joachim said:
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).
 
M

Mark Bluemel

Joachim said:
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.
 
M

Mark Bluemel

Martin said:
Mark:


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.
 
J

Joachim Schmitz

Mark Bluemel said:
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;
}
 
J

J. J. Farrell

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?
 
J

J. J. Farrell

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.
 
I

iesvs

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.
 
I

iesvs

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.
 
J

Joachim Schmitz

It was a theorical question. About the meaning of <<.


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
 
J

Joachim Schmitz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top