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
2answers
328 views

How to optimize bitwise get/set/clear of ranges of bits in JavaScript?

I have been working for a few days on writing get, set, and clear bitwise functions in ...
3
votes
1answer
46 views

Array-like data structure for fixed sized types with non-machine-word sizes

I am writing an array-like data structure for types other than 8 16 32 64 – the usual type sizes. Ideally, my interface is the following for addressing the array. ...
7
votes
1answer
220 views

Parsing a sequence of bits out of a bit field

For homework, I had to parse CPUID instruction output in C, which required a lot of specific bit-manipulation like: (eax & CACHE_LEVEL) >> 5 I ...
2
votes
0answers
28 views

Implementing a Hilbert-like space-filling-curve conversion in Rcpp

I have written a series of functions that are designed to create a space-filling curve (similar to a Hilbert Curve) that is intended for symmetric matrices (as opposed to a square region). I plan to ...
0
votes
1answer
64 views

Factoring the powers of 2 in Python

The powers of 2 are trivial to factor because all powers of 2 have one 1-bit and the rest 0-bits. Just iterate minus one 0-bit as follows: 10000,1000,100,10,1. And then convert the binary strings into ...
32
votes
3answers
6k views

Decide if the sum of three numbers is even or odd

Exercise from an Assembly course I'm enrolled into: Write a program that takes three numbers x,y,z as input, and returns: 0 if (x+y+z) is even. 1 if (x+y+z) is odd. (Note that ...
3
votes
1answer
67 views

Von Neumann random bytes debiasing

I need to create a method to debias biased bytes from the method get_raw_bytes(length) using von Neumann debiasing method. Von ...
2
votes
1answer
140 views

Routine to 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
2answers
158 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: ...
5
votes
1answer
62 views

ROT128 byte-transformation encoding

I'd like some feedback on code I wrote to obfuscate text on-disk. The basic idea is the same as ROT13 (which is implemented in the Python standard library), except instead of rotating the English ...
1
vote
1answer
78 views

LeetCode: Sum of Two Integers C#

https://leetcode.com/problems/sum-of-two-integers/ Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example 1: Input: a = 1, b = 2 Output: ...
5
votes
7answers
13k 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? ...
9
votes
1answer
82 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 ...
7
votes
3answers
136 views

Bit shift 256 bits

I have a working solution to my problem, but I'm less than satisfied with the result, both when it comes to style and performance. In short, I need to be able to shift 256 bits of data, say an array ...
3
votes
1answer
62 views

Swapping Nibbles

I have small (<250 bytes) chunk of binary data, where I know that almost always, the first nibble is going to be zero 0. For example, in hex: ...
6
votes
2answers
2k views

Why are these functions slower than BigInteger's included methods?

I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
4
votes
2answers
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 ...
2
votes
2answers
111 views

Bitmap in Bitmap (Black & white)

Below is my attempt at bitmap in bitmap searching using 1bpp bitmaps. Any suggestions on how I can reduce complexity and improve speed? Any comment is appreciated. ...
8
votes
4answers
19k 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 ...
11
votes
4answers
4k views

Codility binary gap solution using regex

Below is the code I implemented to solve the binary gap problem: Find longest sequence of zeros, bounded by ones, in binary representation of an integer. However, it seems to be different from ...
4
votes
2answers
68 views

converting bits binary to bcd

I wrote a code that requests user input and then converts binary to bcd then back to binary using bitwise operations. While I think my code is good I want to know if there is a better way to write the ...
3
votes
1answer
31 views

Basic Subnet Mask Calculator + A Little Extra

For my Networking class, we learned about subnetting and subnet masks. I decided to write a little "calculator" (a generous term) that when given n-many network ...
2
votes
3answers
173 views

Adding two n and m bit binary numbers

Recently I was studying bitwise operators and bit-manipulation algorithms and I found out an algorithm to add two binary numbers. Pseudocode is as follows: ...
-1
votes
1answer
79 views

Interlock of some equipments in electrical power system using simple Arduino Uno code [closed]

I'm trying to implement some logic functions essential to control a small power system consisting of 2 Transformers labeled T1 and T2, 2 Generators (G1 and G2) and a bus tie (BT). I'm not going to ...
13
votes
1answer
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 ...
7
votes
5answers
158 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 ...
9
votes
2answers
416 views

Proper Case Conversion (Performance)

I wrote a simple function that converts an array of words into proper case (using bitwise operations) and returns the array. I performed a simple test using ...
8
votes
3answers
280 views

“Fast strlen” in C using scalar bithacks

I've translated an implementation of strlen in x86 assembly to C and added alignment checking: ...
4
votes
1answer
291 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 ...
3
votes
2answers
113 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 ...
10
votes
3answers
9k 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 ...
2
votes
1answer
128 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 ...
3
votes
1answer
910 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 ...
2
votes
1answer
35 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 ...
4
votes
1answer
90 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. ...
1
vote
1answer
45 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). ...
10
votes
2answers
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. ...
4
votes
1answer
81 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 ...
6
votes
2answers
83 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 ...
3
votes
1answer
69 views

Set a specific bit to a specific value [closed]

This is the code I came up with: ...
5
votes
1answer
398 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 ...
6
votes
5answers
379 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 ...
1
vote
1answer
78 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 ...
2
votes
1answer
78 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 ...
5
votes
2answers
126 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 ...
7
votes
2answers
302 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 ...
7
votes
4answers
906 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 ...
3
votes
0answers
159 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 ...
4
votes
0answers
332 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: ...
3
votes
1answer
143 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 <...

1
2 3 4 5 6