-1

I want to use xor for my double numbers in matlab,but bitxor is only working for int numbers. Is there a function that could convert double to int in Matlab?

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
saba 2085
  • 21
  • 1
  • 4
  • 1
    Can you explain *why* you want to apply bitxor to a double ? Only there might be an easier way to achieve your goal. – Paul R Jul 19 '13 at 08:12
  • In my program I need to use double number between 0,1 .for using bit xor what should I do? – saba 2085 Jul 19 '13 at 08:15
  • @saba2085 but are you saying you want to find the xor of like 0.2343 and 0.789787 for example? Because in that case how are you defining xor? Casting to a integer will round off... – Dan Jul 19 '13 at 08:18
  • @saba: yes, but *why* do you need to XOR these numbers - this is not normally something you would ever want to do, so it would help to know what the reason behind the question is - that way you might get a better (or more appropriate) answer. – Paul R Jul 19 '13 at 08:30
  • 1
    I think you are trying to run before you walk with this problem. You don't seem to understand the basics of the data types or operations you are talking about. You should read about integers and floating point numeric representations, and then read about bitwise operations such as xor, and how they are usually applied. You do not seem to be listening to anyone's advise on this issue. – Hugh Nolan Jul 19 '13 at 10:48

2 Answers2

1

The functions You are looking for might be: int8(number), int16(number), uint32(number) Any of them will convert Double to an Integer, but You must pick the best one for the result You want to achieve. Remember that You cannot cast from Double to Integer without rounding the number.

If I understood You correcly, You could create a function that would simply remove the "comma" from the Double number by multiplying your starting value by 2^n and then casting it to Integer using any of the functions mentioned earlier, performing whatever you want and then returning comma to its original position by dividing the number by 2^n

Multiplying the starting value by 2^n is a hack that will decrease the rounding error. The perfect value for n would be the number of digits after the comma if this number is relatively small.

Please also specify, why are You trying to do this? This doesn't seem to be the optimal solution.

Grzegorz Piwowarek
  • 13,172
  • 8
  • 62
  • 93
  • 1
    I think it might make more sense to multiply by `2^n`? – Dan Jul 19 '13 at 08:41
  • I have fuzzy cellular automata with double numbers . At each stage of the implementation I want use cellular automata rules and after that use bitxor for result and some random number .after that result of using bit xor save to cellular automata for next stage. – saba 2085 Jul 19 '13 at 08:48
  • @pivovarit also I think `int16` is more appropriate as `int8` has a max of 127. Otherwise I think this is the correct answer. – Dan Jul 19 '13 at 08:57
  • 2
    @saba2085 add all of this information to the question, It will make everyone's life easier – Grzegorz Piwowarek Jul 19 '13 at 09:16
0

You can just cast to an integer:

a = 1.003

int8(a)

ans =

1

That gives you an 8 bit signed integer, you can also get other size i.e. int16 or else unsigned i.e. uint8 depending on what you want to do

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Not like this.I want to save my number completely.my numbers is like these 0.5,0.789 ... and I want to keep this form. – saba 2085 Jul 19 '13 at 08:20
  • So then what is `xor` for doubles? How are you defining it? – Dan Jul 19 '13 at 08:22
  • look in my program I change my number[0,256] to number between [0,1] and I want to use bitxor for my number and some random number (between [0,1]. when i say change double to int my goal is like this 0.789--->789 – saba 2085 Jul 19 '13 at 08:28
  • 2
    How exactly can I "look in your program" since you haven't provided any code? Why don't you just do it before you scale your numbers? Or else you could multiply them by 256, cast to int16, bitxor, and then divide by 256? – Dan Jul 19 '13 at 08:33
  • I have fuzzy cellular automata with double numbers . At each stage of the implementation I want use cellular automata rules and after that use bitxor for result and some random number .after that result of using bit xor save to cellular automata for next stage. – saba 2085 Jul 19 '13 at 08:53
  • Yes then multiply by 256, unint18, divide by 256. Basically do what pivovarit says – Dan Jul 19 '13 at 08:56
  • Thank's Dan and pivovarit.I found answer.using typecast typecast(bitxor(typecast(0.3243,'uint64'),typecast(0.12325,'uint64')),'double') – saba 2085 Jul 19 '13 at 09:24
  • 1
    @saba2085 that is definitely wrong! Please use pivovarit's method instead. – Dan Jul 19 '13 at 09:31