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
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
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
6 votes
3 answers
9k views

Printing longest sequence of zeroes

I am doing a coding exercise in codility and I came across this question: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ...
would_like_to_be_anon's user avatar
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
3 votes
1 answer
87 views

Testing for divisibility of numbers - follow-up

(Here is the follow up question) I had another go at trying to speed up my program described in my previous question. JS1's answer was particulary helpful and now my code is only about 20% slower ...
Linus's user avatar
  • 917
2 votes
2 answers
138 views

Program to test for divisibility of numbers

(EDIT: here is the follow up qusetion) Using this program to test for the smallest number where its permutations or itself is divisible by 10 or less numbers is twice as slow as the fastest program I ...
Linus's user avatar
  • 917
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
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
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
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
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
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
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
8 votes
3 answers
2k views

Custom integer class for calculating crypto algorithms

How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that ...
calccrypto's user avatar
8 votes
2 answers
542 views

Array-like container for uints shorter than 8 bits (Rev 1)

This is a follow-up to Array-like container for uints shorter than 8 bits In short: The PackedBitfieldArray should be a kind of container for packed (unsigned) ...
Christoph's user avatar
  • 507
7 votes
3 answers
3k views

Finding the fastest common prefix of 2 strings in Python

I'm comparing my algorithms, slice_prefix and bit_prefix, with existing ones to find the common prefix length of 2 strings as ...
12345678910111213's user avatar
6 votes
2 answers
3k views

Hamming distance between numbers in JavaScript

In Leetcode it states that my runtime is only faster than 38% all of submitted JavaScript solutions. Is there anything I can change to make it more efficient? ...
Rohit Katyal's user avatar
6 votes
2 answers
3k views

Packing and unpacking bits

Part 2 here. I wrote this class for packing data. Would it benefit from being named BitPacker rather than BitStream (and change write/read to pack/unpack), or it works as is? How can I improve it? ...
Tim's user avatar
  • 115
6 votes
5 answers
587 views

Rotating 3D bit board in C

I've got a 64 bit bitboard, but instead of interpreting as an 8 by 8 binary matrix, as is usually the case, I've taken it to the third dimension, so to speak. Now mirroring and even rotating by 180 ...
Zacariaz's user avatar
  • 373
5 votes
3 answers
22k views

Split a long integer into eight 4-bit values

It's an exercise from a book. There is a given long integer that I should convert into 8 4-bit values. The exercise is not very clear for me, but I think I understood the task correctly. I googled ...
mitya221's user avatar
  • 793
5 votes
2 answers
660 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 ...
Lance's user avatar
  • 351
5 votes
2 answers
675 views

Packing and unpacking bits (2)

Part 1 here. I improved my code from my previous question (linked above). ...
Tim's user avatar
  • 115
4 votes
2 answers
467 views

C bit mask calculator written using GTK+ 3

I've been studying C on my own using K&R 2nd Edition, and started exploring GTK+ to practice some of the concepts I've learned by making GUI applications. This is the first GUI application I've ...
Faraz's user avatar
  • 513
4 votes
0 answers
90 views

Bit Vector implementation

Looking for a general review. ...
MAG's user avatar
  • 2,934
4 votes
2 answers
3k views

Calculate XOR of many consecutive and non-consecutive numbers

question: answer(start,length) #start is the starting number of the matrix #length is number of rows. example 1: answer(0,3) ...
Tetali Anirudh's user avatar
3 votes
2 answers
540 views

Function for splitting an integer into smaller values

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way? ...
Jamal's user avatar
  • 34.9k
1 vote
1 answer
153 views

Bit vector in Java supporting O(1) rank() and O(log n) select()

Introduction I have this GitHub repository (version 1.0.0.). It implements a rank(i) operation in \$\Theta(1)\$ time, and ...
coderodde's user avatar
  • 28.5k
1 vote
1 answer
98 views

Bit vector in Java supporting O(1) rank() and O(log n) select() - follow-up

(This post is the continuation of Bit vector in Java supporting O(1) rank() and O(log n) select(). It resides here (version 1.0.1).) Basically, I have implemented everything harold suggested, except ...
coderodde's user avatar
  • 28.5k