Questions tagged [binary]

Binary is the base 2 number system. Use this tag for challenges involving base 2.

154 questions
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 multiplication ...
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 ...
6k views

Is it a Cyclops number? “Nobody” knows!

Task: Given an integer input, figure out whether or not it is a Cyclops Number. What is a Cyclops number, you may ask? Well, it's a number whose binary representation only has one ...
2k 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 (...
2k 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 ...
2k views

Counting in binary nybbles

This challenge is to output to your terminal, window, canvas or screen the numbers zero to 10 inclusive. Each outputted number must be shown as a 4-bit wide nybble, so zero must show as ...
2k 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 2n items, zero-...
13k 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 ...
8k 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 ...
2k 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 ...
6k 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... ...
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 “...
7k 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 <...
5k 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 ...
5k views

Find the number of leading zeroes in a 64-bit integer

Problem: Find the number of leading zeroes in a 64-bit signed integer Rules: The input cannot be treated as string; it can be anything where math and bitwise operations drive the algorithm The ...
505 views

Find how “smooth” a number is based on binary

I haven't been on PP&CG for a while, so I thought I would post something! Your task is to find how "smooth" a natural number is. Your method is to: 1: Convert the number to binary 2: Find the ...
5k views

prOGraMMIng PuZZleS & cOde ____

Input A non-empty encoded string consisting of printable ASCII characters (in the range 32-126), where some missing letters have been replaced with _. Output A ...
881 views

The Binary Fences

Input: An integer n in the range 2 <= n <= 10 A list of positive integers Output: Convert the integers to their binary ...
559 views

Fizzbuzz in any base

Challenge Input: An integer $b$ between 2 and 62 (inclusive). Output: Count from $1$ to the equivalent of $5000_{10}$ in base $b$, using any reasonable representation for the digits. ...
1k views

Decrypt xor-encryption

Your task is to take an encrypted string as input, and output the string decrypted, to reveal its hidden message. The strings, both the input and output, will contain characters from this list of 64 ...
2k views

Manchester encode a data stream

Manchester coding is a telecom protocol used in radio communications that guarantees bit transitions at a regular interval so a receiver can recover the clock rate from the data itself. It doubles the ...
6k 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 ...
1k 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 ...
13k views

i love yOu i lOve you i love yOu!

XKCD #99, with the title "Binary Heart" shows a simple image of ones and zeros, with some digits colored red. The red heart is visible to the naked eye, but the message hidden in the binary sequence ...
2k views

Find the BCD difference of a number

BCD difference Given an integer n, convert it to BCD (binary-coded decimal) by replacing each decimal digit with its 4-digit binary representation ...
2k views

Calculate hamming weight with low hamming weight

Create a program that computes the hamming weight of a string. Winner is the program with the lowest hamming weight. Rules: Hamming weight for an ASCII character is defined as the total number of ...
2k views

count ones in range

Challenge : Count the number of ones 1 in the binary representation of all number between a range. Input : Two non-decimal positive integers Output : The sum ...
4k views

The first n numbers without consecutive equal binary digits

The sequence contains the decimal representation of the binary numbers of the form: 10101..., where the n-th term has n bits. The sequence is probably easiest to ...
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 ...
14k 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: ...
3k 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 ...
2k 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: ...
4k 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 ...
446 views

Symmetric boolean functions as Zhegalkin polynomials

Let $\mathbb{B} = \mathbb{Z}_2 = \{0, 1\}$ be the set of booleans. A symmetric boolean function in $n$ arguments is a function $f_S : \mathbb{B}^n \to \mathbb{B}$ that checks if the number of ...
965 views

Minimum operations to get from one number to another

Let's define a simple language that operates on a single 8-bit value. It defines three bitwise operations (code explanation assumes 8-bit value variable): ...
3k 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 ...
7k 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 ...
99 views

Golf a bit-reversal table [duplicate]

The Premise: You are writing a C driver for a screen for use on an embedded system. The screen uses a tortured version of SPI that requires data to be sent LSb (least-significant-bit) first, but MSB (...
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 numbers ...
2k views

Diluted Integer Sums

A positive integer can be diluted by inserting a 0 between two bits in its binary expansion. This means that an n-bit number has ...
2k views

Binary Countdown Length

inspired by Count down from infinity Given a non-negative integer N, output the number of repetitions of the following steps it takes to reach 0: Convert ...
4k 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, ...
1k views

Calculate the binary split sum of a word

Take a string, s containing printable ASCII-characters as input, and output its "binary split sum". Need an explanation? How do you get the binary split sum? We'...
3k views

Fractal Cathedral

Given a positive integer n >= 1, output the first n rows of the following structure: ...
4k views

Calculate the longest series of 1's in an integer's binary value

Goal Given a non-negative integer, create a function that returns the starting position of number of largest consecutive 1's in that integer's binary value. When given an input ...
2k views

Compute the Binary Sierpinski Triangle Sequence

The Binary Sierpinski Triangle sequence is the sequence of numbers whose binary representations give the rows of the Binary Sierpinski Triangle, which is given by starting with a 1 in an infinite row ...
3k views

Complex Binary Numbers

Let's create a simple, surjective mapping from positive integers to Gaussian integers, which are complex numbers where the real and imaginary parts are integers. Given a positive integer, for example ...
317 views

Wanna be an Alchemist

Wanna be an Alchemist I've been watching Full Metal Alchemist these past days and I've been wondering, It is really possible to trans-mutate matter? In the real world, I don't know if it is possible, ...