Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [bitwise]

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

Filter by
Sorted by
Tagged with
11 votes
7 answers
736 views

This challenge is from a video game, Keep Talking And Nobody Explodes. A yes or no choice isn’t too bad. Unfortunately you have to make five of them and any of them could be your last. — from the ...
Dannyu NDos's user avatar
  • 7,371
8 votes
7 answers
944 views

The Compatibility Encoding Scheme for UTF-16: 8-Bit, also known as CESU-8, is a Unicode encoding that is not part of the Unicode standard, but mentioned in a Unicode Technical Report as a ...
Explorer09's user avatar
13 votes
17 answers
2k views

Recently I had to implement this problem in c#, and thought it would make for a good code golf. Your goal is to output the smallest unsigned data-type a 64 bit unsigned number can fit into. Input: An ...
Ezlanding's user avatar
  • 381
5 votes
9 answers
360 views

Given a binary string \$s\$ of length \$2^n\$ and a permutation \$\sigma\$ of \$\{1,\dots,n\}\$, generate the binary string \$u\$ of length \$2^n\$ which is a reordering of \$s\$ such that \$u[i^\...
Karl's user avatar
  • 871
22 votes
2 answers
2k views

The task is to find parameters that make a fast leap year check correct for the widest range of years. We assume the Proleptic Gregorian calendar, which extends the Gregorian calendar backward from ...
Falk Hüffner's user avatar
18 votes
12 answers
2k views

Exposition In this challenge, I am talking about ARM assembly language. ARM is a 32-bit RISC processor; I'll use instruction set up to architecture v6. To load a constant ("immediate") value ...
anatolyg's user avatar
  • 14.1k
17 votes
18 answers
2k views

In probability theory, a Bernoulli variable is a random variable which has a single parameter \$p\$, and is equal to 1 with probability \$p\$, and 0 with probability \$1-p\$. In this challenge, there ...
Command Master's user avatar
2 votes
0 answers
272 views

Problem You are given a binary string A of length N. You can perform the following type of operation on the string A: Choose two different indices \$i\$ and \$j\$ (\$1 \le i\$, \$j \le N\$) Change \$...
hemanth's user avatar
  • 21
15 votes
3 answers
621 views

The task is to count the number of disjoint 11 blocks in a 64-bit word, using the minimum number of straight-line instructions. That is, how many nonoverlapping ...
Falk Hüffner's user avatar
2 votes
3 answers
346 views

Given two non-zero 16-bit integers a and b, decide the smallest number of shift operations needed to turn ...
l4m2's user avatar
  • 32.4k
9 votes
3 answers
537 views

Introduction In the binary representation of a \$k-\$bit \$(k>1)\$ square number \$n^2, \$ not all bits can be \$1\$. The maximum number of \$1\$-bits (Hamming weight, \$B\$ for short) is given in ...
Hugo Pfoertner's user avatar
9 votes
2 answers
691 views

I want to read two strings on separate lines, each string the same length and containing only 0's and 1's, and determine if the first is the one's complement of the second. How succinctly can this be ...
Fixee's user avatar
  • 191
4 votes
19 answers
713 views

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. ...
Joao-3's user avatar
  • 2,080
0 votes
4 answers
157 views

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 ...
Nigel's user avatar
  • 357
6 votes
3 answers
460 views

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 ...
JamesTheAwesomeDude's user avatar
16 votes
6 answers
968 views

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 ...
EasyasPi's user avatar
  • 5,355
18 votes
9 answers
1k views

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 =...
Hood's user avatar
  • 2,115
12 votes
9 answers
1k views

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. ...
Dannyu NDos's user avatar
  • 7,371
16 votes
23 answers
2k views

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 ...
RGS's user avatar
  • 14.2k
7 votes
13 answers
315 views

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 ...
S.S. Anne's user avatar
  • 3,358
29 votes
12 answers
4k views

Consider an eight bit number in which an even position (second, fourth, etc) bit will never be 1 if one of its neighbors is 0 (considering MSB and LSB as neighbors). There are fewer than 64 such ...
Sparr's user avatar
  • 6,136
-2 votes
7 answers
512 views

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,...
sugarfi's user avatar
  • 2,231
-2 votes
2 answers
325 views

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) ...
Avi's user avatar
  • 279
22 votes
22 answers
9k views

Your task is pretty simple. Given two floats, bitwise xor the binary representation of them, and output that as a float. For example, ...
virchau13's user avatar
  • 499
19 votes
22 answers
6k views

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 ...
640KB's user avatar
  • 12.4k
12 votes
30 answers
2k views

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 ...
justhalf's user avatar
  • 2,279
19 votes
22 answers
4k views

Challenge Given an IPv4 address in dotted-quad notation, and an IPv4 subnet in CIDR notation, determine if the ...
AdmBorkBork's user avatar
  • 43.7k
31 votes
2 answers
1k views

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 ...
A. Rex's user avatar
  • 2,617
10 votes
21 answers
2k views

This function should take four integer inputs (a,b,c,d) ...
Mr Anderson's user avatar
1 vote
1 answer
299 views

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 ...
MilkyWay90's user avatar
  • 2,742
19 votes
32 answers
9k views

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 ...
Dave's user avatar
  • 335
26 votes
17 answers
3k views

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 ...
Arnauld's user avatar
  • 206k
20 votes
22 answers
4k views

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 ...
Arnauld's user avatar
  • 206k
3 votes
2 answers
395 views

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, ...
Luis felipe De jesus Munoz's user avatar
17 votes
17 answers
4k views

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 ...
ATaco's user avatar
  • 11.7k
13 votes
23 answers
2k views

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 ...
ბიმო's user avatar
23 votes
2 answers
1k views

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 ...
Zgarb's user avatar
  • 43.2k
34 votes
36 answers
6k views

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 ...
Defacto's user avatar
  • 575
4 votes
1 answer
308 views

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 ...
AJMansfield's user avatar
  • 2,898
15 votes
8 answers
2k views

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 ...
Grimmy's user avatar
  • 15.8k
10 votes
32 answers
2k views

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 ...
ATaco's user avatar
  • 11.7k
22 votes
19 answers
2k views

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 ...
musicman523's user avatar
  • 4,802
18 votes
9 answers
726 views

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 ...
Wheat Wizard's user avatar
  • 103k
26 votes
26 answers
2k views

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 ...
FlipTack's user avatar
  • 14.7k
17 votes
5 answers
2k views

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
1k views

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 ...
Wheat Wizard's user avatar
  • 103k
38 votes
27 answers
3k views

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
  • 103k
42 votes
29 answers
3k views

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

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 ...
Leaky Nun's user avatar
  • 50.6k
29 votes
30 answers
5k views

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
  • 17.3k