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
3
votes
1answer
776 views

Finding patterns in a growing collection

If you look at the condition of If calculated Then in the code below, this is what slowing down the code. While the code provided seem fast with ...
3
votes
1answer
116 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 ...
3
votes
1answer
66 views

Set a specific bit to a specific value [closed]

This is the code I came up with: ...
3
votes
1answer
101 views

K&R Exercise 2-6 : Inserting part of a bitset into another

"Inserting" part of a bitset into another This is Exercise 2-6 on the K&R book, 2nd edition. The reader is asked to: Write a function setbits(x,p,n,y) that returns x with the n bits that begin ...
3
votes
1answer
107 views

Gradient noise class based on Simplex noise

Here's a seedable 2D and 3D gradient noise class in C# that I wrote based on Simplex noise. Seems to work properly (even though it's highly experimental). I'm looking for tips to improve the quality ...
3
votes
1answer
2k views

Codility binary gap solution

This is how I solved the binary gap problem: Find longest sequence of zeros, bounded by ones, in binary representation of an integer I wonder how does it fare against solutions such as ones appeared ...
3
votes
1answer
107 views

Code Chef problem COOKMACH

Firstly, I got the solution for this question in CodeChef, but I noticed one thing which still confuses me. This was the code which was accepted as correct. ...
3
votes
1answer
404 views

Encoding variable-length quanties for MIDI

I was reading about the MIDI spec and I challenged myself to implement a function to convert an int32 to a variable length quantity uint32. I didn't look it up or ...
3
votes
0answers
99 views

Java code to convert BigDecimal to/from .NET decimal

Background In many languages we have built-in data types for representing decimal floating point numbers. In .NET that's decimal, and in Java we have ...
3
votes
0answers
2k views

Calculating CRC using arbitrary polynomials

This is my implementation of calculating CRCs which supports arbitrary polynomials specified in different representations (according to the explanations given here and here). My concerns are: I don'...
2
votes
2answers
212 views

Color fading function [closed]

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: ...
2
votes
4answers
71 views

Assembler-program which reverses the content of EAX

Following exercise: "Write a program that takes a number (of size 4 bytes) x as input, and then reverses all the bits of x, and outputs the result. By reversing all bits we mean that the bit ...
2
votes
1answer
470 views

Xoring in constant time in Java

I was inspired by this answer and the explanation and decided to support negative numbers. While testing, I noticed that the solution didn't handle the case with one number in the given range. A quick ...
2
votes
2answers
939 views

Simple Binary Adder

The program adds two binary numbers in two' s complement ignoring the overflow for a constant size of bits. I am trying to generalize the program by taking input from the user in a single line without ...
2
votes
2answers
585 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
3answers
155 views

Testing if the binary representation of an integer has an odd number of ones

The fn odd_ones(x: u32) -> bool function should return true when x contains a odd number ...
2
votes
1answer
365 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 ...
2
votes
1answer
2k views

Hackerrank “Strings: Making Anagrams” Javascript Solution

This is the original problem : Input Format The first line contains a single string, a. The second line contains a single string, b. Constraints 1<= |a|,|b| <= 10^4 It ...
2
votes
2answers
2k 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 ...
2
votes
1answer
32 views

Indeterminate-length big-endian to little-endian conversion in Python

I have a need to convert wire encoding (big-endian) to little-endian in Python. The conversion needs to happen as a separate function; I am not allowed to change the function that reads the byte ...
2
votes
2answers
308 views

Hamming distance

I am wondering how I can improve the runtime of this code. I was solving this task at leetcode, and came up with this solution. But I got the result that my runtime is 88 ms, which beats a bit more ...
2
votes
1answer
105 views

Pseudo-random number generator port from C

C code: ...
2
votes
1answer
2k 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
2answers
1k views

Algorithm with byte shifts and BCD calculation

I'm trying to develop an algorithm that generates a 16-digit validation number to interact with a given system. This is how the algorithm should be implemented: I have implemented it, and have it ...
2
votes
2answers
644 views

Space and time efficient Algorithm for generating prime numbers

I have developed an algorithm for generating prime numbers (which turns out to be an optimized version of the Sieve of Eratosthenes) by reducing the space by a factor of 64. ...
2
votes
1answer
64 views

Three ways to read a number for a BitInputStream

I'm trying to learn how to write more readable code without introducing easily avoidable performance killers. This is part of a BitInputStream Java class I'm ...
2
votes
2answers
105 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 ...
2
votes
1answer
173 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 ...
2
votes
1answer
25 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 ...
2
votes
1answer
87 views

C++ class for a flag register in a Chip8 emulator

In order to learn C++ I decided to code a Chip8 emulator following a tutorial. When I came across the idea of a flag register and decided it would be fun to implement in order to get familiar with ...
2
votes
1answer
211 views

Reading/writing single bits from/to FILE

I'm implementing a Huffman encoding/decoding program and I need a way to deal with single bits. I tried to keep things as simple as possible, I "pack" bits inside a single byte and I have only one ...
2
votes
2answers
329 views

Computing pairwise bitwise OR in a list of bit-strings

I am trying to solve this problem using Clojure: You are given a list of \$N\$ people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. ...
2
votes
0answers
77 views

Kaitai Struct: shift rotate >1 byte groups

This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this ...
2
votes
0answers
224 views

Binary addition in VB.net

It starts by taking two user input from the console and putting them into strings. I then pad them to get them to the same length. I get the length of the num1 and assign it to the integer len. I then ...
2
votes
0answers
611 views

Hackerrank: Max Score

Problem statement Consider an n-element sequence of integers, A = {a0, a0, ..., an-1}. We want to perform \$n\$ operations on \$A\$, where each operation is defined by the following sequence of ...
2
votes
0answers
74 views

Convert tri-gram of type wchar_t to an integer type

I am trying to convert a tri-gram containing 3 characters of type wchar_t to an integer type (an uint64_t at the present time). ...
2
votes
1answer
119 views

A bitwise common prefix algorithm in Python

Is my algorithm slow because it has problems? Or, is any bitwise common prefix solution not going to give me the performance I'm looking for? After profiling my algorithm, I found that over 60% of ...
2
votes
1answer
130 views

Routine for convert decimal digits to Densely Packed Decimal

I've written an amd64 assembly routine (in gas syntax using the System V calling convention) to convert three decimal digits to Densely packed decimal because I wasn't satisfied by the performance of ...
2
votes
0answers
884 views

Read bits, write bits data to arbitrary position in char array

I created functions to read bit data from arbitrary bit position of a char array to long long int, and I also created a function ...
1
vote
2answers
58 views

The program that reverses bytes in two distinct ways (in C++)

Will be grateful if anyone would reasonably comment on my code. I am interested in the style only. If there will be any comments which are not related to the style I am glad to read them as well. <...
1
vote
1answer
2k views

Bitmask type to perform binary operations

Without any specific implementation details, how would you create a bitmask type that wraps binary arithmetic into something easy to read? Ideally, this would be used with an ...
1
vote
2answers
2k views

Reversing the bits of a 32-bit integer

Here is my code: ...
1
vote
2answers
246 views

Musical graph algorithm, with an inlined lookup helper function

I am working with bit strings in clojure. My question is less about performance and more about how one should go about it (in particular see the last inlined version) The tonnetz-array represents an ...
1
vote
1answer
1k views

Decide if a number is even or odd by using bitwise operator

The following exercise is from an Assembler-course I'm taking: Write a program that takes a number x as input, and returns: 0 if x is even. 1 if x is odd. Full exercise-page here: ...
1
vote
3answers
665 views

Byte array to floating point such as FPE2 in Java

Apple's System Management Controller uses FPE2 and SP78 as floating-point formats. While writing a Java library that that interfaces with the SMC, I ported someone's C function to convert read a FPE2-...
1
vote
2answers
99 views

Find out whether number is of Power of Two

The task is taken from leetcode Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: ...
1
vote
1answer
65 views

Maximise XOR of two elements

I have a number x and I need to choose another number y from the given range [l, r] such ...
1
vote
1answer
447 views

K&R 2.6 Exercise setbits(x,p,n,y) function

Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position <...
1
vote
1answer
1k 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 ...
1
vote
1answer
96 views

Using JavaScript-bitwise AND and -bitwise NOT for setting a number to 0

Obviously is the XOR-operator is used in low-level programming-languages used a lot for setting a value back to all bits 0. For example: 11010 ^ 11010 => 00000 Brought my teacher to figuring a out ...