6 ms·
GG-flip: simplest and most efficient ways to flip the sign of a number
- vidanay 9y agoSounds suspiciously like a LUT
- dlahoda 9y agoGood solution. It grounds me even more to start learning Go lang.
- joosteto 9y agoSo what if x > MAXNUMBER/2 ? Also, how can this be faster than x=0-x; ?
- infogulch 9y agoThis took me a second to realize this is satire too.
- jwilk 9y agoIt's a joke. And yeah, it gives wrong answers for some numbers. BTW, JS has unary minus, so you can write: x = -x
- IncRnd 9y agoI think the author chose a more efficient table-driven approach, due to the extra 0 in your 0-x expression.
- kingo55 9y agoThe real joke was that he was interested in Bitcoin cash.
- dahart 9y ago> The final lib size is few GBs, so distribution may become a problem. Hahaha! I admit to being slow; I missed the above sentence the first time, and I actually skimmed the whole thing and made it here to the comments before realizing it was a joke. Reading it again, there are quite a few gems I missed the first time, it's quite funny. BTW check the linked thread on Reddit for more hilarity https://www.reddit.com/r/shittyprogramming/comments/7n35m7/how_to_flip_the_sign_of_a_number_a_minimal/ https://www.reddit.com/r/shittyprogramming/comments/7n35m7/h...
- wcr3 9y agothis reads like a slightly more legible game of code-golf
- emmelaich 9y agoLook at his other repos too, particularly rockstar.
- IncRnd 9y agoBitcoin Client, Critical Unflipper Vulnerability Steals Coins https://news.ycombinator.com/item?id=16206743 https://news.ycombinator.com/item?id=16206743 Apparently, the Federal Government, Amazon, Google, and Facebook have known about the security issues of directly using unary negation without the gg-flip table driven approach for some time. The link claims Shodan found over 20,000 vulnerable government servers and to apply gg-flip immediately.
- wcr3 9y ago- no test suite - no webassembly - no semantic versioning the idea is good, but the execution needs work.
- johnfn 9y agoMaybe this is a problem we could use ethereum smart contracts to solve. Actually, is flipping a number even in P? Verification is easy - just compare against the negative - but I'm not sure if flipping the number can be done in polynomial time, enumerating all cases (as OP has done) aside.
- justin_vanw 9y ago>>GG Flip is a Golang library which generates the Javascript sign flip library. I preferred Go because lack of generics seemed like a good design choice. God help us if this isn't satire.
- simooooo 9y agoConsidering the context of the whole article. It's hilarious
- perl4ever 9y agoThe obvious solution would be x = ~x + 1, right? And don't even bother if x is the minimum signed integer.
- Ono-Sendai 9y agoThe usual/obvious solution is to flip the sign bit. (for floating point numbers). I think you're right for two's complement integers.
- perl4ever 9y agoI was kidding...but I was also thinking of the thread where people were discussing big vs little endian and the idea that since nobody uses big endian any more, it's not necessary to allow for it.
- OskarS 9y agoEndianess only matters for how numbers are stored in memory, but operations like bitshifts, boolean operations and arithmetic happen after the number has been loaded into a processor register, so endianess doesn't matter. Even if that wasn't the case, flipping a two's complement integer requires only negation and addition (as you showed), and those don't really depend on data layout.
- gugagore 9y ago> and those don't really depend on data layout. Not sure I understood this, but 01+09 = 10 and 10+90 = 100 and 10 ≠ fix_endianness(100)
- rkeene2 9y ago10!=01 and 09!=90 The endian-ness is only the in-memory layout of the bits and has nothing to do with how VALUES are treated. 1+9=10, regardless of how those values are stored, similarly 1<<9=512, regardless of how the CPU internally arranges those bits. The same holds true for inverting the value, the internal arrangement of the bits is irrelevant. Really you only care about endianness when you fail to treat numbers as numbers, for example by treating them as bytes.
- IncRnd 9y agoIs this a joke? Two's complement: flips the bits and add one FP: flip the sign bit OR.... x=-x
- krisives 9y agoIt does have an enterprise edition!
- IncRnd 9y agoExcellent! That solves the distribution problem, due to the size of the release. Nobody will notice anything off, in enterprise deployments.
- jschwartzi 9y agoThere are several security issues related to usage of the negation operator that make it undesirable to use in this situation. The solution presented in the article avoids those holes. It also has the advantage that a switch statement is more explicit than use of a language facility.
- IncRnd 9y agoNice, but you really should give some examples, too. Something like: The Bitcoin client's use of unary negation has caused traders to lose $32.49 Billion over the last three week period, according to a recent study by Price Waterhouse Cooper. From a similar accounting produced by the US Office of Management and Budget, HODLers lost an amazing $1.479 Trillion - That's Trillion with a "T"! A table-driven Unary NOT solves the issues caused by the use of Bitwise NOTs and direct operator use. To that end, we submitted a pull request. After 4 weeks of repeatedly sending the same pull request to the BC team (and being ignored), we are now disclosing this Bitcoin Client issue for everyone to see. Remember to patch this use in your Bitcoin Client, or your funds may be stolen! If you need to use an editor for this fix, and do not normally program, the use of SED will speed up your client edits tremendously. https://www.gnu.org/software/sed/manual/sed.html* https://www.gnu.org/software/sed/manual/sed.html* Now that this critical issue has been disclosed, we strongly urge all Bitcoin users to patch immediately, or their funds will likely be summarily depleted within a week.
- maweki 9y agoI wholeheartedly believed that the first approach was a popular npm library.
- avinassh 9y agosoon, it will be! (author here)
- Stratoscope 9y agoI think Stack Flip is the optimal solution, as it requires very little code and can handle any number, even values the code has never encountered before. The library simply calls out to a web service that posts the question on Stack Overflow. It should only take a few seconds for someone to post the answer. And the service will then upvote the answer, thus motivating Stack Overflow users to keep providing those results.
- make3 9y agowhat.. why..