1

For example, If I have a double,

double x = 123.567 

and I turn it to binary form, like

String y = Long.toBinaryString(Double.doubleToLongBits(x))

Now, I stuck in how to get each single bits from this binary(String y). Or Can I get those single bit from double x directly. Because String can not use logic and, I can't figure any idea about this.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
MarvinC
  • 89
  • 1
  • 1
  • 8
  • 1
    '`String` can not use logic' Who told you that? Because they're wrong. – bcsb1001 Jun 14 '16 at 20:28
  • You're on the right track with `doubleToLongBits`. You could inspect the characters of your string using `String.charAt(int)` or `String.toCharArray()`, or you could directly inspect the bits in the long using this technique: http://stackoverflow.com/questions/1092411/java-checking-if-a-bit-is-0-or-1-in-a-long – dnault Jun 14 '16 at 20:52
  • So you mean that is worked ? y & 0x1? – MarvinC Jun 14 '16 at 20:53
  • you can look at [this similar question](http://stackoverflow.com/questions/4095463/how-to-convert-a-byte-into-bits) which provides multiple different solutions – Hans Jun 14 '16 at 21:07

1 Answers1

0

I'm not entirely sure what the purpose of your approach is, but maybe you should have a look at BitSet? You could put your longs into an array, create a BitSet using valueOf and then use a data structure that's actually meant to be used for logical operations.

Silverclaw
  • 1,316
  • 2
  • 15
  • 28