Middle parameter to memset that will achieve UCHAR_MAX

B

Barry Schwarz

I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

I'll be very specific about what I'm asking, so please if you don't
know shit about C or how to code then just stay quiet.

What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
snip

UCHAR_MAX isn't guranteed to fit in an int, but maybe we'll get a
consistent result across all implementations if we use it? I'll have
to look up the rules for signed to unsigned conversions. As far as I
know though, it's something along the lines of:

target = TARGET_TYPE_MAX + 1 - negative_number

~0 is a different value on different systems, depending on the system
used to represent negative numbers.

The ~ is a bitwise operator, not an arithmetic one. 0 is an int that
must be represented by all bits 0. ~0 must therefore be an int with
all bits 1, regardless of what value it represents. Since sizeof(int)
must be at least as large as sizeof(char) which is the same as
sizeof(unsigned char), when ~0 is converted to unsigned char in the
call to memset, the resulting value will have all bits 1 also.
Any other takers for what we should give memset? I think basically we
need an int value that will convert to UCHAR_MAX on every
implementation? Maybe we might have to delve into determing the number
system via macros, but I'm just thinking out loud. In the end we'd
have:

memset(data, INT_VALUE_THAT_WILL_YIELD_UCHAR_MAX, sizeof data);


Remove del for email
 
P

Philip Potter

Joachim said:
signed int has the same sizes as unsigned int, so it does have the same
number of bits, so it would fit. It would not represent the same number
though.

So the number UCHAR_MAX does not fit into an int. Some other number has
taken its place.

By your argument there is no constant which doesn't fit into an int!
typedef sizet_t unsigned int;
size_t size = (size_t)-1;

is legal, isn't it?

That's a different issue. It's legal, but you wouldn't say that -1 fits
into a size_t, would you?
 
P

pete

Barry said:
The ~ is a bitwise operator, not an arithmetic one. 0 is an int that
must be represented by all bits 0. ~0 must therefore be an int with
all bits 1, regardless of what value it represents.
Since sizeof(int)
must be at least as large as sizeof(char) which is the same as
sizeof(unsigned char),

So far, so good.
when ~0 is converted to unsigned char in the
call to memset, the resulting value will have all bits 1 also.

No.
Conversion to unsigned char isn't a bitwise operation.
Conversion to unsigned char depends on the value of (~0)
rather than on its bit pattern.
In one's complement, (~0) is either zero,
or negative zero, or a trap.
 
M

mark_bluemel

I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

Newgroup responses are worth at least what you pay for them...
I'll be very specific about what I'm asking, so please if you don't
know shit about C or how to code then just stay quiet.

This is a public newsgroup. People can choose for themselves whether
and how to respond. My response is just on the verge of killfiling
you. You can equally choose to killfile those you determine unworthy
of your consideration...
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?

I heard you the first time and answered you, correctly as far as I can
tell, in the previous thread.

[Snip]
~0 is a different value on different systems, depending on the system
used to represent negative numbers.

The standard suggests that you are incorrect here, probably due to you
being confused between numeric and bitwise interpretations of data.
Any other takers for what we should give memset? I think basically we
need an int value that will convert to UCHAR_MAX on every
implementation?

I think you're mistaken. You need a value with all bits set. Bitwise
operations seem a more sensible way to achieve that than arithmetic
ones...
 
M

mark_bluemel

I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

Newgroup responses are worth at least what you pay for them...
I'll be very specific about what I'm asking, so please if you don't
know shit about C or how to code then just stay quiet.

This is a public newsgroup. People can choose for themselves whether
and how to respond. My response is just on the verge of killfiling
you. You can equally choose to killfile those you determine unworthy
of your consideration...
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?

I heard you the first time and answered you, correctly as far as I can
tell, in the previous thread.

[Snip]
~0 is a different value on different systems, depending on the system
used to represent negative numbers.

The standard suggests that you are incorrect here, probably due to you
being confused between numeric and bitwise interpretations of data.

Pete's response to Barry on the other hand suggests I'm confused. Fair
enough, I try to learn from my mistakes...
 
P

pete

Pete's response to Barry on the other hand suggests I'm confused. Fair
enough, I try to learn from my mistakes...

(~0) is INT_MIN in sign and magnitude representation.
(~0) is -1 in two's complement representation.
 
J

Joachim Schmitz

But if I take it out there and interprete it as an unsigned char, I have it
back, don't I?
So the number UCHAR_MAX does not fit into an int. Some other number has
taken its place.

By your argument there is no constant which doesn't fit into an int!


That's a different issue. It's legal, but you wouldn't say that -1 fits
into a size_t, would you?
Well, you can force it in there, so it fits, sort of, not bits are chopped
off, added or changed. You can't get it out again though, without knowing
how to interprete it (here as a signed int, which it was before the cast),
as bits changed their meaning

Bye, Jojo
 
M

Martin Wells

pete:
No.
Conversion to unsigned char isn't a bitwise operation.
Conversion to unsigned char depends on the value of (~0)
rather than on its bit pattern.
In one's complement, (~0) is either zero,
or negative zero, or a trap.


Is 0 allowed to be represented as negative zero? If so, then when all
its bits are flipped, you'd get a 0 for the MSB (in sign-magnitude of
course).
 
R

Ralf Damaschke

Martin said:
An int whose value is -1 will convert to an unsigned char whose
value is UCHAR_MAX, [...]
...actually I just found the following in the Standard:

| 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.
I've only got the 1999 Standard though. Can someone please
confirm whether C89 necessitates the same behaviour?

Well, my ascii text copy from the last draft (I think) of C89
has the same wording there. So that should be ok.

Ralf
 
S

Steven Simpson

pete said:
(~0) is INT_MIN in sign and magnitude representation.
(~0) is -1 in two's complement representation.

What about ~0u? And then (unsigned char) (int) ~0u as it goes through
memset?
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

pete:

Is 0 allowed to be represented as negative zero?

No, it isn't. Neither is -0, -1 * 0, -1 + 1, or anything like that. On
the implementations that support negative zeroes at all, they are only
allowed to be generated by bit manipulation, or operations that already
involve another negative zero.
 
?

=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

What about ~0u?

This is always equal to UINT_MAX, regardless of the size, width, or
representation of any type, ...
And then (unsigned char) (int) ~0u as it goes through
memset?

....but (int) UINT_MAX results in an implementation-defined value or
signal. You can't rely on it having all bits set, and keep your code
strictly conforming at the same time.
 
P

pete

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?= said:
No, it isn't. Neither is -0, -1 * 0, -1 + 1, or anything like that. On
the implementations that support negative zeroes at all,
they are only allowed to be generated by bit manipulation,
or operations that already involve another negative zero.

And, on the implementations that support negative zeroes at all,
negative zero compares equal to zero.
 
O

Old Wolf

What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?

If you could read as well as you could repeat yourself,
you would have noticed that this question was already
answered on the other thread.

Here it is again:
memset( ptr, -1, size );

This will set all value bits in the memory to 1.

(unsigned char)-1 is guaranteed to be UCHAR_MAX,
regardless of the size of int. Unsigned integral
types are guaranteed to have a pure binary
representation.

Obviously there is no way to use memset to
set padding bits.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top