Is there “0b” or something similar to represent a binary number in Javascript

后端 未结 10 1229
太阳男子
太阳男子 2020-11-29 02:32

I know that 0x is a prefix for hexadecimal numbers in Javascript. For example, 0xFF stands for the number 255.

Is there something similar f

10条回答
  •  迷失自我
    2020-11-29 03:12

    I know this does not actually answer the asked Q (which was already answered several times) as is, however I suggest that you (or others interested in this subject) consider the fact that the most readable & backwards/future/cross browser-compatible way would be to just use the hex representation.

    From the phrasing of the Q it would seem that you are only talking about using binary literals in your code and not processing of binary representations of numeric values (for which parstInt is the way to go).

    I doubt that there are many programmers that need to handle binary numbers that are not familiar with the mapping of 0-F to 0000-1111. so basically make groups of four and use hex notation.

    so instead of writing 101000000010 you would use 0xA02 which has exactly the same meaning and is far more readable and less less likely to have errors.

    Just consider readability, Try comparing which of those is bigger:
    10001000000010010 or 1001000000010010

    and what if I write them like this:
    0x11012 or 0x9012

提交回复
热议问题