473,503 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float and 1's

Hi,
Just like counting the number of bits set to
1 in an integral variable, can we count the same
in a float?

I have two solutions in my mind:

1.
union {
float f;
int i;
}u = { 10 };

Now, count the required number from u.i.
Will this work?
2.
float f;
unsigned char uc[sizeof (float)];

memcpy ( uc, &f, sizeof (float) );

Now count from the array. How about this?
Season's Greetings!

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #1
5 1923
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bt************@ID-203837.news.uni-berlin.de...
Hi,
Just like counting the number of bits set to
1 in an integral variable, can we count the same
in a float?

I have two solutions in my mind:

1.
union {
float f;
int i;
}u = { 10 };

Now, count the required number from u.i.
Will this work?
2.
float f;
unsigned char uc[sizeof (float)];

memcpy ( uc, &f, sizeof (float) );

Now count from the array. How about this?
Season's Greetings!

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/

Hello,

It is enough to declare the float then take a char* to it as:
float f;
char *p = &f;
// now do the bit counting in 'p' till sizeof(f) , making at most
sizeof(f)*8 bits

--
Elias
Nov 14 '05 #2
lallous wrote:
Hello,

It is enough to declare the float then take a char* to it as:
float f;
char *p = &f;
You need an explicit cast here. And it should be unsigned char*
probably.

unsigned char *p = (unsigned char *)&f;
// now do the bit counting in 'p' till sizeof(f) , making at most
sizeof(f)*8 bits


Note that this will also count any padding bits if there are any.

--
Thomas.

Nov 14 '05 #3
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:
Just like counting the number of bits set to
1 in an integral variable, can we count the same
in a float?

I have two solutions in my mind:

1.
union {
float f;
int i;
}u = { 10 };

Now, count the required number from u.i.
Will this work?
Not reliably. Note that float and int may or may not be the same
size. (As a matter of style, I'd use "10.0", or even "10.0f", rather
than "10" in the initialization to make it clear that you're
initializing the float member.)
2.
float f;
unsigned char uc[sizeof (float)];

memcpy ( uc, &f, sizeof (float) );

Now count from the array. How about this?


Yes, that should work, though others have pointed out that you don't
really need to copy f to an array.

BTW, I can't think of any use for the number of 1 bits in a float
other than idle curiosity -- not that there's anything wrong with idle
curiosity.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #4
On Fri, 2 Jan 2004 13:49:15 +0200, "lallous" <la*****@lgwm.org> wrote
in comp.lang.c:
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bt************@ID-203837.news.uni-berlin.de...
Hi,
Just like counting the number of bits set to
1 in an integral variable, can we count the same
in a float?

I have two solutions in my mind:

1.
union {
float f;
int i;
}u = { 10 };

Now, count the required number from u.i.
Will this work?
2.
float f;
unsigned char uc[sizeof (float)];

memcpy ( uc, &f, sizeof (float) );

Now count from the array. How about this?
Season's Greetings!

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/

Hello,

It is enough to declare the float then take a char* to it as:
float f;
char *p = &f;
// now do the bit counting in 'p' till sizeof(f) , making at most
sizeof(f)*8 bits


Aside from what others have said, your very last sentence is
completely wrong. I am working on a platform right now where CHAR_BIT
is 16. sizeof(float) is 2, and a float contains 32 bits.

It is definitely possible for the binary representation of a float to
have more than sizeof(float)*8 1 bits. In fact it could have as many
as sizeof(float)*16 1 bits.

But it will never, ever have more than sizeof(float)*CHAR_BIT 1 bits.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #5
[..]

BTW, I can't think of any use for the number of 1 bits in a float
other than idle curiosity -- not that there's anything wrong with idle
curiosity.


You are right. It was only my curiosity.

Thanks
vijay-z.
Nov 14 '05 #6

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

Similar topics

5
1860
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
14
2671
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
16
2529
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
6
2287
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
2388
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
11
2247
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
20
3117
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
8
5982
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
13
6146
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
3
10641
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.