Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [bitwise]

Bitwise refers to a bitwise operation which operates on one or more bit patterns or binary numerals at the level of their individual bits.

Filter by
Sorted by
Tagged with
118 votes
11 answers
53k views

Fastest way to clamp an integer to the range 0-255

I'm working on some image processing code that can generate pixel values outside of the normal range of 0 to 255, and I'd like to clamp them back into the valid range. I know that there are saturating ...
Mark Ransom's user avatar
  • 1,866
57 votes
8 answers
36k views

Efficiently checking if a number is divisible by 3

I'm looking for code review, best practices and optimizations. ...
JavaDeveloper's user avatar
44 votes
9 answers
18k views

Copying 80 bytes as fast as possible

I am running a math-oriented computation that spends a significant amount of its time doing memcpy, always copying 80 bytes from one location to the next, an array ...
Alexandros Marinos's user avatar
33 votes
1 answer
73k views

Pack and unpack bytes to strings

I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that ...
Xeon06's user avatar
  • 433
32 votes
3 answers
7k views

Decide if the sum of three numbers is even or odd

Exercise from an Assembly course I'm enrolled into: Write a program that takes three numbers x,y,z as input, and returns: 0 if (x+y+z) is even. 1 if (x+y+z) is odd. (Note that ...
michael.zech's user avatar
  • 4,348
26 votes
4 answers
51k views

Counting number of 1's and 0's from integer with bitwise operation

Is there a better way of doing this? ...
Jaysinh Shukla's user avatar
26 votes
3 answers
2k views

Diamond in the rough! Finding Gems in the rocks

Recently a question was asked that solved a hackerrank problem: Counting gems amongst the rocks The description of the challenge is: John has discovered various rocks. Each rock is composed of ...
rolfl's user avatar
  • 97.4k
25 votes
3 answers
1k views

Gray codes addition

As some of you know, I have been working with Gray codes and am trying to design a good gray_code class in modern C++. I will end up posting several parts of the ...
Morwenn's user avatar
  • 20k
24 votes
3 answers
8k views

Converting an IP

I've written a DNS tool in Node and would like to know if there's a better and more efficient way of handling the conversion from an IP to a long. I'm not too good at bitwise just yet and would like ...
user avatar
23 votes
5 answers
5k views

Setting and getting bits in C

I am just starting to get my feet wet with C; and am rather enjoying myself in doing so. This is sort of a follow up to my last question here. I am posting this ordinate to hopefully getting some ...
motoku's user avatar
  • 2,521
17 votes
4 answers
6k views

Return the next power of two

Given an unsigned integer \$N\$, I want to return \$P\$ with \$P\$ being a power of two such that \$P \ge N\$. Examples: 1 -> 1 2 -> 2 3 -> 4 5 -> 8 The goal is to provide a very fast ...
Mr_Pouet's user avatar
  • 365
16 votes
1 answer
5k views

Compressing a 'char' array using bit packing

I have a large array (around 1 MB) of type unsigned char (i.e. uint8_t). I know that the bytes in it can have only one of 5 ...
avikpram's user avatar
  • 171
15 votes
2 answers
1k views

Program that guesses your number using bitwise operations

I got the idea for this program from this site. functions.cpp: ...
alienCY's user avatar
  • 363
14 votes
4 answers
2k views

Exhaustive search for encryption keys

This does the job, but is not especially elegant. What is the preferred Pythonic idiom to my childish nests? ...
jsj's user avatar
  • 467
14 votes
2 answers
20k views

Validating UTF-8 byte array

I'm writing a validator function that receives a byte[] and checks whether it represents a valid UTF-8 byte sequence, according to this table. Is my approach ...
Óscar López's user avatar
13 votes
4 answers
654 views

Finding the largest of a series of numbers being XORed

I'm working on solving a problem involving finding the largest of a series of numbers being XORed. This is the actual problem: Xorq has invented an encryption algorithm which uses bitwise XOR ...
Ryan McClure's user avatar
13 votes
2 answers
890 views

