Questions tagged [bitwise]

For challenges involving bitwise commands or manipulation of numbers at the bit level

Filter by
Sorted by
Tagged with
4 votes
19 answers
551 views

Double bit rotation to the right [closed]

Given a positive integer as input, output that integer, but with its bits rotated two times to the right. Also, think of the number as a donut of bits, eg. ...
user avatar
0 votes
4 answers
109 views

Calculate the truncated log2 of some unsigned integer [duplicate]

Definition Given some number, x calculate the smallest number i such that 2i≥x. This is not a duplicate of this question. It is slightly different in that an input of 16 should yield an output of 4 ...
user avatar
6 votes
3 answers
382 views

Dr. Lamport's Unfinished Business

Introduction Dr. Leslie Lamport, of the eponymous Lamport one-time signature scheme, is getting rather old. But he can't die until his most famous algorithm gets all the kinks ironed out! In ...
user avatar
12 votes
6 answers
796 views

Golf this Thumb-2 Constant!

One thing that is constantly frustrating when golfing ARM Thumb-2 code is generating constants. Since Thumb only has 16-bit and 32-bit instructions, it is impossible to encode every immediate 32-bit ...
user avatar
  • 4,100
37 votes
31 answers
4k views

Find the "Bittiest" Number [closed]

The Challenge Given a list of integers, the "bittiest" number among them is the one with the most bits on - that is, the largest amount of bits set to 1. Write a function (or a program) that ...
user avatar
  • 537
18 votes
9 answers
1k views

The Koszul Sign Rule

A Bit of Background The exterior algebra is a central object in topology and physics (for the physics concept cf. fermion). The basic rule dictating the behavior of the exterior algebra is that \$yx =...
user avatar
  • 2,075
12 votes
9 answers
808 views

Simulate D Flip-Flop

