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.

277 questions
Filter by
Sorted by
Tagged with
101 views

Decoding of binary data (AIS) from socket

I am currently working on a AIS message decoder written in pure Python. It's just a little project to teach me some things about binary data, decoding, etc. Because I am only programming for about a ...
88 views

Byte extension methods for common bitwise operations

I'd like to learn how I can optimize the following byte extension methods. I'm targeting .NET Standard 2.0 but I'm curious if there are any ways to go about some of this functionality using features ...
8k views

Calculating the 'binary gap' of a number

Inspired by this question and therefore this one I decided that clearly the best most readable way to solve this is with Regular Expressions. A binary gap within a positive integer N is any maximal ...
118 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 ...
55 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 ...
23 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 ...
58 views

Invert bits of binary representation of number

This is the code I came up with. I added comments to make the solution more verbose. ...
35 views

LeetCode Count number of set bits solution

Question - Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). ...
1k views

Bouncing map back into its bounds, after user dragged it out

Users like exploring maps, seeing all that is around. However they tend to go too far. To prevent the map from getting lost to the user it bounces back into the set bounds, as defined by a rectangle. ...
70 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
78 views

Count all X-shaped bits from 8 integers

I am given 8 positive 32-bit integer numbers. The task is to write program to count all X-bits. X-bits are groups of 9 bits (3 rows x 3 columns) forming the letter "X". task is to count all X-bits ...
65 views

Set a specific bit to a specific value [closed]

This is the code I came up with: ...
130 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 ...
344 views

Rotating 3D bit board in C

I've got a 64 bit bitboard, but instead of interpreting as an 8 by 8 binary matrix, as is usually the case, I've taken it to the third dimension, so to speak. Now mirroring and even rotating by 180 ...
76 views

Go command-line tool to do some simple byte manipulation

I have some files containing 16-bit words in big-endian order, and a tool to process them that assumes little-endian order. I therefore need to swap the order of each pair of bytes in my files. This ...
30 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 ...
122 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 ...
50 views

Fun crypto problem: Exploiting a vulnerable encryption scheme with Python

I came across a question asked here and here on the Crypto Stack Exchange site, and decided to analyze it as a kind of case study. To demonstrate the concept, I wrote a short python script depicting ...
97 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: ...
166 views

Register “%b” conversion specifier

I'm writing a library to register the b, B conversion specifiers and make them work the closest possible to ...
557 views

Calculating linux based octal file permission

I'm applying for a mid level devloper position. In order to progress to 2nd interview, you need to pass a technical test. One of the questions was to write a function to work out linux based octal ...
87 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 ...
89 views

RGB to YCbCr conversion in Java

I found a Go language code to do RGB to YCbCr conversion from here! I ported it to Java: ...
97 views

Division without using division, multiplication, or modulus operators

The task Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder. My solution <...
15k views

Find the binary gap of a number N

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of ...
9k views

16 bit checksum calculation [closed]

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

Codility “PermMissingElem” Solution

This is my solution to the PermMissingElem problem, I wonder what can be improved? Expected worst case time complexity is O(N), but the performance test shows that it's O(N) or O(N * log(N)), which I ...
65k views

Pack and unpack bytes to strings

I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that ...
1k views

Hamming Distance in Python

I was solving this Leetcode challenge about Hamming Distance. Would love feedback on my syntax and code style. Here's the challenge description: The Hamming distance between two integers is the ...
11k 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? ...
251 views

Encoding 0/1 bits to 0/255 bytes in C++

I need to map a sequence of bits that represents a float into a sequence of bytes by this rule: the value 1 maps to the value 255 and the value 0 maps to the value 0. This is the code I have now: <...
7k 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 ...
865 views

Largest binary gap

I took a task from Codility to find longest sequence of zeros in binary representation of an integer. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. ...
296 views

Change Arithmetic Right Shift to Logical Right Shift

The following code is a solution to a textbook (Bryant&O'Hallaron: Computer Systems A programmer's Perspective 2nd Ed) problem in bit-level data manipulation (attempted for the challenge, not a ...
698 views

Creating and storing binary arrays

I recently created a library in C, which creates and stores binary arrays. My library uses the standard fixed data types and allocates no more than 8 bytes for a binary array (8 bytes = 64-bit ...
39 views

Implementing carryless multiplication using normal multiplication

In carryless multiplication the partial products are XORed (ie addition without carry) together rather than added normally. The partial products themselves are still the product of a power of two (...
141 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 ...
28 views

Given an integer, return its binary representation. You cannot use any Integer.toXXXString methods

Here is my code: ...
66 views

Given an integer number, invert its bits. You cannot use the ~ operator

Here is my code: ...
208 views

Efficiently swapping the first and last five bits in an unsigned 16-bit integer in C

I'm programming on an 8-bit Z80 embedded system, and encountered a problem. The program needs to render some pixels to the screen. The color is stored as RGB565 format as a 16-bit unsigned integer, ...
86 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 ...
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 ...
128 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 ...
53 views

Kademlia is a DHT (distributed hash table) which uses a specific type of routing table, dependent on the Hamming distance between the "addresses" of two peers on the network. I'm attempting to ...
144 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 ...
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. ...
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 ...