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
1
vote
1answer
63 views

Bit pattern to console output

This function will draw a card in the console ...
1
vote
0answers
97 views

Sending data from UART to microcontroller via AT89C2051 functioning as a UART/SPI bridge

I have an unusual setup. I have an AT89S52 microcontroller in which its UART is used by the PC. I needed a way to make a second UART based device work with it, so what I decided to do was make a UART ...
2
votes
0answers
183 views

Binary addition in VB.net

It starts by taking two user input from the console and putting them into strings. I then pad them to get them to the same length. I get the length of the num1 and assign it to the integer len. I then ...
6
votes
3answers
328 views

Fast bitswap in assembly

Can the following code to swap two bits be optimized? ...
2
votes
2answers
568 views

Space and time efficient Algorithm for generating prime numbers

I have developed an algorithm for generating prime numbers (which turns out to be an optimized version of the Sieve of Eratosthenes) by reducing the space by a factor of 64. ...
6
votes
3answers
86 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 <...
3
votes
1answer
95 views

Gradient noise class based on Simplex noise

Here's a seedable 2D and 3D gradient noise class in C# that I wrote based on Simplex noise. Seems to work properly (even though it's highly experimental). I'm looking for tips to improve the quality ...
6
votes
1answer
240 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
393 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 ...
5
votes
2answers
328 views

Thread-safe BitArray

Notes: supports 64-bit platforms only ulong used where negative values make no sense library functions are assumed to be correct (whether they actually do what I ...
1
vote
2answers
245 views

Musical graph algorithm, with an inlined lookup helper function

I am working with bit strings in clojure. My question is less about performance and more about how one should go about it (in particular see the last inlined version) The tonnetz-array represents an ...
15
votes
1answer
3k views

Compressing a 'char' array using bit packing

I have a large array (around 1 MB) of type unsigned char (i.e. uint8_t). I know that the bytes in it can have only one of 5 ...
2
votes
4answers
68 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 bit ...
6
votes
2answers
321 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 ...
2
votes
1answer
2k views

Hackerrank “Strings: Making Anagrams” Javascript Solution

This is the original problem : Input Format The first line contains a single string, a. The second line contains a single string, b. Constraints 1<= |a|,|b| <= 10^4 It ...
3
votes
2answers
493 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 ...
3
votes
1answer
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 ...
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: ...
10
votes
2answers
3k views

C++ Bit shift whole array (left)

For an embedded project I want to shift a (byte) array left by a certain amount of bits. I've built this template function to do so: ...
3
votes
3answers
438 views

Binary multiplication on elliptic curves in C++

I wrote a function in C++ which uses binary multiplication (a form of binary exponentation): ...
2
votes
0answers
534 views

Hackerrank: Max Score

Problem statement Consider an n-element sequence of integers, A = {a0, a0, ..., an-1}. We want to perform \$n\$ operations on \$A\$, where each operation is defined by the following sequence of ...
29
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 ...
1
vote
1answer
973 views

Decide if a number is even or odd by using bitwise operator

The following exercise is from an Assembler-course I'm taking: Write a program that takes a number x as input, and returns: 0 if x is even. 1 if x is odd. Full exercise-page here: ...
3
votes
1answer
110 views

Compact Braille lookup

A related question got me thinking about a compact Braille dictionary. It certainly would be easier with Int16 or string but was going for most compact. I don't think there is a case insensitive char ...
7
votes
1answer
281 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 ...
4
votes
3answers
107 views

Count number of bits to convert one integer to another integer

Return the number of bits (N) that will need to be changed in order to convert an integer(X), into another integer(Y). Constraint: 0 <= X <= Y <= 2**128 Example: X = 1, Y = 2 => ...
3
votes
2answers
447 views

Assembly: Sum up the single bytes of a 32bit-register to a checksum

Exercise description: "Write a program that takes a double word (4 bytes) as an argument, and then adds all the 4 bytes. It returns the sum as output. Note that all the bytes are considered to be of ...
13
votes
2answers
549 views

N-Queens, bitwise approach

I've recently solved the famous N-Queens problem: Can you place \$N\$ chess queens on a \$N × N\$ board in such a way that no one attacks another queen? The idea here is based on the ideas from ...
10
votes
3answers
2k views

UTF-8 Validation

I've recently solved the "UTF-8 Validation" LeetCode problem: A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, ...
5
votes
4answers
14k 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 ...
1
vote
1answer
1k views

Forced and easy hton/ntoh conversion

Context: When sending message across the network, the sender has to convert the message from host byte order to network byte order and the receiver has to do the the reverse - network order to host ...
5
votes
1answer
78 views

Search for the longest sequence of toggled consecutive bits in an integer

I have to do a function ​called findsaw. This function searches for the longest sequence of toggled consecutive bits in an integer and returns the index of the ...
8
votes
3answers
1k views

Calculating int next power of two

Here is one of the possible solutions: ...
4
votes
1answer
1k views

Parsing GSM 7-Bit messages in Java

I wrote a function to decode GSM 7-Bit encoded messages as defined in the 3GPP standard. Example available here. I found a tool to help me check my answers here. I used the charset from here. ...
5
votes
1answer
895 views

Biggest Binary Gap Solution

Inspired by this question I've come up with a solution of my own. The task is to find longest sequence of zeroes surrounded by ones in a binary representations of an integer (e.g. 1041 == ...
5
votes
2answers
2k 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? ...
3
votes
2answers
1k views

Calculate XOR of many consecutive and non-consecutive numbers

question: answer(start,length) #start is the starting number of the matrix #length is number of rows. example 1: answer(0,3) ...
6
votes
1answer
143 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 ...
7
votes
1answer
832 views

Integer Log2 implemented using binary search

I saw a simple method to calculate Log2(N), where N is the power of 2: ...
15
votes
2answers
1k views

Program that guesses your number using bitwise operations

I got the idea for this program from this site. functions.cpp: ...
0
votes
1answer
73 views

Adding bits, simpler

Considering that two bitsets are provided in the form of strings, char a[] and char b[], as in the code below. And the task of ...
12
votes
3answers
8k views

Endianness conversion in C

I have written a simple C header for converting the endianness of short integers and long integers. It uses the GCC macro ...
5
votes
1answer
542 views

Check if a given integer is power of two

Given an integer, check if it's the power of two. ...
7
votes
2answers
693 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 ...
11
votes
3answers
6k views

Implementation of C Standard Library Function ntohl()

This is an implementation of ntohl() that I wrote as an exercise. ntohl() takes a uint32_t ...
4
votes
0answers
74 views

Bit Vector implementation

Looking for a general review. ...
4
votes
1answer
367 views

Count bits in VBScript

I'm trying to write a function that returns the number of bits set in a 32-bit integer in VBScript, it's just for practising the language. The function I've written so far looks okay, but I'm ...
3
votes
0answers
2k views

Calculating CRC using arbitrary polynomials

This is my implementation of calculating CRCs which supports arbitrary polynomials specified in different representations (according to the explanations given here and here). My concerns are: I don'...
4
votes
1answer
297 views

Decoding 32-bit MIPS instructions

For a homework assignment I've been given the task of parsing out information from an 32-bit MIPS instruction. (For more information on the instruction formats, see here). The instructor has provided ...
4
votes
2answers
560 views

Compute the XOR of all the elements in the upper left triangular of a square matrix

Given a dimension \$D\$. We generate a \$D\times D\$ matrix with elements that start from \$0\$. For example, if we're given a dimension \$4\$ this is what our matrix looks like: $$ \begin{bmatrix}...