• (nodebb)

    01100110 01110010 01101001 01110011 01110100

  • Long Time Lurker (unregistered) in reply to nerd4sale

    @nerd4sale you forgot the 00100001 at the end

  • Sauron (unregistered)

    We should name that a "Bugificator Pattern".

  • (nodebb)

    I get confused newbies asking how to convert an integer into its binary representation from time to time, to use with bitmasks and the like (i.e. not for printing to screen or anything else requiring a string). Takes all my diplomacy not to facepalm myself so hard my hand goes through the back of my skull.

  • COBOL Vet (unregistered) in reply to prueg

    I guess you are just too young to remember BCD.

  • Mark B (OP) (unregistered)

    Wow Remy, your WTF-fu is superior to mine. You have uncovered a level of WTAF that I missed completely.

    taskDetail is a (int) bit mask, and as you have described, here it gets converted to a long which is the decimal interpretation of the binary value. Then it gets passed to the JavaScript front end via JSON. Once in JS, it gets converted back to an int using the call:

    parseInt(binaryTaskDetail, 2)

    and then used normally as a bit mask. How can this work? binaryTaskDetail is still an int in JS.

    Well, parseInt expects a string as its first parameter, so JS helpfully does an implicit toString*, which is the binary value that we want, and we end up with the correct value, completely by accident.

    The WTF is that it could have just been passed as an integer value the whole way through.

    • At least I assume it does, it's the only possible way I can see that it can work - and it does work
  • (nodebb)

    Probably a WTF, but not necessarily. There are a few edge cases where doing this ONCE outside of time critical code (talking true real-time where nS may matter) .

  • (nodebb)

    Ha, come on, the reason is f*cking simple. Just do a ToString on that number again, and then take a look at a specific position in order to find out if a specific bit has been set (1) or not (0). So easy, actually any beginner ought to understand that principle.

  • (nodebb) in reply to COBOL Vet

    the x86 NPX (old style x87 floating point support) still has BCD instructions.

    So do IBM zSystem mainframes.(1)

    And probably others.

    (1) For reasons of backward compatibility with System/360 compiled binaries.

  • RLB (unregistered)

    It makes you feel a bit off, Remy? It makes me feel a bit 0ff...

  • Foo AKA Fooo (unregistered) in reply to Steve_The_Cynic

    BCD, sure, but these are DCB.

  • Argle (unregistered)

    Binary is still a big mystery to a lot of programmers. Sometimes I think that's probably for the best. Just concern yourself with tasks at hand, the vast majority of which won't need any awareness of bits. Me? I can't help but seeing the path down to binary. The first computer I had at home was a loaner from an adult mentor. It was a 6800 based computer with 8 LEDs and 9 toggle switches. But even today, I will be with many year programming veterans who can't adequately explain what a byte is. Sad, but what are you going to do other than create sites like this one?

  • (nodebb)

    I once worked with a developer who thought that she needed to convert her integer into hex format before doing some more work with it. Thankfully, she recognized her error before she committed the code. (I'd like to think that she tested the code and realized how wrong everything was.)

  • Randal L. Schwartz (google) in reply to nerd4sale

    As I mentioned earlier, the binary numbers "01100110 01110010 01101001 01110011 01110100" could represent ASCII code for a series of characters. To interpret these numbers as ASCII characters, we need to translate each binary number into its corresponding ASCII character. Here is how to do that:

    01100110 = "f" 01110010 = "r" 01101001 = "i" 01110011 = "s" 01110100 = "t"

    So when these binary numbers are interpreted as ASCII characters, they spell out the word "first". Again, this is just one possible interpretation of these numbers, and without more context it is impossible to say for sure what they mean.

  • Officer Johnny Holzkopf (unregistered) in reply to BernieTheBernie

    Why a .toString()? You can easily do bitmask stuff with basic math on integers as well: Just use division and modulo to get the "number bit" (integer value 0 or 1) at the required decimal position you are interested in and compare it. And if you like, translate from integer to floating point, because that gives you a more accurate result ("If this zero would be a lot bigger, it would almost be a small portion of a 1!"). That looks so much more educated and science-y than querying positions in an ASCII string (typical coding bootcamp participant's level), justifying the "But I have a degree in computer science!" statement that got you into the job, and you can turn boring truth tables and flag checks into bar graphs and pie charts. PHBs will love it.

  • (nodebb)

    Fun fact, there was a ternary computer that was developed in USSR at some point in the distant past.

  • (nodebb) in reply to Randal L. Schwartz

    So when these binary numbers are interpreted as ASCII characters, they spell out the word "first".

    Nope. They spell out the non-word "frist".

    Again, this is just one possible interpretation of these numbers, and without more context it is impossible to say for sure what they mean.

    For sure we can say that interpreted in EBCDIC, they mean nothing at all, since none of those byte values mean anything in the basic EBCDIC code pages.

  • RLB (unregistered) in reply to Randal L. Schwartz

    Again, this is just one possible interpretation of these numbers, and without more context it is impossible to say for sure what they mean.

    Surely it's absolutely apparent that it's the floating point representation of 28,60297948?

  • (nodebb)

    It's clearly for an API that specifies inputs in decimal coded binary. Well, the specification used those words, but not necessarily in that order.

  • markm (unregistered) in reply to Randal L. Schwartz

    Lots of hardware can do BCD arithmetic, but in any computer language since the 1950s an integer variable is binary by default. I know of only two ways to write a program using BCD arithmetic: write in COBOL and define a field as BCD, or write in assembly language (where variables are defined only as an untyped space in memory) and directly call the BCD instructions.

  • سمارت بتس (unregistered)
    Comment held for moderation.

Leave a comment on “A Bit of a Misunderstanding”

Log In or post as a guest

Replying to comment #588848:

« Return to Article