Tagged Questions

Bitwise refers to a bitwise operation which operates on one or more bit patterns or binary numerals at the level of their individual bits.

learn more… | top users | synonyms (1)

3
votes
0answers
44 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 ...
6
votes
2answers
60 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 ...
8
votes
1answer
169 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: ...
7
votes
1answer
95 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 ...
11
votes
1answer
349 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
2answers
118 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) ...
11
votes
1answer
135 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 ...
16
votes
3answers
1k 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 ...
2
votes
1answer
72 views

Bitwise operation to get the weekday name

The function days_name receive a val (string) corresponding to weekdays in hexadecimal. ...
3
votes
2answers
89 views

Emulating __uint128_t

I developed a lot of code locally with __uint128_t only to find out that it is not available on the target platform. I am now trying to write my own struct that ...
7
votes
1answer
126 views

Hexadecimal to RGB conversion

I am trying to convert hex to rgb, and rgb to hex. My current code doesn't seem optimal, and there is an issue with 0's in the hexadecimal. What do you think about the overall quality of this code? ...
8
votes
2answers
315 views

Validating UTF-8 byte array

I'm writing a validator function that receives a byte[] and checks whether it represents a valid UTF-8 byte sequence, according to this table. Is my approach ...
4
votes
4answers
248 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 ...
6
votes
1answer
177 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 ...
10
votes
3answers
385 views

Finding the largest of a series of numbers being XORed

I'm working on solving a problem involving finding the largest of a series of numbers being XORed. This is the actual problem: Xorq has invented an encryption algorithm which uses bitwise XOR ...
8
votes
3answers
102 views

Check if bit is only set once in a vector of int

I have a vector of uint16_t and I want to check if there is a bit which is only set in one vector. I then get its position within the vector as well as from the ...
5
votes
2answers
168 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 ...
3
votes
1answer
93 views

Finding the 'Minimum' AND of all the possible subsets of a set

The subset size should be greater than or equal to 2. How can I speed up this code? ...
15
votes
3answers
1k views

Converting an IP

I've written a DNS tool in Node and would like to know if there's a better and more efficient way of handling the conversion from an IP to a long. I'm not too good at bitwise just yet and would like ...
3
votes
2answers
243 views

Function for splitting an integer into smaller values

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way? ...
4
votes
2answers
60 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 ...
5
votes
1answer
87 views

Template for endianness-free code, data always packed as BIG-Endian

It's been a while since I've been properly grilled about my code. I had this Idea which should probably never make it into production code but still I couldn't find anything seriously wrong with it ...
5
votes
2answers
95 views

Implementations of rotate, shift, XOR, NOT and AND

I am going through the example of SHA256 over at Wikipedia and wanted to make sure I understood all the operations of the manipulation math before trying to actually attempt the implementation itself. ...
1
vote
2answers
18 views

Displaying data as binary in C with bitmasks

I wrote a program to display data in memory as binary and it worked. (It gave me immense sense of satisfaction and joy to see this work. I guess this happens to newbies who are just testing the ...
0
votes
4answers
131 views

Arithmetic or logical right shifts

Is this code correct? Is there any major improvement I can ad in terms of number of operations? ...
23
votes
8answers
10k views

Check if a number is divisible by 3, efficiently

Efficient way to check if a number is multiple of three. Looking for code review, best practices and optimizations. ...
2
votes
2answers
143 views

Fastest / most efficient random bit indexing in Python

Using Python/Cython, I am trying to construct an efficient way to index boolean values. I have been trying to use the bitarray and/or bitstring packages to construct a fast, non memory intensive way ...
4
votes
1answer
148 views

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

I have the following method for multiplying two polynomials in GF(232): ...
8
votes
3answers
472 views

Integer square root in x86 assembly (NASM)

This program calculates the square root of an unsigned 32-bit number with some bit fiddling to get a very close approximation, then one iteration of the Babylonian method to get an exact value. I ...
7
votes
3answers
428 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 ...
4
votes
2answers
521 views

Bit packing and unpacking

I needed a variadic function to pack and unpack bits into integer types. This is my first attempt: ...
5
votes
2answers
100 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 ...
5
votes
1answer
60 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. ...
0
votes
0answers
41 views

Properly handling NOT operators for NodeJS

Having the assembly code: ...
14
votes
5answers
1k views

Setting and getting bits in C

I am just starting to get my feet wet with C; and am rather enjoying myself in doing so. This is sort of a follow up to my last question here. I am posting this ordinate to hopefully getting some ...
4
votes
1answer
110 views

Efficiently counting '1' bits in the first n bits

I have two functions to count the number of set '1' bits in the first offset bits of either a 64 bit number, or a bitmap struct. After some profiling, particularly ...
7
votes
4answers
857 views

Boolean expression complexity and bitwise operators

Given this kind of code: ...
13
votes
4answers
3k views

Counting number of 1's and 0's from integer with bitwise operation

Is there a better way of doing this? ...
3
votes
2answers
141 views

Function for reading bits

I've made this simple function for reading N bits from an unsigned char* (with a possible offset) and I'm willing to share it here, get a review, suggestions for ...
0
votes
0answers
38 views

Could this ROM reader be faster and/or smaller?

I've been practicing arduino programming for a few weeks now having become confident in reading and writing to pins so I decided to take it a step further and do a serious project that does both, so ...
2
votes
2answers
133 views

Copy and bit-invert ints in an std::vector

I want a second copy of an std::vector in which all elements are inverted, e.g. 0x01 in the first vector should be ...
2
votes
1answer
141 views

Take all the bits in a number and shift them to the left end

I wrote this program which takes all the bits in a number and shifts them to the left end (for example: 01010110 --> 11110000). It's and exercise from a book. It works, but it seems to be very ...
5
votes
3answers
2k 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 ...
11
votes
1answer
203 views

10x10 bitmapped square with bits

This program prints out a 10x10 square and uses only bit operations. It works well, but I don't like that global array. Can you tell me if the code is proper or not? ...
5
votes
1answer
145 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 ...
1
vote
1answer
350 views

Missing element from array (D&C) - code design/aesthetic improvement

I am trying to implement in C/C++ a 'classical' Divide and Conquer algorithm which solves the following problem "Given an array of n-1 numbers (int) from 0 to n, find the missing number". I am using ...
0
votes
1answer
119 views

Bitwise Logic optimization

I should be able to figure this out on my own, but I can't seem to wrap my head around how to optimize this set of logic for interpreting the existence of URL parameters. Below is my code: ...
2
votes
2answers
138 views

Color fading function [on hold]

I found this old color fading function in my snippets folder and would like to implement it to one of my projects. It can be used to fade one color to another. It's a very long one-liner: ...
4
votes
2answers
872 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 ...
2
votes
5answers
2k views

Divide group of numbers into two groups of which the sums are equal

I have a piece of code that divides a group of numbers into two groups of numbers of which the sums are equal. I created this because of a Stack Overflow question: For example: {10,15,20,5} will ...