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
8
votes
1answer
572 views

Optimizing multiplication of two polynomials in GF(2^32)

I have the following method for multiplying two polynomials in GF(232): ...
8
votes
1answer
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 ...
7
votes
3answers
2k views

Custom integer class for calculating crypto algorithms

How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that ...
7
votes
7answers
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 ...
7
votes
4answers
556 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 ...
7
votes
2answers
1k views

Maximize result of bitwise AND

This was the problem from "30 Days of Code" on Hackerrank: Given set \$S = \lbrace{1,2,3,\dots,N}\rbrace\$, find two integers \$A\$ and \$B\$ (where \$A \lt B\$), from set \$S\$ such that the ...
7
votes
2answers
272 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? ...
7
votes
3answers
1k views

Bit exchange operation

I have been working on a certain task these days and after several hours of torture, I have done it! However, I believe the code that I have "crafted" is quite difficult to be understood and to be ...
7
votes
1answer
16k views

Start an EventHandler in different Thread

I just stumbeled accross some code and I would do it in a different way. Since I am quite new to programming, I just wanted to know if I understand it right. Original Code: ...
7
votes
1answer
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: ...
7
votes
1answer
6k views

Combining two 32-bit integers into one 64-bit integer

long long combine(unsigned int high, unsigned int low) { return ((unsigned long long) high) << 32 || low; } Is there a better name for the operation? Is ...
7
votes
2answers
755 views

Find the Sum of Digit XOR till N?

The question is to find the sum of Digit XOR till \$N\$. For eg:- Digit XOR of a number 112 112 => 1 xor 1 xor 2 = 2 So we need to find ...
7
votes
2answers
951 views

Portable byte order conversion

I'm trying to improve the portability of a file format converter, specifically to POSIX platforms other than Linux. I'd appreciate a review of the following functions used for converting between ...
7
votes
2answers
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 ...
7
votes
1answer
1k views

Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
7
votes
2answers
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 ...
7
votes
1answer
295 views

Implementing checksum add without carry in C

I need to write a function in C which performs a checksum using the rule "add without carry". As I understand it, the "add without carry" concept is equivalent to the bitxor operator and given that ...
7
votes
1answer
297 views

Convert size_t to big endian on Linux

I see that on Linux there are various built-in functions to convert endianness for specific integer width types, and they are probably faster than anything one can write and compile on their own. But ...
7
votes
2answers
952 views

Print Bits Part II

Could you suggest ways of making this more simple? ...
7
votes
1answer
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 ...
7
votes
0answers
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 ...
6
votes
2answers
774 views

Bitwise comparison of any structure

What I tried to do is compare arbitrary structures independent of the == operator implementation. ...
6
votes
5answers
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 ...
6
votes
3answers
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: <...
6
votes
3answers
346 views

Fast bitswap in assembly

Can the following code to swap two bits be optimized? ...
6
votes
4answers
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, ...
6
votes
2answers
331 views

Optimizing special cases of modulo

I have a use case where improvements to the speed of calculating modulo for 64 bit integer dividends makes a significant difference in overall processing time. The divisor isn't known statically - it ...
6
votes
2answers
2k views

Packing and unpacking bits

Part 2 here. I wrote this class for packing data. Would it benefit from being named BitPacker rather than BitStream (and change write/read to pack/unpack), or it works as is? How can I improve it? ...
6
votes
2answers
514 views

Variable integer encoding

There is a varint encoding, which I call LEP64 (Little Endian Prefix Max 64-bit). Basically, the first byte of the encoded value tells us, how many bytes follow (needed bytes encoded in unary coding, ...
6
votes
1answer
104 views

Take first 12 bits of a number if longer than 12

I need to only take first 12 bits of a number if its longer than a 12 bit number and convert it to a decimal number. So if 7000 is entered than it will return 3500. I also print out the binary form of ...
6
votes
3answers
87 views

Minimal stack from unsigned int to track boolean values wtih bits

This grew out of a need to track sequential ctrl+shift+Left/Right-arrow keypress events on a gtk editor. It is a small stack implementation for boolean values (for <...
6
votes
2answers
1k views

C# RGBA to ARGB using shifts

I came up with the following code to convert a rgba color to an argb color: ...
6
votes
1answer
16k 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. ...
6
votes
1answer
510 views

Bitfield class and Register depiction

For a few months now I have been working with an ARM CPU. To be specific an ARM Cortex M3 from STM (STM32107VC). Complete example So far I am using the StdPeriphal library, and will continue to do so ...
6
votes
1answer
253 views

YouTube playlist steganography: hide secret messages inside YouTube playlists

I have created this repo on GitHub and I have shown how this software works through a video. If you have time, please visit the repo and read the files. I'll post some relevant snippets below, but if ...
6
votes
2answers
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 ...
6
votes
1answer
490 views

Bit string builder for Java

I have this Java class for building bit strings. The set of methods it provides resembles that of java.lang.StringBuilder. See what I have: BitStringBuilder.java: ...
6
votes
3answers
2k views

Finding the fastest common prefix of 2 strings in Python

I'm comparing my algorithms, slice_prefix and bit_prefix, with existing ones to find the common prefix length of 2 strings as ...
6
votes
1answer
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 ...
6
votes
1answer
2k views

Implementing Karatsuba Multiplication Algorithm in Python

I am trying to implement Karatsuba multiplication algorithm for binary (base 2) numbers. A requirement is that the intermediate / final results must also be in binary so as to assist in educative ...
6
votes
2answers
416 views

Decomposing bitfield with Haskell: is recursion the only way to do this?

Here's a program to take a bitfield like 7 and decompose it into the flags [1,2,4]. Ignore that the algorithm for doing this doesn't use bit shifting/is stupid. Problems: it seems like I have to have ...
6
votes
1answer
695 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 ...
6
votes
2answers
430 views

Shifting an 128-bit integer consisting of four 32-bit integers

For a little project of mine I have written two versions for shifting a 128-bit unsigned integer consisting of four 32-bit unsigned integers in x86 assembly. I cannot really decide which is better in ...
6
votes
1answer
146 views

Representing integers as a sequence of bits

Here is a class Bits that I wrote in order to deal with sequences of bits that also need to be interpreted as non-negative integers (in case you are familiar with ...
5
votes
8answers
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 ...
5
votes
3answers
10k views

Base-36 encoding of a byte array

After posting a question about Alphanumeric Hash generation on StackOverflow, the most helpful answer was to change the conversion method from pulling 5-bit chunks of a binary hash value, to instead ...
5
votes
6answers
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? ...
5
votes
3answers
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 ...
5
votes
4answers
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 ...
5
votes
1answer
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 ...