Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take 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.

320 questions
Filter by
Sorted by
Tagged with
105 views

### Counting initial bits of 0 in Java

I want to count the number of bits set to 0 at the beginning of a byte array. So far the code I have is this: ...
210 views

### Convert a c# decimal to big-endian byte array

Following the avro schema documentation for decimals I've created a method to turn a decimal into a byte array. The goals are: Should be represented by a non-scaled integer Should be big-endian I've ...
• 155
1 vote
73 views

### C++ Chess Engine - (Magic)Bitboard-based move generation

I am currently writing a bitboard-based Chess Engine in C++, and I recently finished the move generation. I am using move-lookup-tables that get pre-calculated at application startup for sliding and ...
• 390
962 views

...
• 1,353
113 views

### Calculate Hamming distance between integers with bitwise operations

I recently learned about the Hamming distance. The use-case I found it through was actually working with integers instead of strings, yet its implementation was using string comparison. I was pretty ...
• 843
1 vote
114 views

### Permuting BitSet in Java

I have this tool for permuting bits in BitSets: com.stackexchange.codereview.util.BitPermutation.java: ...
• 25.3k
161 views

### (Google Foobar XOR Checksum Challenge) How do i optimize this function to run for much larger values?

I wrote a function that accepts 2 integers start and length as input and returns the bitwise XOR of certain numbers as described below: ...
141 views

...
223 views

### Separating odd numbers and even numbers into two vectors in C++

I'm learning C++, and would like some guidance. I'm pretty sure I'm doing something wrong. This algorithm runs in O(n) time and uses O(n) external space. Could I make a more efficient algorithm for ...
• 13
61 views

### LetterDict, a dictionary whose keys must be lowercase letters [a-z]

Looking for any suggestions - alternative design, improvements to readability, improvements to the comments, etc. I implemented this letter dictionary to see if it would faster than a regular ...
• 243
67 views

### Recursive function that gives the number of hashes present in a Merkle Mountain Range proof

I have written a function that gives the number of hashes present in a Merkle Mountain Range proof. Using Solidity, this function can be written as: ...
67 views

### Transformations on a game board represented as a bitset

