Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What does that operation mean?

Reply
Thread Tools

What does that operation mean?

 
 
Al
Guest
Posts: n/a
 
      09-26-2006
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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      09-26-2006
Al wrote:

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

--
Chris "all to pieces, bits and pieces" Dollin
I'm full of sweetness and light. And I'm /keeping/ it.

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      09-26-2006
Al posted:

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



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

--

Frederick Gotham
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-26-2006
On Tue, 26 Sep 2006 10:08:25 +0100, Chris Dollin <>
wrote in comp.lang.c:

> Al wrote:
>
> > 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.


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-26-2006
On Tue, 26 Sep 2006 20:54:15 GMT, Frederick Gotham
<> wrote in comp.lang.c:

> Al posted:
>
> > j=(int)(x)&512;

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-27-2006
Chris Dollin wrote:
> Al wrote:
> > 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.


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

Jack Klein wrote:
> 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?

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      09-27-2006
Jack Klein wrote:

> On Tue, 26 Sep 2006 10:08:25 +0100, Chris Dollin <>
> wrote in comp.lang.c:
>
>> > 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.

>
> 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;`.)

--
Chris "`x` marks the nasal demon" Dollin
RIP John M. Ford (April 10, 1957 -- September 24, 2006)

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      09-30-2006
In article <>,
Jack Klein <> wrote:
>On Tue, 26 Sep 2006 20:54:15 GMT, Frederick Gotham
><> wrote in comp.lang.c:
>
>> Al posted:
>>
>> > j=(int)(x)&512;

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


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
aspnet_regiis -pc operation does not honor -size argument (.NET 2. =?Utf-8?B?Sm9zaHVhQmlhZ2lv?= ASP .Net 2 04-13-2007 04:12 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57