What does that operation mean?

A

Al

Hi,
can anyone tell me what the following means? x is a float, and j an
integer. What value is in j afterwards?

j=0; j=(int)(x)&512;

Now if I use 128 instead of 512 what's the difference?

Thanks,
Al
 
C

Chris Dollin

Al said:
can anyone tell me what the following means?

You could try reading your C book, rather than asking
for help.
x is a float, and j an
integer. What value is in j afterwards?

j=0; j=(int)(x)&512;

We don't know what float value `x` has, so we don't know what
int value `(int)(x)` has (this could be written as `(int) x`),
so we don't know what value `(int) x & 512` has, except that
it's either 0 or 512, because of the way bitwise-& works.

There's no point in assigning `0` to `j` and then assigning
it the other value.

Don't post incomplete fragments.
Now if I use 128 instead of 512 what's the difference?

The answer's either 0 or 128.
 
J

Jack Klein

You could try reading your C book, rather than asking
for help.


We don't know what float value `x` has, so we don't know what
int value `(int)(x)` has (this could be written as `(int) x`),
so we don't know what value `(int) x & 512` has, except that
it's either 0 or 512, because of the way bitwise-& works.

No, it's undefined, and anything can happen. Accessing the value of
an uninitialized float produces the undefined behavior. Attempting to
cast to int and performing a bit-wise and has nothing to do with it,
the wheels have already fallen off before you get that far.
The answer's either 0 or 128.

There is no answer here, either, since the behavior is still
undefined.
 
J

Jack Klein

Al posted:



The value of the expression, "x", is converted to int, and then BitwiseAND'ed
with the integer value 512.

Accessing the uninitialized value of the float x causes undefined
behavior. What happens after that doesn't matter, at least not here.
 
O

Old Wolf

Chris said:
We don't know what float value `x` has, so we don't know what
int value `(int)(x)` has (this could be written as `(int) x`),
so we don't know what value `(int) x & 512` has, except that
it's either 0 or 512, because of the way bitwise-& works.

If 'x' has a value outside the range of int, then the behaviour
is undefined. Otherwise it is 0 or 512 :)

Jack said:
No, it's undefined, and anything can happen. Accessing the value of
an uninitialized float produces the undefined behavior.

What makes you think x is uninitialized?
 
C

Chris Dollin

Jack said:
No, it's undefined, and anything can happen. Accessing the value of
an uninitialized float produces the undefined behavior.

Good catch. I was rather assuming that his `x` had been given
/a/ value somewhere: I should have said so. (I did wonder if
the irrelevant `j=0;` was a typo for `x=0;`.)
 
K

Kenny McCormack

Accessing the uninitialized value of the float x causes undefined
behavior. What happens after that doesn't matter, at least not here.

What makes you think that x is uninitialized?

Just because you can't see it in the posted fragment, doesn't mean you
can assume that the posted fragment is the entire program. In fact, we
can be sure that the posted fragment is *not* the entire program, since
it would not compile as is.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top