background preloader

Bit Twiddling Hacks

Bit Twiddling Hacks
By Sean Eron Anderson seander@cs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please. The aggregate collection and descriptions are © 1997-2005 Sean Eron Anderson. The code and descriptions are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY and without even the implied warranty of merchantability or fitness for a particular purpose. As of May 5, 2005, all the code has been tested thoroughly. Contents About the operation counting methodology When totaling the number of operations for algorithms here, any C operator is counted as one operation. Compute the sign of an integer The last expression above evaluates to sign = v >> 31 for 32-bit integers. Alternatively, if you prefer the result be either -1 or +1, then use: sign = +1 | (v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then -1, else +1 On the other hand, if you prefer the result be either -1, 0, or +1, then use: Related:  Algorithmes

turing Visualizing Algorithms The power of the unaided mind is highly overrated… The real powers come from devising external aids that enhance cognitive abilities. —Donald Norman Algorithms are a fascinating use case for visualization. To visualize an algorithm, we don’t merely fit data to a chart; there is no primary dataset. But algorithms are also a reminder that visualization is more than a tool for finding patterns in data. #Sampling Before I can explain the first algorithm, I first need to explain the problem it addresses. Light — electromagnetic radiation — the light emanating from this screen, traveling through the air, focused by your lens and projected onto the retina — is a continuous signal. This reduction process is called sampling, and it is essential to vision. Sampling is made difficult by competing goals. Unfortunately, creating a Poisson-disc distribution is hard. You can see from these dots that best-candidate sampling produces a pleasing random distribution. Here’s how it works: Now here’s the code:

Low Level Bit Hacks You Absolutely Must Know I decided to write an article about a thing that is second nature to embedded systems programmers - low level bit hacks. Bit hacks are ingenious little programming tricks that manipulate integers in a smart and efficient manner. Instead of performing some operation (such as counting the 1 bits in an integer) by looping over individual bits, these programming nuggets do the same with one or two carefully chosen bitwise operations. To get things going I'll assume that you know what the two's complement binary representation of an integer is and also that you know all the the bitwise operations. I'll use the following notation for bitwise operations in the article: & - bitwise and | - bitwise or ^ - bitwise xor ~ - bitwise not << - bitwise shift left >> - bitwise shift right The numbers in the article are 8 bit signed integers (though the operations work on arbitrary length signed integers) that are represented as two's complement and they are usually named 'x'. Here we go. Bit Hack #1. 1. 2.

visualgo.net/sorting.html VisuAlgo was conceptualised in 2011 by Dr Steven Halim as a tool to help his students better understand data structures and algorithms, by allowing them to learn the basics on their own and at their own pace. VisuAlgo contains many advanced algorithms that are discussed in Dr Steven Halim's book ('Competitive Programming', co-authored with his brother Dr Felix Halim) and beyond. Today, some of these advanced algorithms visualization/animation can only be found in VisuAlgo. Though specifically designed for NUS students taking various data structure and algorithm classes (e.g. CS1010, CS1020, CS2010, CS2020, CS3230, and CS3233), as advocators of online learning, we hope that curious minds around the world will find these visualisations useful too. VisuAlgo is not designed to work well on small touch screens (e.g. smartphones) from the outset due to the need to cater for many complex algorithm visualizations that require lots of pixels and click-and-drag gestures for interaction.

Binary Game Skip to Content | Skip to Footer Cisco Binary Game The Cisco Binary Game is the best way to learn and practice the binary number system. It is great for classes, students and teachers in science, math, digital electronics, computers, programming, logic and networking. Katrina Ellison Geltman Thu 20 February 2014 Simulated annealing is a method for finding a good (not necessarily perfect) solution to an optimization problem. If you're in a situation where you want to maximize or minimize something, your problem can likely be tackled with simulated annealing. The traveling salesman problem is a good example: the salesman is looking to visit a set of cities in the order that minimizes the total number of miles he travels. Why choose simulated annealing? There are many optimization algorithms, including hill climbing, genetic algorithms, gradient descent, and more. You can visualize this by imagining a 2D graph like the one below. Broadly, an optimization algorithm searches for the best solution by generating a random initial solution and "exploring" the area nearby. This is perfectly logical, but it can lead to situations where you're stuck at a sub-optimal place. On top of this, simulated annealing is not that difficult to implement, despite its somewhat scary name. Example Code

A Stick Figure Guide to the Advanced Encryption Standard (AES) (A play in 4 acts. Please feel free to exit along with the stage character that best represents you. Take intermissions as you see fit. Click on the stage if you have a hard time seeing it. Act 1: Once Upon a Time... Act 2: Crypto Basics Act 3: Details Act 4: Math! Epilogue I created a heavily-commented AES/Rijndael implementation to go along with this post and put it on GitHub. The Design of Rijndael is the book on the subject, written by the Rijndael creators. Please leave a comment if you notice something that can be better explained. Update #1: Several scenes were updated to fix some errors mentioned in the comments.Update #2: By request, I've created a slide show presentation of this play in both PowerPoint and PDF formats.

Algorithmist Ethiopian multiplication Ethiopian multiplication You are encouraged to solve this task according to the task description, using any language you may know. A method of multiplying integers using only addition, doubling, and halving. Method: Take two numbers to be multiplied and write them down at the top of two columns. In the left-hand column repeatedly halve the last number, discarding any remainders, and write the result below the last in the same column, until you write a value of 1. In the right-hand column repeatedly double the last number and write the result below. stop when you add a result in the same row as where the left hand column shows 1. For example: 17 × 34 Halving the first column: Doubling the second column: Strike-out rows whose first cell is even: Sum the remaining numbers in the right-hand column: So 17 multiplied by 34, by the Ethiopian method is 578. The task is to define three named functions/methods/procedures/subroutines: References [edit] ACL2 [edit] ActionScript Output: ex. [edit] Ada Output:

university lectures computer science Whether your goal is to earn a promotion, graduate at the top of your class, or just accelerate your life, lectures can help get you there. Our archives of lectures cover a huge range of topics and have all been handpicked and carefully designed by experienced instructors throughout the world who are dedicated to helping you take the next step toward meeting your career goals. Lifelong learns can turn their free time turn into self-improvement time. The online lectures on this list are more than lecture notes or a slideshow on a topic -- they were designed for audiences like you, with carefully sequenced themes and topics taught by veteran educators, and often with additional resources for your own independent study. The lectures are available to anybody, completely free of charge. Lecture courses are a valid and vital learning tool, and may be one of the best methods of learning available.

Related: