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

280 questions
Filter by
Sorted by
Tagged with
202 views

### Basic binary number container

Summary: a templated class, whose main purpose is to store a binary representation of a decimal number. I choose array of booleans as a storage, since bitset was forbidden. One of the requirements was ...
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 ...
2k views

124 views

### Templated division by a power of 2

I've implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases when the developer know ...
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? ...
584 views

### Check if a given integer is power of two

Given an integer, check if it's the power of two. ...
7k views

### Bit packing and unpacking

I needed a variadic function to pack and unpack bits into integer types. This is my first attempt: ...
3k 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 ...
2k 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 ...
8k 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 ...
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 ...
628 views

### Bitwise operation to get the weekday name

The function days_name receive a val (string) corresponding to weekdays in hexadecimal. ...
2k views

### Managing a bit array

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

### Circular-shift with variable bitlength

For some memory masking operations I need a bitwise-rotation functionality for unsigned integral types. I came across solutions based on the wiki article, but have not found a variation for a function ...
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. ...
147 views

### Map a range of integers to a 64-bit integer

Given a 64-bit integer x (which is always larger than 0), we wish to map it to an index so that: when x is between 0..64, we map it to 0, when x is between 65..128,...
948 views

### Biggest Binary Gap Solution

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == ...
10k views

### Decoding binary coded decimal (BCD) value

I have 3 questions, all of them are concerned with binary coded decimal (BCD) conversion. My coding snippets are listed as follows. But I don't like my algorithm because it takes too long when dealing ...
170 views

### Speeding up the generation all bit strings with Hamming distance one

I'm playing around with hypercubes. For this, I need to create its edge set. Specifically, the edge set is the set of all bit strings with Hamming distance 1. An important property is that I want to ...
127 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. ...
364 views

Notes: supports 64-bit platforms only ulong used where negative values make no sense library functions are assumed to be correct (whether they actually do what I ...
431 views

### Packing and unpacking bits (2)

Part 1 here. I improved my code from my previous question (linked above). ...
663 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 ...
150 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 ...
3k views

### Gaussian blur - convolution algorithm

My program uses graphics convolution, an algorithm that processes pixels to make them round, soft and blurred. This particular algorithm is well-known, but its slowing down the operation of the whole ...
9k views

### 16 bit checksum calculation [closed]

I need to calculate checksum using 16-bit ones' complement addition: ...
771 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 ...
133 views

### pythonic way to split bytes

I have incoming data from an absolute rotary encoder. It is 24 bits wide. 12 bits are the number of turns and 12 bits are the angle. I'm reading the data with an Arduino and sending it to the ...
110 views

### C++ container — variable byte coded numbers list

I'm writing a container for unsigned integers, which uses variable byte coding. It works but some decisions I took look ugly so I would be glad if you gave me some hints for improving the design. I ...
9k views

### A simple assembly program to count the number of ones in a register

I made a simple assembly program to count the number of ones in a register. Since this is basically my first ever, I would like to hear what I can improve or if there are some major flaws in this one: ...
561 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 ...
79 views

### Search for the longest sequence of toggled consecutive bits in an integer

I have to do a function ​called findsaw. This function searches for the longest sequence of toggled consecutive bits in an integer and returns the index of the ...
144 views

### Test four int of commonality in the lower four bits

The four parameters a, b, c and d can be ...
15k 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 ...
650 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. ...
315 views

### BitShift code compacting

I'm new to Java programming and am really enjoying it. I made a bitshift program, but I think it's too long. I expect the program to: Let users input the number and degree to be shifted Specify if ...
109 views

### Count number of bits to convert one integer to another integer

Return the number of bits (N) that will need to be changed in order to convert an integer(X), into another integer(Y). Constraint: 0 <= X <= Y <= 2**128 Example: X = 1, Y = 2 => ...
112 views

### Checking if a number is a power of 2 without loops

I made a short program which checks if a number is a power of 2 without using any loops. The idea: A number which is a power of 2 must have only one bit "1" ( ex: 8= 1000, 4=100 and so on). Suppose ...
346 views

### K&R 2-7 - Invert a bitfield from a number

This is my solution to the Exercise 2-7 of K&R C book. The assignment is: Write a function invert(x,p,n) that returns x ...
3k views

I have been trying to learn more about using bits/bitfields/bitmask or whatever you call them exactly in PHP for different settings and permissions. With the help of others, I have come up with this ...
604 views

### Compute the XOR of all the elements in the upper left triangular of a square matrix

Given a dimension $D$. We generate a $D\times D$ matrix with elements that start from $0$. For example, if we're given a dimension $4$ this is what our matrix looks like:  \begin{bmatrix}...
153 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 ...
283 views

### Python binary calculator

My task was to build a calculator in Python supporting binary addition etc. To begin I define a couple of custom exceptions : ...
149 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 ...
1k views

### Associating a name with each bit in a bitmask

I have the following code: ...
583 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 ...
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: <...
72 views

### Check digital input signals that is sent as an integer using bitwise checking

I have a program that works with a hardware via an ActiveX API. The API have events that is triggered by the hardware. In the hardware there is an IO card. There is 16 in and 16 out. Changes in input ...