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

learn more… | top users | synonyms (1)

7
votes
3answers
68 views

Check if bit is only set once in a vector of int

I have a vector of uint16_t and I want to check if there is a bit which is only set in one vector. I then get its position within the vector as well as from the ...
2
votes
1answer
86 views

Why are these functions slower than BigInteger's included methods?

I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
3
votes
1answer
78 views

Finding the 'Minimum' AND of all the possible subsets of a set

The subset size should be greater than or equal to 2. How can I speed up this code? ...
15
votes
3answers
1k 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 ...
3
votes
2answers
208 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? ...
4
votes
2answers
55 views

Is there a more optimal way to approach some of these bitwise functions?

I completed some bit manipulation exercises out of a textbook recently and have grasped onto some of the core ideas behind manipulating bits firmly. My main concern with making this post is for ...
5
votes
1answer
80 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 ...
5
votes
2answers
68 views

Can my implementations of rotate, shift, XOR, NOT and AND be improved to function more efficiently?

I am going through the example of SHA256 over at wikipedia and wanted to make sure I understood all the operations of the manipulation math before trying to actually attempt the implementation itself. ...
0
votes
4answers
59 views

Arithmetic or logical right shifts

Is this code correct? Is there any major improvement I can ad in terms of number of operations? ...
20
votes
8answers
7k views

Check if a number is divisible by 3, efficiently

Efficient way to check if a number is multiple of three. Looking for code review, best practices and optimizations. ...
2
votes
2answers
89 views

Fastest / most efficient random bit indexing in Python

Using Python/Cython, I am trying to construct an efficient way to index boolean values. I have been trying to use the bitarray and/or bitstring packages to construct a fast, non memory intensive way ...
4
votes
1answer
116 views

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

I have the following method for multiplying two polynomials in GF(232): ...
8
votes
3answers
210 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 ...
7
votes
3answers
373 views

Bit exchange operation

I have been working on a certain task these days and after several hours of torture, I have done it! However, I believe the code that I have "crafted" is quite difficult to be understood and to be ...
4
votes
2answers
315 views

Bit packing and unpacking

I needed a variadic function to pack and unpack bits into integer types. This is my first attempt: ...
5
votes
2answers
69 views

Decomposing bitfield with Haskell: is recursion the only way to do this?

Here's a program to take a bitfield like 7 and decompose it into the flags [1,2,4]. Ignore that the algorithm for doing this doesn't use bit shifting/is stupid. Problems: it seems like I have to have ...
5
votes
1answer
55 views

Eliminating repetitiveness in code to test hash functions

I usually take my school code as a playground to mess around with things. I was given a bunch of premade hash functions and had to test their output and find when they reach a specific lower range. ...
14
votes
5answers
963 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 ...
4
votes
1answer
99 views

Efficiently counting '1' bits in the first n bits

I have two functions to count the number of set '1' bits in the first offset bits of either a 64 bit number, or a bitmap struct. After some profiling, particularly ...
7
votes
4answers
532 views

Boolean expression complexity and bitwise operators

Given this kind of code: ...
13
votes
4answers
2k views

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

Is there a better way of doing this? ...
3
votes
2answers
133 views

Function for reading bits

I've made this simple function for reading N bits from an unsigned char* (with a possible offset) and I'm willing to share it here, get a review, suggestions for ...
0
votes
0answers
36 views

Could this ROM reader be faster and/or smaller?

I've been practicing arduino programming for a few weeks now having become confident in reading and writing to pins so I decided to take it a step further and do a serious project that does both, so ...
2
votes
2answers
116 views

Copy and bit-invert ints in an std::vector

I want a second copy of an std::vector in which all elements are inverted, e.g. 0x01 in the first vector should be ...
2
votes
1answer
138 views

Take all the bits in a number and shift them to the left end

I wrote this program which takes all the bits in a number and shifts them to the left end (for example: 01010110 --> 11110000). It's and exercise from a book. It works, but it seems to be very ...
5
votes
3answers
1k 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 ...
8
votes
1answer
166 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? ...
5
votes
1answer
134 views

Playing with bits using Java

How can I optimize the following class? prepareSharing packs bits on left and prepareMask counts the required bits for encoding ...
1
vote
1answer
256 views

Missing element from array (D&C) - code design/aesthetic improvement

I am trying to implement in C/C++ a 'classical' Divide and Conquer algorithm which solves the following problem "Given an array of n-1 numbers (int) from 0 to n, find the missing number". I am using ...
0
votes
1answer
109 views

Bitwise Logic optimization

I should be able to figure this out on my own, but I can't seem to wrap my head around how to optimize this set of logic for interpreting the existence of URL parameters. Below is my code: ...
1
vote
1answer
106 views

Color fading function

I found this old color fading function in my snippets folder and would like to implement it to one of my projects. It can be used to fade one color to another. It's a very long one-liner: ...
4
votes
2answers
732 views

Speed up a program to find the minimum Hamming distance

I have a solution to the puzzle at https://kattis.csc.kth.se/problem?id=codes. What can I do to make my algorithm much much faster? What the algorithm does is take an N×K matrix and search for the ...
2
votes
5answers
1k views

Divide group of numbers into two groups of which the sums are equal

I have a piece of code that divides a group of numbers into two groups of numbers of which the sums are equal. I created this because of a Stack Overflow question: For example: {10,15,20,5} will ...
2
votes
1answer
284 views

JavaScript xor function

I made simple JavaScript xor function. It accepts string, numeric array and mixed array (char, string, num) as params. It returns string or an array. Returning an array is a must!. All numbers are ...
2
votes
1answer
129 views

Simplify bit flags checking code

Please help make this code more cleaner. It's a part of window procedure that notifies my renderer before client area of window resized. I need to check two combinations of bit blags: some of them ...
4
votes
1answer
621 views

Bitwise Flag code for Python

I am looking for feedback on this compact Python API for defining bitwise flags and setting/manipulating them. Under the hood, FlagType extends int and all ...
-2
votes
1answer
55 views

What are these bitwise operations doing? [closed]

I found a snippet of code on the PHP manual which packs 64 bit integers. APparently this is necessary because pack() always treats numbers as 32 bit integers even ...
0
votes
2answers
1k views

Spotify's “Reversed Binary Numbers” Problem [closed]

I wrote some Java code for Spotify's Reversed Binary Numbers puzzle and I tested it on my machine and I am almost positive that it behaves as expected. However, when I send it to puzzle AT spotify.com ...
3
votes
1answer
348 views

Connect Four: Bitboard checking algorithm

I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning. The code below is a Clojure implementation of this bitboard algorithm. The whole code can be found here: ...
2
votes
3answers
2k views

Base-36 encoding of a byte array

After posting a question about Alphanumeric Hash generation on StackOverflow, the most helpful answer was to change the conversion method from pulling 5-bit chunks of a binary hash value, to instead ...
1
vote
1answer
133 views

What specific optimizations can be made to this BitArray class, and how can they be implemented?

Here is the current code for the BitArray class needing optimization (implemented on big integers): ...
3
votes
1answer
539 views

speeding up an algorithm that find patterns in a growing collection

I want to find a way to speed up this code. if you look at the condition of If calculated Then in the code below, this is what slowing down the code. While the ...
5
votes
3answers
796 views

How to manage a bit array with C programming langage?

I'm training a bit with C, and I'm trying to build a bit array. I would like to have some comments about my code, because I'm not sure that it's the best way to do it. ...
4
votes
5answers
662 views

Counting the number of bits of a positive integer

How can I improve this code for counting the number of bits of a positive integer n in Python? ...
3
votes
1answer
382 views

BitVector Class: Performance

Lately, I've been reading up on the Common Language Infrastructure's data formats. As a result, one thing it mentioned is some of the data is stored in a BitVector. I hadn't ever used a BitVector ...
5
votes
1answer
371 views

Jon Bentley's bitmap sort implementation

I've implemented Jon Bentley's bitmap sorting algorithm from Column 1 of his Programming Pearls book. This code supports user-defined integer range (works from ...
4
votes
3answers
848 views

Bit rotations exercise

I'm studying C on K&R and I solved exercise 2.08: Write a function rightrot(x,n) that returns the value of the integer x ...
42
votes
8answers
9k 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 ...
3
votes
1answer
242 views

Print Bits Part III

The last question I have been playing around with is a right rotate. I think it is also called a barrel roll. Please post if you have a better solution please post. I am still learning the art of bit ...
7
votes
2answers
151 views

Print Bits Part II

Could you suggest ways of making this more simple? ...