N-Queens, bitwise approach

I've recently solved the famous N-Queens problem: Can you place \$N\$ chess queens on a \$N × N\$ board in such a way that no one attacks another queen? The idea here is based on the ideas from ...
alecxe's user avatar
  • 17.3k
13 votes
2 answers
2k views

Calculating holes on a Tetris grid quickly

I have created a Tetris AI which can play Tetris on its own given a set of rules. It places the next piece in every possible way on the board, and calculates which position that gives the fewest "...
maxb's user avatar
  • 1,522
13 votes
1 answer
2k views

Steganography for images

For a school project I'm working on a small library to do steganography. I've just finished the first method of steganography and thought I'd get some feedback. This steganography method works by ...
Simon Verbeke's user avatar
12 votes
3 answers
27k views

Endianness conversion in C

I have written a simple C header for converting the endianness of short integers and long integers. It uses the GCC macro ...
Jack Wilsdon's user avatar
  • 1,651
12 votes
3 answers
19k views

Implementation of C Standard Library Function ntohl()

This is an implementation of ntohl() that I wrote as an exercise. ntohl() takes a uint32_t ...
ad absurdum's user avatar
12 votes
3 answers
6k views

Codility binary gap solution using regex

Below is the code I implemented to solve the binary gap problem: Find longest sequence of zeros, bounded by ones, in binary representation of an integer. However, it seems to be different from ...
Such Much Code's user avatar
12 votes
1 answer
649 views

10x10 bitmapped square with bits

This program prints out a 10x10 square and uses only bit operations. It works well, but I don't like that global array. Can you tell me if the code is proper or not? ...
mitya221's user avatar
  • 793
12 votes
3 answers
21k views

Bitmap implementation

I recently started cleaning up my projects and decided that I want to create a personal repository of useful bits of code. I found this bitmap code between them and heavily edited it and I'd ...
Dictum Mortuum's user avatar
12 votes
3 answers
1k views

Register "%b" conversion specifier

I'm writing a library to register the b, B conversion specifiers and make them work the closest possible to ...
alx - recommends codidact's user avatar
12 votes
1 answer
272 views

Array-like container for uints shorter than 8 bits

Follow-up: Array-like container for uints shorter than 8 bits (Rev 1) First of all: yes, I need this kind of array to generate data for a piece of hardware, and it must be stored and manipulated ...
Christoph's user avatar
  • 507
12 votes
1 answer
299 views

Console app to teach bitwise operators

I'm creating a console app that has the purpose to teach you how to use C# bitwise operators. I have several classes which contain info about the different operators and I instantiate/add them in the ...
Denis's user avatar
  • 8,478
12 votes
2 answers
17k views

Packing and unpacking two 32-bit integers into an unsigned 64-bit integer

Does this look good for combining/retrieving two 32 bit integers (a type and an index) into/from an unsigned 64 bit integer (a unique ID)? The point is to hide in the API the composition of the ...
JDiMatteo's user avatar
  • 223
11 votes
3 answers
3k views

Calculating int next power of two

Here is one of the possible solutions: ...
Nolequen's user avatar
  • 270
11 votes
4 answers
27k views

Find the binary gap of a number N

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of ...
Cormack's user avatar
  • 201
11 votes
3 answers
5k views

UTF-8 Validation

I've recently solved the "UTF-8 Validation" LeetCode problem: A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, ...
alecxe's user avatar
  • 17.3k
11 votes
3 answers
1k views

Counting the number of "on" bits in an int

Here is some code I wrote as a solution to Programming Exercise 15.3 in Stephen Prata's C Primer Plus, 6th Edition. It is probably worth pointing out that the title of the chapter is Bit Fiddling. The ...
ad absurdum's user avatar
11 votes
3 answers
3k views

Template for endianness-free code, data always packed as BIG-Endian

It's been a while since I've been properly grilled about my code. I had this Idea which should probably never make it into production code but still I couldn't find anything seriously wrong with it ...
CplusPuzzle's user avatar
11 votes
2 answers
5k views

