Link to home
Start Free TrialLog in
Avatar of cowanpp
cowanpp

asked on

How do I perform operations on the actual bits of a double?

I want to be able to change the actual bits of a given double value, how do I access the actual bits to change them?
Avatar of PlanetCpp
PlanetCpp

might be more able to help if you said what you want to do
but one trick i used once was to get rid of the decimal. i used it to XOR a float, as you know you can't normally do that so what i did was shift the bits over to get rid of the decimal point
so 10.5 was shifted once (variable = variable<<1)
105
then i put that into a long, xor'd it then shifted back
seemed to work fine
only really easy if you know that the number of decimal places will always be one or two. you can convert it to string and then check the decimal places that way, theres other ways also.
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
o....k i think you need to get off that HIGH horse you're on salte. i mis-stated what i meant by accident.
i meant 10.5*10 gets rid of the decimal point. as would
10.05*100 or #*(N*10) N being the decimal places
if you thought about it it was an obvious mistake.
I'm sure you don't make any mistakes though.
I don't mind when i make a mistake whether it be what i really thought to be right , a typo, or just a mis-statement. And i don't mind being told i was wrong the same as i would correct anyone else, you don't learn without making mistakes. But your reaction to a wrong statement whether meant lightly or not is sort of childish.
The problem with multiplying by a factor of 10 etc gets you the floating point value to a certain number of decimals, either by truncating or rounding. Either way, that wasn't how I interpreted the original question which was how one can manipulate the individual bits of a double. Regarding that original question there are two points which I tried to make clear in my posting:

1. You can easily enough store the double in some form of char or int buffer and do bit manipulation on it there.

2. Why would you do that? if you want to interpret the bits you probably want to make some software emulation. So I outlined how you can do that. If you want to make some encryption or other manipulation of the bits I also explained that. If you want to do something else, I really wonder what that something else is. In most cases it probably wouldn't make sense. For example if he really wanted to truncate or round the value then he doesn't really want to manipulate the bits but rather work on the value in a decimal 10 form and so the multiplication of 10 or 100 or 1000 etc becomes part of the solution. However, this isn't the individual bits of the double. A double isn't stored in decimal 10 format.

The only language that uses decimal 10 format values that I know of is COBOL which uses the so-called BCD numbers. BCD numbers are also supported on the intel platform but is a separate type from double or int or any other type.

Btw, the decimal type in C# - despite its name - isn't decimal, it is more akin to the currency type of Visual Basic which is an integer scaled by a factor of 10. This way you can represent 10.01 exactly as the value 1001 scaled by -2, i.e. divided by 100. What is good about the decimal type of C# though is that it is 128 bits with 32 bit scaling and 96 bit integer value. That gives very high precision.

Alf
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (https://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer