# Tagged Questions

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

84 views

### Java conversion of base 16 to base 64

I wrote a solution (below) in Java to solve the problem here. I use BigInteger to convert first from base 16 to base 10, then base 10 to base 64. This seems quite inefficient to me, and I would ...
53 views

### Remove elements existing in a list built from an enum

The objective of this code is to make the difference inbetween two lists of items, that is knowing which items are missing in the second list in regards to the first one. The first list is built from ...
39 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 ...
106 views

### All arithmetic operator functions (+, -, *, /, %) coded only using bitwise operators in C

Here's all the functions in C that does all the arithmetic operators without using arithmetic operators themselves, mostly using bitwise operators. Recursive functions and comparison operators are ...
44 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 ...
33 views

### PWM wave generation

This code takes a integer x of range [0,127] as input, returns an array of four 32-bit integers, with x bits set. I'm trying to ...
61 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 ...
77 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 ...
238 views

### Finding nearest power of 2

I have the following simple program which calculates and returns the nearest power of 2 of a given number. Because I use increments and decrements to find the closest power, this takes a very long ...
76 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 ...
61 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 ...
53 views

### “Flipping bits” Python implementation

Problem Statement You will be given a list of 32 bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. ...
71 views

### Return Y integer with same bit sets as X & having | X - Y | minimum value

I happened to come across one question: Given an integer 'x' (Only positive), write an algorithm that returns integer 'y' such that it has the same number of bits set as 'x' and is not equal ...
59 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. ...
74 views

### Reading bits from a stream from least to most significant

For a recent project I was in need of excessive bit manipulation. Specifically, I had to write to or read from a variable number of bits up from the least significant bit towards the most significant ...
111 views

### Performing add/delete/count operations on a list

Problem Statement (HackerRank) You have a list of integers, initially the list is empty. You have to process Q operations of three kinds: add s: Add integer s to your list, note ...
563 views

### Division without multiplication, addition or division

In context of preparing for a coding interview, I gave the division problem a go using Java. This is what I came up with. It's a little bit different to other solutions I've come across the interweb. ...
1k views

### String reversal, capitalize vowels, lowercase consonants

This was a portion of a skills test given to me by a company. The task was to write a method that takes a string, reverses it, and sets the vowels as capitals and consonants to lowercase. I was also ...
237 views

### Number of right cyclic shifts to give the biggest number

I am preparing to coding interview and I met this task today: You are given 30-bit unsigned integer N. A right cyclic shift of N by K bits is the result of performing a right cyclic shift of N by one ...
304 views

### Mockup in C of hardware implementation for multiplication of signed integers

I challenged myself to implement multiplication of signed integers using only +, -, ...
56 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 ...
169 views

### Pixel bit-wise LSB extraction to byte array

I wrote the following code below to extract the least significant bit (LSB) of each pixel and store it into a byte[] array. I was wondering if Java has a more ...
155 views

### Verifying validity of Intel.hex file records

I have a class called FirmwareFile which represents and Intel HEX format file. The file is constructed with the name, build time and build date, at a later point ...
135 views

### Printing lines in a file, filtering by the first column

I am setting up a project which involves a lot of data analysis, each data point represented by a line in a large text file. In order to test for certain types of data, I added a tag to each line (It ...
98 views

I have a program which reads and analyses MPEG-TS files. I've been using gperftools to identify hotspots and optimize them, and 45% of all samples at this point are in my main bitstream reader ...
140 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 ...
203 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 ...
179 views

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 ...
90 views

### Given bit strings, perform a bitwise addition

I'm looking for code-review, optimizations and best practices. This code is a Java adaptation of code on geeksforgeeks.org. Please verify if space complexity is $O(n)$ where n is number of bits, and ...
74 views

C code: ...
375 views

### Byte-to-binary conversion function

My implementation of a binary-to-bits converting function: ...
303 views

### Calculating holes on a Tetris grid quickly

I have created a Tetris AI which can play Tetris on its own given a set of rules. It places the next piece in every possible way on the board, and calculates which position that gives the fewest ...
175 views

### Network byte order to host byte order

While doing a little project involving sniffing packets, I found myself programming a cross platform network to host byte order and host to network byte order, this is my first time developing a ...
1k views

### Packing and unpacking two 32-bit integers into an unsigned 64-bit integer

Does this look good for combining/retrieving two 32 bit integers (a type and an index) into/from an unsigned 64 bit integer (a unique ID)? The point is to hide in the API the composition of the ...
377 views

### Optimizing code to find maximum XOR in Java

This problem is from HackerRank (not a competition, just for practice) Basically what you do is take in two integers as a range and then finding the maximum XOR from a pair of integers in that ...
503 views

### An Army of Ones

I've been practicing using recursion lately and thought this was a case where it would prove really useful, is this good use or is there some superior non-recursive way to go about it? Challenge: ...
380 views

### Bitwise operators for permissions

I'm trying to create a read-write-execute permissions system for items using user-group-world system. I already have the user/group system set up, just not the permissions. This is what I wrote: ...
1k views

### XOR of a list of a numbers

In preparing a solution for the CodeChef "Will India Win" challenge, I am trying to find out the xor of list of numbers. The first line of input contains the number of test cases, 1 ≤ T ≤ 106. ...
141 views

### Copy arbitrary number of bits at arbitrary offset from buffer to another buffer

I have just written a function copy_lowbits_off to copy any number of bits (not bytes) from a source buffer to a destination buffer. The function also supports ...
86 views

### Portable bitfields code generator

I am working on a project that involves quite a lot of instantiated objects, with tons of properties, most of which have only a few possible values, so the prospect of saving on memory is quite ...
3k views

### Reversed Binary Numbers

Here is my code for "Reverse Binary Numbers" in C. Problem Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and ...
859 views

### Changing element visibility based on multiple checkbox states

I am writing a script in JavaScript & HTML and have some optimization problems. In the for, loop I check a variable named ...
29 views

### Bit stream input in Factor

My attempt at writing code to input values of arbitrary bitlength based on a bytearray in Factor. Any comments on the correctness or style would be appreciated. ...
128 views

### Speed optimization for transparent gradient blend algorithm

The following algorithm works correctly for the purposes that I need, which is to set the top side of an image with a transparent gradient blend. Any tips to optimize it for speed? ...
2k views

### Binary to decimal number converter

Here I am doing some exercises for my studies with Python. This is a binary to decimal number converter (currently restricted to that). These are the very first steps, so there is no error handling. ...
253 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. ...
511 views

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 ...
145 views

### Implementing binary output to a file in C++11

I would like to have a class which supports outputting bits into a file. This is what I came up with but I fear there are many poor decisions. Should I inherit from a ...