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.
337
questions
8
votes
3
answers
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 ...
8
votes
3
answers
281
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 ...
8
votes
2
answers
3k
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 ...
8
votes
1
answer
29k
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:
...
8
votes
1
answer
2k
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
1
answer
11k
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 ...
8
votes
2
answers
5k
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. ...
8
votes
2
answers
269
views
All arithmetic operator functions (+, -, *, /, %) coded only using bitwise operators in C
Here's all the functions in C that does all the arithmetic operators without using arithmetic operators themselves, mostly using bitwise operators. Recursive functions and comparison operators are ...
8
votes
2
answers
540
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) ...
8
votes
2
answers
1k
views
Representing Tic-Tac-Toe using a binary BitBoard
So I have been learning more about binary numbers lately, particularly
in related to describing a gamestate. While perhaps chess is the most natural candidate I wanted to start of with something ...
8
votes
1
answer
188
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 ...
8
votes
1
answer
95
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
7
answers
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
5
answers
256
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 ...
7
votes
2
answers
299
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
1
answer
6k
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:
<...
7
votes
3
answers
2k
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
2
answers
817
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 ...
7
votes
1
answer
330
views
Calculating the results of bit operations
I have a simple program that calculates and outputs the results of all the bit operators. I'm planning to send it to my friend and to explain some stuff about them to him. Before I do that, I want to ...
7
votes
2
answers
1k
views
Convert 64-bit Gray code to integer
This algorithm encodes a 64 bit integer.
As input I have a 64 bit integer a, for example:
(a is a single 64 bit number,which I split in 8 bytes for readability) <...
7
votes
3
answers
1k
views
"Fast strlen" in C using scalar bithacks
I've translated an implementation of strlen in x86 assembly to C and added alignment checking:
...
7
votes
1
answer
518
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
3
answers
102
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 <...
7
votes
2
answers
2k
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
2
answers
2k
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
2
answers
1k
views
Print Bits Part II
Could you suggest ways of making this more simple?
...
7
votes
3
answers
3k
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 ...
7
votes
2
answers
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
3
answers
926
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 ...
7
votes
1
answer
544
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
1
answer
3k
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 ...
7
votes
1
answer
597
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
1
answer
5k
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 ...
7
votes
1
answer
183
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 ...
6
votes
3
answers
16k
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 ...
6
votes
2
answers
2k
views
Bitwise comparison of any structure
What I tried to do is compare arbitrary structures independent of the == operator implementation.
...
6
votes
5
answers
582
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
3
answers
524
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
2
answers
3k
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 ...
6
votes
3
answers
1k
views
Fast bitswap in assembly
Can the following code to swap two bits be optimized?
...
6
votes
2
answers
3k
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?
...
6
votes
3
answers
797
views
C++ Bitwise adjacency matrix implementation
I have tried to implement a bitwise adjacency matrix to represent a graph with a fixed number of vertices.
Instead of wastefully representing each connection with an integer, 64 connections are ...
6
votes
6
answers
20k
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:
...
6
votes
2
answers
11k
views
Bit packing and unpacking
I needed a variadic function to pack and unpack bits into integer types. This is my first attempt:
...
6
votes
4
answers
744
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
3
answers
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.
...
6
votes
2
answers
3k
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
2
answers
3k
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
1
answer
171
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
1
answer
20k
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.
...