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
5
votes
3answers
205 views

Basic binary number container

Summary: a templated class, whose main purpose is to store a binary representation of a decimal number. I choose array of booleans as a storage, since bitset was forbidden. One of the requirements was ...
3
votes
4answers
198 views

Find the longest length of sequence of 1-bits achievable by flipping a single bit from 0 to 1 in a number

Problem Statement The problem is defined in the book as following: 5.3 You have an integer and you can flip exactly one bit from 0 to ...
4
votes
2answers
116 views

Checking if a number is a power of 2 without loops

I made a short program which checks if a number is a power of 2 without using any loops. The idea: A number which is a power of 2 must have only one bit "1" ( ex: 8= 1000, 4=100 and so on). Suppose ...
2
votes
0answers
86 views

Kaitai Struct: shift rotate >1 byte groups

This is an implementation of rotation (cyclic shift) of many-byte groups to the left. Rotate right is supported as negative amount. I would like to request, if someone would be willing, to review this ...
4
votes
1answer
74 views

Check digital input signals that is sent as an integer using bitwise checking

I have a program that works with a hardware via an ActiveX API. The API have events that is triggered by the hardware. In the hardware there is an IO card. There is 16 in and 16 out. Changes in input ...
6
votes
2answers
959 views

Bitwise comparison of any structure

What I tried to do is compare arbitrary structures independent of the == operator implementation. ...
5
votes
1answer
115 views

C++ container — variable byte coded numbers list

I'm writing a container for unsigned integers, which uses variable byte coding. It works but some decisions I took look ugly so I would be glad if you gave me some hints for improving the design. I ...
7
votes
4answers
862 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 ...
2
votes
1answer
317 views

Reading/writing single bits from/to FILE

I'm implementing a Huffman encoding/decoding program and I need a way to deal with single bits. I tried to keep things as simple as possible, I "pack" bits inside a single byte and I have only one ...
1
vote
2answers
3k views

Reversing the bits of a 32-bit integer

Here is my code: ...
6
votes
1answer
657 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 ...
3
votes
1answer
54 views

An application that compares two lists of byte strings

I am a beginner in Rust, and I am trying to write a program that compares common bits between two lists of bits. The program below does that (with the variable names indicating the domain of ...
5
votes
2answers
157 views

Map a range of integers to a 64-bit integer

Given a 64-bit integer x (which is always larger than 0), we wish to map it to an index so that: when x is between 0..64, we map it to 0, when x is between 65..128,...
1
vote
2answers
58 views

The program that reverses bytes in two distinct ways (in C++)

Will be grateful if anyone would reasonably comment on my code. I am interested in the style only. If there will be any comments which are not related to the style I am glad to read them as well. <...
2
votes
2answers
334 views

Hamming distance

I am wondering how I can improve the runtime of this code. I was solving this task at leetcode, and came up with this solution. But I got the result that my runtime is 88 ms, which beats a bit more ...
3
votes
1answer
814 views

Finding binary gap in Java

I want to know the performance of my code for following problem description. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary ...
4
votes
3answers
1k views

Associating a name with each bit in a bitmask

I have the following code: ...
6
votes
5answers
378 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
2answers
573 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
1k 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
1answer
106 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 ...
1
vote
1answer
67 views

Bit pattern to console output

This function will draw a card in the console ...
1
vote
0answers
158 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
257 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
454 views

Fast bitswap in assembly

Can the following code to swap two bits be optimized? ...
2
votes
2answers
710 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
88 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
111 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
269 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
511 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
401 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
248 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
4k 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 ...
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 ...
6
votes
2answers
352 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 ...
3
votes
1answer
3k 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
675 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 ...
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 ...
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
4k 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
549 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
652 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 ...
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 ...
1
vote
1answer
2k 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
114 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
330 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
110 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
788 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
667 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
3k 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, ...