Questions tagged [binary]

For challenges about binary, or the base 2 number system.

Filter by
Sorted by
Tagged with
15 votes
14 answers
3k views

I want 8 bits for every character!

This is the reverse of this challenge. Given an encoded list of codepoints and the characters used to encode it, you need to decompress it to its original string. For example, given the encoded list <...
naffetS's user avatar
  • 12.2k
45 votes
84 answers
9k views

Binary to decimal converter

Binary to decimal converter As far as I can see, we don't have a simple binary to decimal conversion challenge. Write a program or function that takes a positive binary integer and outputs its ...
Stewie Griffin's user avatar
32 votes
77 answers
12k views

Count the number of ones in an unsigned 16-bit integer

Write some statement(s) which will count the number of ones in an unsigned sixteen-bit integer. For example, if the input is 1337, then the result is ...
ayane_m's user avatar
  • 1,019
11 votes
16 answers
3k views

Give me the Gray Code list of bit width n

The Gray Code is a sequence of binary numbers of bitwidth n where successive numbers differ only in one bit (see example output). Reference Example input: ...
ToonAlfrink's user avatar
  • 1,276
46 votes
11 answers
2k views

Run Stackylogic

Stackylogic is a logic-based programming language I made up that take in 0's and 1's for input and outputs a single ...
Calvin's Hobbies's user avatar
36 votes
11 answers
3k views

Encode an integer

Given positive integer n > 2. We convert it to an array as follows: If it is equal to 2 return an empty array Otherwise ...
user avatar
29 votes
30 answers
4k views

Bit-Reversal Permutations

Your goal is to create a function or a program to reverse the bits in a range of integers given an integer \$n\$. In other words, you want to find the bit-reversal permutation of a range of \$2^n\$ ...
miles's user avatar
  • 16.9k
86 votes
11 answers
16k views

How on earth did llhuii output the Evil Numbers in 42 bytes of Python?

This is a tips question for golfing in Python concerning the Evil Numbers question on Anarchy Golf. An number is evil if its binary expansion has an even number of 1's. The challenge is to print the ...
xnor's user avatar
  • 144k
66 votes
11 answers
4k views

Only Even Bytes

The scenario Lately you have been noticing some strange behavior with your favorite text editor. At first it seemed that it was ignoring random characters in your code when writing to disk. After a ...
Wheat Wizard's user avatar
  • 96.5k
55 votes
73 answers
8k views

In how many bits do I fit

For any positive 32-bit integer (1 ≤ n ≤ 0xFFFFFFFF) output the number of bits needed to represent that integer. Test cases ...
Bassdrop Cumberwubwubwub's user avatar
28 votes
26 answers
3k views

Find the next 1-sparse binary number

A positive integer N is K-sparse if there are at least K 0s between any two consecutive 1s in its binary representation. So, the number 1010101 is 1-sparse whereas 101101 is not. Your task is to ...
articuno's user avatar
  • 381
6 votes
7 answers
932 views

Find the simplest value between two values

Goal Your goal is to find the simplest value in an open interval. In other words, given two values a,b with a<b, output the ...
xnor's user avatar
  • 144k
35 votes
69 answers
9k views

Convert a string of binary characters to the ASCII equivalents

Take a string of binary characters separated by a space, and convert it to an ASCII string. For example... ...
James Williams's user avatar
15 votes
50 answers
6k views

Print the amount of ones in a binary number without using bitwise operators

Description Given a number, print the amount of 1s it has in binary representation. Input A number >= 0 in base 10 that ...
pimvdb's user avatar
  • 859
12 votes
15 answers
2k views

Quine outputs itself in binary

Your task, if you wish to accept it, is to write a program that outputs its own source code in the binary UTF-8 representation. Rules The source must be at least 1 byte long. Your program must not ...
mdahmoune's user avatar
  • 2,862
37 votes
27 answers
3k views

Folding Numbers

Given a number determine if it is a folding number. A folding number is a number such that if you take it binary representation and "fold" it in half, That is take the result of XNOR ...
Wheat Wizard's user avatar
  • 96.5k
37 votes
63 answers
8k views

Sum of Powers of 2

The Challenge Given an integer input x where 1 <= x <= 255, return the results of powers of two that when summed give <...
SpookyGengar's user avatar
  • 1,813