I'm using a long as a bitset to represent a game board. If a field is set (X) the corresponding bit is set, if it's empty (...
• 141
1 vote
43 views

### Counting Cram game positions in C

This code counts the number of possible positions in the game of Cram on a mxn board. EDIT 1: In the original question I mistakenly mentioned that this number is ...
• 141
52 views

### UPDATEIT SPOJ problem using Segment tree

UPDATEIT on SPOJ: UPDATEIT - Update the array ! #bitmasks You have an array containing n elements initially all 0. You need to do a number of update operations on it. In each update you specify l, r ...
• 101
67 views

### Barebones DNS Client in Rust

I'm starting to learn about networking and as a project I am building a really simple DNS client in Rust. Functionality to start was just to take a command line domain argument and sending a DNS A ...
396 views

### Representing Tic-Tac-Toe using a binary BitBoard

So I have been learning more about binary numbers lately, particularly in related to describing a gamestate. While perhaps chess is the most natural candidate I wanted to start of with something ...
• 5,493
93 views

### Move an unaligned bit-addressed range into another bitmap

Context I have an array of maximum-fixed-size, size, containers of items; these items are either pointers to objects or an index of the same array, (but no loops.) ...
• 1,015
72 views

### Generate mask for non zero (or inverse) nibbles in integer

Apologies for the cryptic title, but the problem really is rather simple. I have a 64 bit integer which I interpret as 16 4 bit nibbles, or a nibble array if you wish, and I needed generate a mask ...
• 353
73 views

### How to refactor a method with many prameters that sets a bit array

I've written a method that is going to set a bit array by passing bool parameters to be able send correct command ...
• 21
1 vote
89 views

### Generating Unique Subsets using Bitmasking

Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Link to the judge: LINK Note: Each subset should be sorted. Example 1: ...
61 views

### Invert n bits beginning at position p of an unsigned integer

In an attempt to create a function that returns x with the n bits that begin at position p ...
1 vote
192 views

### Determine whether bitwise-OR of sub-array is odd

Problem: Given an array A of N integers, you will be asked Q queries. Each query consists of two integers L and R. For each query, find whether bitwise OR of all the array elements between indices L ...
263 views

### BitVector64 implementation

I have implemented a structure to handle BitVector64 (default System library support only 32 bits). In the implementation i have added some custom method to manipulate bits inside the vector. I would ...
• 163
117 views

### Generically convert bytes to integers

I wanted to know If there was a way to make it more versatile or shorter/simpler. Here's the code: ...
560 views

### Random alphanumeric string generator function (C, Windows, bcrypt.h)

The function uses the Windows "Cryptography API: Next Generation" (bcrypt.h)'s BCryptGenRandom function to generate ...
• 31
75 views

### Bitarr class optimization C++

I have implemented a bitarr class that does bit manipulations. I would love to know if there is any way to make the code more optimized. Any input would be much appreciated. Thank you! Note: int main()...
• 369
319 views

### Bit manipulator (reader / writer)

Please take a review of my simple bit manipulator: ...
• 75
524 views

### How to optimize bitwise get/set/clear of ranges of bits on 8-bit integers in JavaScript?

I have been working for a few days on writing get, set, and clear bitwise functions in ...
• 299
54 views

### Array-like data structure for fixed sized types with non-machine-word sizes

I am writing an array-like data structure for types other than 8 16 32 64 – the usual type sizes. Ideally, my interface is the following for addressing the array. ...
• 51
468 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 ...
• 16.1k
46 views

### Implementing a Hilbert-like space-filling-curve conversion in Rcpp

I have written a series of functions that are designed to create a space-filling curve (similar to a Hilbert Curve) that is intended for symmetric matrices (as opposed to a square region). I plan to ...
• 183
157 views

### Factoring the powers of 2 in Python

The powers of 2 are trivial to factor because all powers of 2 have one 1-bit and the rest 0-bits. Just iterate minus one 0-bit as follows: 10000,1000,100,10,1. And then convert the binary strings into ...
• 431
170 views

### Von Neumann random bytes debiasing

I need to create a method to debias biased bytes from the method get_raw_bytes(length) using von Neumann debiasing method. Von ...
106 views

### ROT128 byte-transformation encoding

I'd like some feedback on code I wrote to obfuscate text on-disk. The basic idea is the same as ROT13 (which is implemented in the Python standard library), except instead of rotating the English ...
1 vote
698 views

### LeetCode: Sum of Two Integers C#

https://leetcode.com/problems/sum-of-two-integers/ Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example 1: Input: a = 1, b = 2 Output: ...
• 5,249
131 views

### Swapping Nibbles

I have small (<250 bytes) chunk of binary data, where I know that almost always, the first nibble is going to be zero 0. For example, in hex: ...
• 215
412 views

### Bit shift 256 bits

I have a working solution to my problem, but I'm less than satisfied with the result, both when it comes to style and performance. In short, I need to be able to shift 256 bits of data, say an array ...
• 353
211 views

### Bitmap in Bitmap (Black & white)

Below is my attempt at bitmap in bitmap searching using 1bpp bitmaps. Any suggestions on how I can reduce complexity and improve speed? Any comment is appreciated. ...
• 75
54 views

### Basic Subnet Mask Calculator + A Little Extra

For my Networking class, we learned about subnetting and subnet masks. I decided to write a little "calculator" (a generous term) that when given n-many network ...
• 16.1k
281 views

### converting bits binary to bcd

I wrote a code that requests user input and then converts binary to bcd then back to binary using bitwise operations. While I think my code is good I want to know if there is a better way to write the ...
218 views

### Adding two n and m bit binary numbers

Recently I was studying bitwise operators and bit-manipulation algorithms and I found out an algorithm to add two binary numbers. Pseudocode is as follows: ...
• 221
213 views

### Interlock of some equipments in electrical power system using simple Arduino Uno code [closed]

I'm trying to implement some logic functions essential to control a small power system consisting of 2 Transformers labeled T1 and T2, 2 Generators (G1 and G2) and a bus tie (BT). I'm not going to ...
447 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 ...
768 views

### "Fast strlen" in C using scalar bithacks

I've translated an implementation of strlen in x86 assembly to C and added alignment checking: ...
• 1,705
1k views

### Decoding of binary data (AIS) from socket

I am currently working on a AIS message decoder written in pure Python. It's just a little project to teach me some things about binary data, decoding, etc. Because I am only programming for about a ...
• 43
274 views

### Byte extension methods for common bitwise operations

I'd like to learn how I can optimize the following byte extension methods. I'm targeting .NET Standard 2.0 but I'm curious if there are any ways to go about some of this functionality using features ...
• 133
5k views

### overload c++ enum class operators and enable check value set

I use c++ 11 enum class usually instead of the old enums because many enums can contain the same value but as known they don't have the bitwise operators out of the box so we should define them ...
• 293
112 views

### Invert bits of a number in clojurescript

Doing a challenge to print the number that you would get if you convert a given number to binary, invert its bits, then convert back to decimal. I decided to give it a run in clojurescript (which I ...
• 1,652
562 views

### Invert bits of binary representation of number

This is the code I came up with. I added comments to make the solution more verbose. ...
• 405
1 vote