5 ms·
That pipe is a C bitwise OR operator. I’m not gonna pretend to know what that means. I'm honestly not being sarcastic, but I think there's something interestin
by petercooper 12y ago
That pipe is a C bitwise OR operator. I’m not gonna pretend to know what that means.
I'm honestly not being sarcastic, but I think there's something interesting about reading an enjoyable programming article by someone who doesn't know what bitwise OR is. It sorta feels like a huge reminder we've moved on to a whole new generation/level of abstraction where competent developers no longer need to understand binary.
I gave a talk the other day about the future of code (think 2024 and on) and one of the biggest things out of my research was that we'll eventually not need to think about intermediate data structures in code anymore as AI profilers, VMs, compilers, etc. will be able to optimize and profile almost everything away with developers working more with specifications and constraints. I wonder if in 10 years' time a programmer not knowing what a linked list or a binary tree is will not be considered unusual either.
- peterjmag 12y agoA couple links for those who, like me, want to learn more about bitwise operators: http://www.cprogramming.com/tutorial/bitwise_operators.html http://www.cprogramming.com/tutorial/bitwise_operators.html (A decent intro to the concept of bitwise operators and what you might use them for.) https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... (Their use in JS.) And Peter: I'd love to watch that talk! Was it recorded? A quick search for "peter cooper future of code" brought up some references, but no videos (at least not yet).
- protonfish 12y agoI've been a programmer for 15 years (web dev, self-taught) and I don't know what those are. I think a linked list is like a JSON object, right? Yes, I am a real developer but the technologies I use don't have those paradigms. I doubt it this is good or bad, just different.
- Igglyboo 12y agoA linked list is an actual data structure, whereas JSON is just a format for storing data. The computer actually has to lay the json out in memory somehow. JSONs have keys that are strings so a hashmap or hashtable is probably more likely. http://en.wikipedia.org/wiki/Linked_list http://en.wikipedia.org/wiki/Linked_list http://en.wikipedia.org/wiki/Hash_table http://en.wikipedia.org/wiki/Hash_table
- deckiedan 12y agoImagine an object: { data: stuff, next: more_stuff } `next` is a reference to the next object like this one in the list, and `data` is a reference to the actual object at this point in the list. You've now got a close-enough idea about what a linked-list is. You can also have dual-linked lists, where each object also has a `previous` pointer to the previous object in the list. If you want, you could store the data directly in this object, rather than having a pointer out to the actual data (of course). This kind of thing can be very useful for certain kinds of data manipulation, where the built-in lists aren't efficient enough - for whatever reason. Say the language you're using creates an entirely new list whenever you try to remove an element from the middle of a list - or when you need to be moving / removing elements all over the place. The big downside of such a linked list is that you can't easily jump to a specific item number in the list. So jumping to item 200 means walking through the first 199 elements in the list to find it. I've only had to implement a linked list once in a JS project, I don't remember the reason why. I think the algorithm just worked a lot more efficiently that way. One reason a linked list could be useful in a javascript (or PHP, or whatever) language would be, for instance, if you had a game where when a player hit a block with a hammer, that block would bounce up and down, as would the two blocks on either side. If you store all the blocks in a regular list, then you need to know the index of the block in order to find the blocks on either side. In a linked list, you don't. One of the original big advantages of linked lists is that you can have objects of variable size, and the list can grow and shrink without adjusting any of the previous elements. Opposed to this is an `array` (sometimes called a vector) which is a fixed size chunk of memory which is split up into identical sized parts. You can then jump to element 200 by looking at the starting position + 200 * the size of a single element. This works pretty well, but if you want to add another 20 elements into the middle of the array, you need to allocate a new chunk of memory and move the whole lot into it, and deallocate the previous entire array. This is one of the reasons why strings are usually immutable in many languages. Strings are stored as arrays, not linked lists of letters. I think it's worth learning about all this "Computer Science" stuff, even if you don't need it very often, as it means when you come across a new problem you have a much bigger mental toolkit from which to figure it out.
- tel 12y agoA better reason why linked lists are better than arrays is that they have obvious, fast immutable APIs. Immutable data is not always a win, but sometimes it is.
- tel 12y agoLinked lists are another way of getting a lot of the functionality of arrays. While arrays are like a box of ordered things, linked lists are like a chain. Here's a model of a linked list. We start with an empty list represented by an empty array. [] In normal arrays we'd add a number by putting it into the array, like [1], but we'll do something a little weirder: [1, []] we build a new array which contains one value and the empty array. We add new elements in the same way [] [1, []] [2, [1, []]] [3, [2, [1, []]]] The final one there is a bit like the array [3,2,1], but seems to have a lot more ceremony. In any case, the values above model linked lists. Why would you use such a thing? Well, when you want to push a new value on to an array you may find (internally) that the array you have so far is too small. To fix this, you build a new, larger array and move all of the old elements to the new array plus the one new thing you've added. This can be expensive. With linked lists, adding a new element is always the exact same process. It is in this way faster and simpler. Of course, the downside of linked lists is that there is more overhead to, say, read the 10th element. In an array you can just do a[10], but in an linked list you must do a[0][0][0][0][0][0][0][0][0][1]. Obviously this involves much greater overhead. Linked lists are closely related to infinite streams, however. Sometimes they better model what you actually want to talk about—the idea of having a value "now" and some more values "in the future". They also are much more naturally implemented immutably and have a much more natural sense of a fold or reduction. So, as with all data structures, there's a tradeoff.
- Glide 12y agoProbably not, you can have a career never using linked lists . The problem is that it's such a fundamental CS concept it's going to be hard to pass interviews without that knowledge if you were starting from a junior position. To me it's not a paradigm. It's how data is stored and represented in memory. Where such issues become problems in larger projects is when you have a lead that doesn't know what B-trees are and they need to optimize database access. Or that person is loud and influential and derails every technical conversation because it's way over their head.
- frik 12y ago> the technologies I use don't have those paradigms. Your language most certainly has a syntax for bitwise operators. If you want to optimize (speed up) your code, knowledge like that would help you. Node/Javascript: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators https://developer.mozilla.org/en/docs/Web/JavaScript/Referen... PHP: http://php.net/manual/en/language.operators.bitwise.php http://php.net/manual/en/language.operators.bitwise.php Python: https://wiki.python.org/moin/BitwiseOperators https://wiki.python.org/moin/BitwiseOperators Ruby: http://calleerlandsson.com/2014/02/06/rubys-bitwise-operators/ http://calleerlandsson.com/2014/02/06/rubys-bitwise-operator... Visual Basic: http://msdn.microsoft.com/en-us/library/wz3k228a.aspx http://msdn.microsoft.com/en-us/library/wz3k228a.aspx A linked list is a kind of (abstract) data structure (http://en.wikipedia.org/wiki/Linked_list http://en.wikipedia.org/wiki/Linked_list). If you come in touch with a static typed language (C, C++, C#, Java) such knowledge is essential. In languages like JavaScript and PHP you usually just use an object or an array. You can think of an linked list as an array of objects.
- protonfish 12y agoMost of the optimizing I do has to do with DOM manipulation and replacing slow generic library functions with native and/or custom-written ones. I've got a lot of low-hanging fruit to pick before I get to things like bitwise operators. Most optimizations I've seen are very language-specific and it can be hard to guess what is faster. Which is why profiling and a tool like http://jsperf.com/ http://jsperf.com/ are critical. By the way, from what I've read bitwise operators aren't very efficient in JavaScript because of the overhead of conversion from generic duck-typing to binary.
- petercooper 12y agoYou probably already know this, but your last sentence also has one minor advantage, in that you can use an otherwise pointless bitwise operation to strip off decimals, e.g. 100.919 | 0 .. => 100
- collyw 12y agoI actually fond the most efficient solution to a problem recently (generating massive numbers of DNA sequence combinations, and checking for the number of mismatches between them) came from using MySQL and bitwsie operators. I tried Python, it was at least an order of magnitude slower. I tried Java, but that ran out of memory surprisingly quickly. I might try it in C at some stage but that will involve a fair bit more time.
- jwmerrill 12y agoA linked list is a tool for making sure that your data is never in the cache when you want it to be that a lot of developers fetishize because lisp and because they're so easy to implement that every CS curriculum will make you implement one at some point. Just use (dynamically growable) arrays, like, almost always, or heaps or binary trees in special circumstances. Okay, now that I've got that out of my system, I'll try to contribute something positive. Linked lists are useful to understand because they're very similar to, but less complex than, a common representation of trees and graphs. In a tree or graph, each node can have several children instead of just one. There are also some special algorithms that make use of the ability to grow or shrink a linked list in the middle without copying the whole tail [1]. The thing that makes this more rarely useful than you might think is that random access to the middle is O(n), so you have to be moving around in order adding/deleting things. [1] Dancing Links: http://en.m.wikipedia.org/wiki/Dancing_Links http://en.m.wikipedia.org/wiki/Dancing_Links Note, this is actually a circular, doubly linked list.
- deleted 12y ago[deleted]
- thegaw 12y agohaha, knew I was gonna get called out on that one! That is super interesting and spot on in my case. Would love to see your talk. When I got to that part of the article I stared researching bitwise operators to try and give a short explanation. I couldn't sum it up well in the voice I wanted to. So I decided to just say, "I don't know, and that's OK." I try to write in a way that's accessible to people that are maybe newer at programming. Maybe without a CS background, like me. With this, I felt like there would likely be so many new concepts, that it would help to leave one out that wasn't essential to building the example. For my next post I should research and write about bitwise operators. I also do not know what a linked list or binary tree is, but I'll certainly take the time to learn about them in the future. Thanks for reading it and commenting.
- petercooper 12y agoI've already written a response to you elsewhere on this, but wanted to add here that I don't think writing about bitwise operators here would have made much sense. What struck me though was how you didn't just hand wave through it, but actually admitted you weren't au fait with them. I think this actually makes the content more accessible and could encourage readers to go and learn for themselves, never a bad thing!
- jonesetc 12y agoThis is an interesting thing that has cropped up in interviews with my company. The company is mostly made of C++ guys (lots of game and sim dev), but we have recently started on some web projects and we seem to have no idea how to evaluate interviewees. Personally, I am a recent grad (last May), but have focused on web dev in my free time. However, having gone through formal CS I do know a good bit about the low level data structures and bitwise operations. Some of the interviewees have been developing for over a decade and can't say what a linked list is, let alone contrast it with vector or an array. It is all, anecdotal, but we've seen a fair amount of correlation between ability to solve simple problems (writing simple functions with no expectation of optimization) and knowledge of these entry level CS topics. So as much as I feel like we are testing for the wrong things when we ask about simple bitwise operations or linked lists when interviewing for front-end JS guys, it has still been a good test for us. All of that being said, this post is definitely well written and interesting. It is odd to me that an author who seems interested in learning and trying something new would so actively choose not to know what the code he's writing (and telling the reader to write) is doing.
- javajosh 12y ago> Some of the interviewees have been developing for over a decade and can't say what a linked list is, let alone contrast it with vector or an array. Jaw dropping. Some degree of this is acceptable - e.g. I wouldn't really expect a front-end dev to know browser-native DOM manipulation, because everyone uses some sort of shim (particularly jQuery), and modern-modern frameworks abstract away DOM manipulation entirely (think React). But that's a high-level API trade-off, rather than a foundational issue. OTOH it's far more serious when a dev doesn't know about Array.forEach and insists on using $.each for iteration, showing an inability to distinguish between native JS and jQuery. Part of the problem with JS and data structures in particular is that, AFAIK, there is not even a de facto collections standard. Google's Closure structs[1] might come the closest, but generally brings a lot of baggage. js_cols[2] is pretty good. And then you have what I would consider "fringe" solutions like mori[3]. The bottom line is that even an autodidactic developer starting off in JavaScript land is going to have a much harder time learning about basic data structures than if they started off in Java land, which, while it has it's warts, does have a wonderful standard library for collections[4]. [1] http://docs.closure-library.googlecode.com/git/namespace_goog_structs.html http://docs.closure-library.googlecode.com/git/namespace_goo... [2] https://code.google.com/p/jscols/ https://code.google.com/p/jscols/ [3] https://github.com/swannodette/mori https://github.com/swannodette/mori [4] http://docs.oracle.com/javase/tutorial/collections/index.html http://docs.oracle.com/javase/tutorial/collections/index.htm...
- javajosh 12y ago>where competent developers no longer need to understand binary Experts master their dependancies. All software depends on binary representation. All expert programmers need to understand binary. Of course, you said "competent" and yes, you can get things done without knowing binary. Although it pains me to think that it can't be understood or explained simply! Bitwise or is so foundational to many techniques, like efficient storing flags, security, and so much more. BTW a 'bitwise or operator' loops through the digits of a binary representation of both operands, or's the binary digits, and outputs the result. That one little operator has elements of a transform, a for-each (or a map(), if you like), and a logical operator, all in one! It's powerful and cheap to compute, and programmers and others, would do well to learn what it does. One practical, everyday use: numeric arguments to chmod (e.g. chmod 712 myfile.txt)! The reason why the modes are assigned to powers of two then added is so that they can be recovered from a bitfield using...bitwise or.