Mostly portable 128 by 64 bit division

I wrote this out of curiosity. It is based on the theory behind Knuth's Algorithm D and is intended to emulate the behavior of the x86 div instruction (though the ...
defube's user avatar
  • 243
10 votes
3 answers
12k views

Calculating the 'binary gap' of a number

Inspired by this question and therefore this one I decided that clearly the best most readable way to solve this is with Regular Expressions. A binary gap within a positive integer N is any maximal ...
RobH's user avatar
  • 17k
10 votes
2 answers
1k views

Bouncing map back into its bounds, after user dragged it out

Users like exploring maps, seeing all that is around. However they tend to go too far. To prevent the map from getting lost to the user it bounces back into the set bounds, as defined by a rectangle. ...
Remy's user avatar
  • 289
10 votes
4 answers
5k views

Boolean expression complexity and bitwise operators

Given this kind of code: ...
janos's user avatar
  • 111k
10 votes
1 answer
7k views

Bit shifting and masking

I was wondering if someone could review this simple bit of code that originally was in C++, but now converted to C# as practice in using bit-shifting and creating a sort of a quasi-array functionality....
SomeStudent's user avatar
10 votes
3 answers
381 views

Fastest sum of absolute values of 32 differences

A long should be treated as a list of 32 unsigned numbers, each of them just two bit long. So 0x1234F... represents ...
maaartinus's user avatar
  • 13.5k
10 votes
1 answer
2k views

Logical shift with signed numbers

I am going through the Coursera course The Hardware/Software Interface and because it has already ended, I would not be able to get any feedback on the code. The assignment is: LogicalShift: ...
Yan's user avatar
  • 683
10 votes
1 answer
4k views

Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
Tiger Hwang's user avatar
10 votes
2 answers
292 views

Embedded C# bitpacked arrays to low-level STM32F4 driver for GE G35 RGB LED Christmas tree light

I'm going to be opensourcing some code I'm working on. I don't need help with the code, I just want to make sure my code is readable and my comments make sense. I have a knack for the esoteric. ...
Michael Rice's user avatar
10 votes
2 answers
724 views

Lossy packing 32 bit integer to 16 bit

I am working on the lossy 16 bit representation for 32 bit integers that catches all 32 bit range with precision depending on absolute value. My idea is to store integer ...
Vasaka's user avatar
  • 201
10 votes
2 answers
225 views

Simulation of image transfer over noisy channel

Following this question, I've tried to rewrite some core methods to avoid using String for bit operations, and I ended up using ...
StepTNT's user avatar
  • 385
10 votes
2 answers
7k views

C++ Bit shift whole array (left)

For an embedded project I want to shift a (byte) array left by a certain amount of bits. I've built this template function to do so: ...
Stefan's user avatar
  • 813
9 votes
2 answers
462 views

Proper Case Conversion (Performance)

I wrote a simple function that converts an array of words into proper case (using bitwise operations) and returns the array. I performed a simple test using ...
user avatar
9 votes
3 answers
17k views

Integer square root in x86 assembly (NASM)

This program calculates the square root of an unsigned 32-bit number with some bit fiddling to get a very close approximation, then one iteration of the Babylonian method to get an exact value. I don'...
Jintonation's user avatar
9 votes
1 answer
1k views

Parsing a sequence of bits out of a bit field

For homework, I had to parse CPUID instruction output in C, which required a lot of specific bit-manipulation like: (eax & CACHE_LEVEL) >> 5 I ...
Carcigenicate's user avatar
9 votes
2 answers
1k views

Gray codes addition — Take 2

For a while, I have tried to find ways (known or new) to do stuff with Gray codes. As some of you probably know, I already tried to implement an algorithm to add two Gray codes without having to ...
Morwenn's user avatar
  • 20k
9 votes
1 answer
646 views

Optimizing multiplication of two polynomials in GF(2^32)

I have the following method for multiplying two polynomials in GF(232): ...
Gideon's user avatar
  • 245

1
2 3 4 5
7