Objective Simulate an edge-triggered D Flip-Flop. What is D Flip-Flop? A D flip-flop is an electronic digital device that outputs an inputted data (abbr. D) with synchronization to a clock (abbr. ...
user avatar
  • 4,151
16 votes
23 answers
2k views

Summing a sub-square of a quarter of an infinite chessboard

Description of the problem Imagine a quarter of an infinite chessboard, as in a square grid, extending up and right, so that you can see the lower left corner. Place a 0 in there. Now for every other ...
user avatar
  • 13.8k
7 votes
13 answers
200 views

Computational Parity Party! [duplicate]

The computational parity of an integer is defined as 1 if the integer has an odd number of set bits, and 0 otherwise. (src). Input You will receive one nonnegative integer that you will take the ...
user avatar
  • 3,199
28 votes
12 answers
4k views

Compress 8 bits to 6 when certain bit patterns will never occur

Consider an eight bit number in which an even position bit will never be 1 if one of its neighbors is 0 (considering MSB and LSB as neighbors). There are fewer than 64 such numbers, so there is at ...
user avatar
  • 5,978
-2 votes
7 answers
394 views

Interpreting the Wolfram Code

Introduction An elementary cellular automaton is a cellular automaton that is 1-dimensional and has 2 states, 1 and 0. These cellular automata are categorized based on a simple code: the Wolfram code,...
user avatar
  • 2,035
-1 votes
2 answers
276 views

Mirror, Mirror, on the wall. Who's the fairest of them all?

Well, you know it's Snow White, and the evil Queen is at it again. Will Snow White be saved? Will she fall asleep once again? Will the Prince find her? Challenge: Given an arbitrary number (>= 2) ...
user avatar
  • 281
21 votes
22 answers
7k views

Floating Point XOR

Your task is pretty simple. Given two floats, bitwise xor the binary representation of them, and output that as a float. For example, ...
user avatar
  • 359
17 votes
18 answers
4k views

Decode a variable-length quantity

A variable-length quantity (also referred to as VLQ or uintvar) is a way to encode up to a 28 bit integer value using only as many bytes as necessary. This was used ...
user avatar
  • 11.1k
12 votes
30 answers
1k views

String to Bit Transition

Task Given an input string of one or more ASCII characters which codepoints are between 0 and 128 (exclusive), do the following: Convert each character into its 7-bit ASCII code (if the ASCII code is ...
user avatar
  • 2,118
30 votes
2 answers
1k views

Can a neural network recognize primes?

Background Recognizing primality seems like a poor fit for (artificial) neural networks. However, the universal approximation theorem states that neural networks can approximate any continuous ...
user avatar
  • 2,509
10 votes
21 answers
2k views

Compare four integers, return word based on maximum

This function should take four integer inputs (a,b,c,d) ...
user avatar
1 vote
1 answer
246 views

(bit)Wise Images [closed]

Given a truth table and two images, use the truth table as a bitwise operation to the two images. For example with the truth table: 1 0 1 0 0 0 1 0 (Note that ...
user avatar
  • 2,612
18 votes
32 answers
7k 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 ...
user avatar
  • 325
26 votes
15 answers
3k views

Toggle some bits and get a square

Given an integer \$N>3\$, you have to find the minimum number of bits that need to be inverted in \$N\$ to turn it into a square number. You are only allowed to invert bits below the most ...
user avatar
  • 165k
20 votes
22 answers
4k views

Double, XOR and do it again

We define the function g as g(n) = n XOR (n * 2) for any integer n > 0. Given x > 0, find the smallest integer y > 0 such that gk(y) = x for some k > 0. Example ...
user avatar
  • 165k
3 votes
2 answers
344 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, ...
user avatar
16 votes
17 answers
3k views

XOR sort an array

Given a key, and an array of strings, shuffle the array so that it is sorted when each element is XOR'd with the key. XOR'ing two strings To XOR a string by a key, XOR each of the character values ...
user avatar
  • 8,384
13 votes
22 answers
1k views

Alternating bit smearing

Introduction This challenge requires you to set the trailing zeros of an integers binary representation to 010101…, this is best explained with an example: Given ...
user avatar
  • 16.3k
22 votes
2 answers
742 views

Bitwise XOR of rational numbers

Introduction Every rational number between 0 and 1 can be represented as an eventually periodic sequence of bits. For example, the binary representation of 11/40 is ...
user avatar
  • 42k
33 votes
35 answers
5k 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 ...
user avatar
  • 565
4 votes
1 answer
271 views

Find the Closest Double Palindrome

Given a double-precision float, find the closest double-precision float whose binary representation is a palindrome. Input A floating point number x. You may use ...
user avatar
  • 2,858
14 votes
8 answers
2k views

Exploring the xorspace

The xorspace of a set of integers is the set of all integers that can be obtained by combining the starting integers with the usual bitwise xor operator (^). For ...
user avatar
  • 15.3k
8 votes
28 answers
2k views

Do two numbers contain unique powers of 2

Break two numbers up into their powers of 2, if they share any, return a falsey value. Otherwise, return a truthy value. If one input is 0, the answer will always ...
user avatar
  • 8,384
22 votes
19 answers
2k views

Different number, same weight

Background The Hamming weight of an integer is the number of ones in its binary representation. For this challenge, integers are represented with 32 bits, and they are unsigned. Challenge Given an ...
user avatar
  • 4,672
18 votes
9 answers
614 views

Make Wise Numbers

Wise is a simple bitwise language I designed a while back. It is based around Python's bitwise operations. It has several operations most of these are the same or very similar to the equivalent ...
user avatar
  • 84.3k
26 votes
26 answers
2k views

Digital Hardness of Integers

To find the digital hardness of an integer, take its binary representation, and count the number of times both a leading and trailing 1 can be removed until it ...
user avatar
  • 13.9k
16 votes
5 answers
1k views

Binary Convolution

A binary convolution is described by a number M, and is applied to a number N. For each bit in the binary representation of <...
user avatar
10 votes
9 answers
927 views

Super Folding Numbers

We have already defined a folding number here. But now we are going to define a Super Folding Number. A Super Folding number is a number that if folded enough times it will eventually reach one less ...
user avatar
  • 84.3k
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 ...
user avatar
  • 84.3k
42 votes
29 answers
3k views

Bitflip and negate

Given an integer, make an expression that produces it from 0 using unary negation - and bitwise complement ...
user avatar
  • 137k
14 votes
22 answers
3k views

Golf bit weaving

Note: the first half of this challenge comes from Martin Ender's previous challenge, Visualize Bit Weaving. The esoteric programming language evil has an interesting operation on byte values which it ...
user avatar
  • 48.2k
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\$ ...
user avatar
  • 16.4k
20 votes
12 answers
2k views

"Bit-borrow" two numbers

Did you know that a small number can borrow bits from a larger number? Here's an example. Let's say our two numbers 5 and 14. First, write them out in binary: ...
user avatar
30 votes
43 answers
3k views

Mean bits: an average challenge

Given an integer N >= 1, output the mean number of bits in an integer from 0 to N - 1 Specification The output can be calculated as the sum of the number of bits in the binary representation of each ...
user avatar
  • 10.7k
14 votes
9 answers
1k views

Compute CRC32 Hash

Credits This challenge originated from @miles. Create a function that computes the CRC32 hash of an input string. The input will be an ASCII string of any length. The output will be the CRC32 hash of ...
user avatar
  • 48.2k
19 votes
23 answers
5k views

Generate a parity bit

A parity bit, is one of the simplest forms of a checksum. First, you have to pick the parity, even or odd. Let's say we pick even. Now, we need a message to transmit. Let's say our message is "...
user avatar
24 votes
27 answers
6k views

Split a byte array into a bit array

Write a function that when given a buffer b (1 - 104857600 bytes long) and a number of bits n (1 <= n <= 64), splits the ...
user avatar
  • 371
22 votes
11 answers
2k views

The XOROR sequence

Cellular Automata are truly fascinating. The ones that are usually talked about are the binary ones, i.e., the ones representable by a number. However, those, in my opinion, have been done to death. ...
user avatar
4 votes
2 answers
257 views

Bit Manipulator/Reader

Introduction There are often times that we have challenges which require communication of some sort (such as in team KOTH), where density of information may be more important than density of code. ...
user avatar
  • 3,528
11 votes
8 answers
591 views

Bijective mapping from integers to a variable number of bits

A variable number of bits is an array of 0 or more bits. So [0, 1] is a variable number of bits, but so is []. Write a function ...
user avatar
  • 38.6k
41 votes
33 answers
12k views

Cheating a multiple choice test

Introduction While studying, I tried to come up with several ways to cheat a multiple choice test. It basically is a compressed version of the multiple choice answers. The method goes as following: ...
user avatar
  • 43.3k
110 votes
18 answers
5k views

Xorting an array

Conceptually, this challenge is really simple. You're given a list of non-negative integers ai. If possible, find a non-negative integer ...
user avatar
23 votes
29 answers
3k views

Produce an XOR table

Introduction XOR is a digital logic gate that implements an exclusive or. Most of the times, this is shown as ^. The four possible outcomes in binary: ...
user avatar
  • 43.3k
11 votes
7 answers
540 views

Mask an IP address and give its broadcast

Background Inspired by this Unix.SE question (and of course my own answer). When an IP address is specified for an interface, it is often given in this dotted-decimal form: ...
user avatar