31 votes
70 answers
4k views

Number to Binary

It seems we've managed to go all this time without a plain vanilla Number-To-Binary challenge! Whilst this will inevitably be only one element in many languages, it should put a few esolangs through ...
ATaco's user avatar
  • 10.7k
31 votes
33 answers
4k views

Numbers with multiple runs of ones

Task Find the set of numbers such that the binary representation contains two or more runs of 1 separated by at least one 0. ...
user avatar
29 votes
20 answers
3k views

Simple Boolean Algebra Calculator

Background Boolean Algebra concerns representing values with letters and simplifying expressions. The following is a chart for the standard notation used: Above is what actual boolean algebra looks ...
Jacob Creutzfeldt's user avatar
26 votes
19 answers
2k views

Bubbler numbers

Background My user ID is 78410, or \$1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0_2\$. One interesting property of this number is that, in binary, it doesn't have three consecutive identical digits, and yet it ...
Bubbler's user avatar
  • 73.8k
25 votes
23 answers
2k views

Swap bits with their neighbours

Task description Given an integer, swap its (2k–1)-th and 2k-th least significant bits for all integers k > 0. This is sequence A057300 in the OEIS. (The number is assumed to have “...
Lynn's user avatar
  • 68.1k
21 votes
13 answers
1k views

The Binary Square Diagonal Sequence

The binary-square-diagonal-sequence is constructed as follows: Take the sequence of positive natural numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, ... Convert each number to ...
Laikoni's user avatar
  • 26.1k
17 votes
12 answers
2k views

Convert from binary to negabinary

Given a binary integer inclusively between 0 and 1111111111111111 (i.e. a 16-bit unsigned integer) as input, output the same ...
Peter Olson's user avatar
  • 7,722
16 votes
14 answers
969 views

Number of binary partitions

We all know that any positive integer can be represented as the sum of powers of two. This is how binary representations work. However there's not just one way to do this. The canonical method, ...
Wheat Wizard's user avatar
  • 96.5k
14 votes
2 answers
1k views

Count how many distance sequences are far from all others

The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. Let P be a binary string of ...
user avatar
7 votes
2 answers
857 views

math with bitwise operators

objective write a program that can perform addition, subtraction, multiplication, and division, using only bit-wise operators, by bit-wise operators i mean the equivalent of JavaScript ...
Math chiller's user avatar
2 votes
11 answers
8k views

compute number of 1 bits in a binary representation of a number

The goal is to write a program that returns number of 1 bits in a given number. Examples 5 -> 2 1254 -> 6 56465 -> 8 Winner The winning ...
Ali1S232's user avatar
  • 425
101 votes
23 answers
17k views

Where's the 0xBEEF?

This challenge was inspired by this Wendy's commercial from 1984. Illustration by T S Rogers Your task is to find a hexadecimal 0xBEEF on a binary bun. The 'beef' consists of the following pattern: ...
Arnauld's user avatar
  • 188k
74 votes
97 answers
7k views

Is the number binary-heavy?

An integer is binary-heavy if its binary representation contains more 1s than 0s while ignoring leading zeroes. For example 1 is ...
Mayube's user avatar
  • 11.6k
49 votes
51 answers
5k views

Bit run rundown

Given an integer n > 0, output the length of the longest contiguous sequence of 0 or 1 in ...
Arnaud's user avatar
  • 8,503
43 votes
25 answers
3k views

Let's study Neil numbers

Congratulations to Neil for hitting 100k rep! As a tribute, we are going to study 'Neil numbers'. Neil's user ID is 17602 and there's something special about the binary representation of this number: ...
Arnauld's user avatar
  • 188k
41 votes
14 answers
5k views

Incrementing Gray Codes

Introduction A Gray Code is an alternative to binary representation in which a number is incremented by toggling only one bit, rather than a variable amount of bits. Here are some gray codes along ...
Patrick Roberts's user avatar
41 votes
11 answers
4k views

Find a binary needle in a decimal haystack

The challenge You're given: a non-empty, unsorted list h of positive integers (the haystack) a positive integer n (the needle) Your task is to return the list of all unique decimal concatenations ...
Arnauld's user avatar
  • 188k
37 votes
49 answers
10k views

What is the reverse (binary) bit number?

So you are given a POSITIVE base 10 (decimal) number. Your job is to reverse the binary digits and return that base 10 number. Examples: ...
Joseph's user avatar
  • 955
36 votes
34 answers
5k views

Create a binary wall

Given an array of positive integers in base 10, where n > 0, output their representation of a binary wall. How does this work? Convert each number to it's ...
TheLethalCoder's user avatar
35 votes
36 answers
5k views

Binary Fibonacci

Challenge You need to generate a program or function that takes in a positive integer N, calculates the first N terms of the Fibonacci sequence in binary, concatenates it into a single binary number, ...
Nathan Wood's user avatar
34 votes
32 answers
4k views

16-bit binary grid

Given any unsigned 16 bit integer, convert its decimal form (i.e., base-10) number into a 4x4 ASCII grid of its bits, with the most-significant bit (MSB) at the top left, least-significant bit (LSB) ...
type_outcast's user avatar
33 votes
79 answers
17k views

Clearing the most significant bit from an integer

Input The input is a single positive integer n Output The output isn with its most significant bit set to ...
Marcus Andrews's user avatar
33 votes
15 answers
5k views

Let's play Mölkky!

Mölkky Mölkky is a Finnish throwing game. The players use a wooden pin (also called "mölkky") to try to knock over wooden pins of almost similar dimensions with the throwing pin, marked with ...
Arnauld's user avatar
  • 188k
32 votes
40 answers
3k views

Parenthifiable Binary Numbers

If you express some positive integer in binary with no leading zeros and replace every 1 with a ( and every ...
Calvin's Hobbies's user avatar
32 votes
19 answers
3k views

Decode USB packets

Inspired by this video by Ben Eater. This challenge forms a pair with Encode USB packets. The USB 2.0 protocol uses, at a low level, a line code called non-return-to-zero encoding (specifically, a ...
pxeger's user avatar
  • 23.3k
32 votes
49 answers
6k views

Is that number a Two Bit Number™?

Let's start by defining a Two Bit Number™: It is a positive integer When expressed as a binary string it has exactly 2 true bits OR When expressed as a decimal number, it has exactly 2 of the numeral ...
AJFaraday's user avatar
  • 11.8k
32 votes
32 answers
2k views

Decompose binary into alternating subsequences

This was inspired by Problem 13 - Non-Repeating Binary of HP CodeWars' recent competition. Let's take a random decimal number, say 727429805944311 and look at ...
El'endia Starman's user avatar
30 votes
45 answers
7k views

Convert a 32 bit binary IPv4 address to its quad-dotted notation

Challenge Write a program or function that converts a 32 bit binary number to its quad-dotted decimal notation (often used for representing IPv4) Quad-dotted decimal A quad-dotted decimal is formed ...
mabel's user avatar
  • 1,619
28 votes
12 answers
4k views

Densely packed decimal (DPD) to decimal

For nandgame fans: Please try out DPD to decimal in logic gates too! Background Densely packed decimal (DPD) is a way to efficiently store decimal digits in binary. It stores three decimal digits (...
Bubbler's user avatar
  • 73.8k
27 votes
21 answers
1k views

Primenary Strings

A Primenary (binary-prime) string is one which, when written as a binary grid, every row and column has a prime total. That's quite a vague explanation, so let's break it down with a worked example......
FlipTack's user avatar
  • 14.6k
26 votes
36 answers
4k views

Write out the Thue-Morse sequence

There's quite a few challenges on this site that ask you to print out a sequence, and this is no exception. (The following explanation of the sequence for this challenge assumes the symbols in the ...
Sherlock9's user avatar
  • 12.2k
26 votes
65 answers
6k views

String to Binary

This is a code golf challenge. Just like the title says, write a program to covert a string of ascii characters into binary. For example: "Hello World!" should ...
ericmarkmartin's user avatar
26 votes
14 answers
2k views

Counting valid Binary Sudoku rows

Background Binary Sudoku, also known as Takuzu, Binario, and Tic-Tac-Logic, is a puzzle where the objective is to fill a rectangular grid with two symbols (0s and 1s for this challenge) under the ...
Bubbler's user avatar
  • 73.8k