473,382 Members | 1,392 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,382 software developers and data experts.

What does that operation mean?

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

Sep 26 '06 #1
7 2216
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.

Sep 26 '06 #2
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
Sep 26 '06 #3
On Tue, 26 Sep 2006 10:08:25 +0100, Chris Dollin <ch**********@hp.com>
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
Sep 26 '06 #4
On Tue, 26 Sep 2006 20:54:15 GMT, Frederick Gotham
<fg*******@SPAM.comwrote 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
Sep 26 '06 #5
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?

Sep 27 '06 #6
Jack Klein wrote:
On Tue, 26 Sep 2006 10:08:25 +0100, Chris Dollin <ch**********@hp.com>
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)

Sep 27 '06 #7
In article <v3********************************@4ax.com>,
Jack Klein <ja*******@spamcop.netwrote:
>On Tue, 26 Sep 2006 20:54:15 GMT, Frederick Gotham
<fg*******@SPAM.comwrote 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.

Sep 30 '06 #8

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

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
24
by: Massimo Soricetti | last post by:
Think for a moment: IF you were Bjarne Stroustrup in 1985 (or whenever he were doing it) writing down C++, and knowing all you know NOW of the C++ language, (pro, cons, features of today...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
2
by: needin4mation | last post by:
I was looking at this code, and have seen it in other code (like it): /// <summary> /// Bitmap font class for XNA /// </summary> public class BitmapFont { private SpriteBatch m_sb; private...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.