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
52
votes
8answers
34k views

Efficiently checking if a number is divisible by 3

I'm looking for code review, best practices and optimizations. ...
5
votes
2answers
7k 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 ...
5
votes
4answers
15k 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 ...
3
votes
1answer
75 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 ...
2
votes
2answers
104 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 ...
11
votes
3answers
7k 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 ...
7
votes
1answer
1k views

Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
12
votes
1answer
229 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 ...
24
votes
3answers
873 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 ...
24
votes
3answers
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 ...
10
votes
2answers
200 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 ...
9
votes
2answers
591 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 ...
8
votes
2answers
370 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) ...
7
votes
3answers
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 ...
6
votes
5answers
344 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 ...
5
votes
2answers
416 views

Packing and unpacking bits (2)

Part 1 here. I improved my code from my previous question (linked above). ...
5
votes
3answers
18k 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 ...
10
votes
3answers
344 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 ...
10
votes
4answers
4k 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 ...
6
votes
2answers
2k 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? ...
6
votes
3answers
2k 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 ...
5
votes
2answers
2k 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? ...
4
votes
0answers
78 views

Bit Vector implementation

Looking for a general review. ...
4
votes
2answers
141 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 ...
3
votes
2answers
476 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? ...