![]() Imagine that it's 1996 and you just built a database system from scratch. If you ever have to interface with code written in Java or C or C++ you'll come across bitwise configuration flags eventually. Ok, ok, this has to be the most boring use of bitwise math. If it is 1 in either input value, but not bothīitwise Inverse operator sets result bit to 0īitwise Right Shift Operator. Move input bits right Once you learn them, you'll be able to use them for the rest of your life.īitwise AND Operator sets the result's bit to 1īitwise OR Operator set's the result's bit to 1īitwise XOR Operator sets the result bit to 1 If you're not familiar with them, spend a little time in IRB trying them out. Pretty much every programming languages comes with this set of bitwise operators. The following examples illustrate this using a single bit.Įxample of bitwise AND operations with a single bit.Įach pair of bits is anded separately Ruby's Bitwise Operators So if you have eight bits, you'll have eight separate ANDs happen. If both bits are 1, it sets the corresponding output bit to 1. Bitwise operators are very similar.įor example, bitwise AND takes two values and compares them bit by bit. The expression a & b returns true only if a and b are both true. You're probably comfortable with boolean operators like &. To do that we'll use the bitwise operators. Now that we know how to use binary literals in ruby, we can start playing with them. To add binary literals to your code, prefix the string of ones and zeroes with 0b. You can write binary numbers inside your Ruby code by using a syntax like 0b11111111. You can use the ord method in Ruby to get a character's ASCII code, and then use printf to print a "binary" representation of it.Īlthough we have to use a method like printf to display the number in binary, it was always binary. ![]() ![]() But what does that look like in ruby? In this example, I'm using Ruby to look up the ASCII char code for the letter "a", then printing it as "binary." You probably know that everything in your computer is represented as numbers, and those numbers are in binary format. If you're able to solve a problem of yours using the most basic operations that a computer is capable of (binary math), it doesn't get any more elegant than that. Who hasn't accidentally typed & when they meant &?īut if you grew up programming lower level languages like C or Assembler, or in my case Turbo Pascal then you've probably done at least some bit twiddling.īitwise solutions to problems are just cool. Ruby's bitwise AND and OR operators ( & and | ) are probably used more by accident than on purpose. Ruby (182) Honeybadger (80) Rails (58) JavaScript (54) PHP (42) Python (30) Laravel (27) Briefing (13) Go (13) DevOps (10) Django (10) Elixir (8) Aws (8) Node (8) Briefing 2021 Q3 (7) React (7) FounderQuest (6) Briefing 2021 Q2 (6) Conferences (5) Testing (5) Error Handling (5) Security (4) Developer Tools (4) Elastic Beanstalk (4) Heroku (3) Debugging (3) Docker (3) Markdown (3) Serverless (3) Events (2) Jekyll (2) Startup Advice (2) Guest Post (2) Sidekiq (2) Git (2) Front End (2) Rspec (2) Oauth (2) Logging (2) GraphQL (2) Flask (2) Sql (2) Websockets (2) Nextjs (2) Case Studies (1) Performance (1) Allocation Stats (1) Integrations (1) Bitbucket (1) Mobile (1) Gophercon (1) Clients (1) Vue (1) Lambda (1) Turbolinks (1) Redis (1) CircleCI (1) GitHub (1) Crystal (1) Stripe (1) Saas (1) Elasticsearch (1) Import Maps (1) Build Systems (1) Minitest (1) Guzzle (1) Tdd (1) I18n (1) Github Actions (1) Postgresql (1) Xdebug (1) Zend Debugger (1) Phpdbg (1) Pdf (1) Multithreading (1) Concurrency (1) Web Workers (1) Fargate (1) Active Record (1) Django Q (1) Celery (1) Amazon S3 (1) Aws Lambda (1) Amazon Textract (1) Sucrase (1) Babel (1) Pdfs (1) Hanami (1) Discord (1) Active Support (1) Blazer (1) Ubuntu (1) DynamoDB (1)Ĭhances are you don't usually need to do any bitwise math in your day job. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |