# 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.

268 questions
Filter by
Sorted by
Tagged with
1k 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 ...
856 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 ...
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 ...
3k 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. ...
153 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 to ...
103 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. ...
2k 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 ...
165 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 that ...
646 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. ...
4k 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 ...
2k 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 ...
365 views

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

I challenged myself to implement multiplication of signed integers using only +, -, >>,...
385 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 ...
944 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 ...
635 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 ...
149 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 ...
529 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 ...
343 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 ...
580 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 ...
856 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 ...
143 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 ...
104 views

C code: ...
5k views

### Byte-to-binary conversion function

My implementation of a binary-to-bits converting function: ...
1k 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 "...
1k 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 ...
9k 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 ...
1k 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 ...
511 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: ...
3k 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: <...
14k 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. ...
1k 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 ...
144 views

### Portable bitfields code generator [closed]

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 ...
14k 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 ...
2k 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 ...
53 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. ...
263 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? ...
8k 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. ...
328 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. ...
862 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 ...
653 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 ...
278 views

### Embedded C# bitpacked arrays to low-level STM32F4 driver for GE G35 RGB LED Christmas tree light

I'm going to be opensourcing some code I'm working on. I don't need help with the code, I just want to make sure my code is readable and my comments make sense. I have a knack for the esoteric. ...
3k views

### Mostly portable 128 by 64 bit division

I wrote this out of curiosity. It is based on the theory behind Knuth's Algorithm D and is intended to emulate the behavior of the x86 div instruction (though the ...
180 views

### Saving bytes instead of objects to write smaller files

This question is about reducing the size of files saved to disk in an infinite world 2D mining game. I finally added code to my game to save and load distant chunks instead of keeping all of them ...
1k views

### Count bits that differ between integers X and Y

I'm trying to solve the following problem : You are given two integers, X and Y. Calculate the number of bits that will be changed in order to convert integer X to integer Y. Here is my solution ...
1k views

### Logical shift with signed numbers

I am going through the Coursera course The Hardware/Software Interface and because it has already ended, I would not be able to get any feedback on the code. The assignment is: LogicalShift: ...
414 views

### Lossy packing 32 bit integer to 16 bit

I am working on the lossy 16 bit representation for 32 bit integers that catches all 32 bit range with precision depending on absolute value. My idea is to store integer ...
2k views

### Steganography for images

For a school project I'm working on a small library to do steganography. I've just finished the first method of steganography and thought I'd get some feedback. This steganography method works by ...
359 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) ...
226 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 ...
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 ...