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
4
votes
1answer
1k views

Copy arbitrary number of bits at arbitrary offset from buffer to another buffer

I have just written a function copy_lowbits_off to copy any number of bits (not bytes) from a source buffer to a destination buffer. The function also supports ...
4
votes
1answer
169 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 ...
4
votes
1answer
70 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
4
votes
1answer
402 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 ...
4
votes
1answer
228 views

Using bitwise operators with (unsigned long) instead of bitfields

Here is my solution to Programming Exercise 15-7 from Stephen Prata's C Primer Plus, 6th Edition. This problem asks the reader to: 15-7 Write a program with the same behavior as described in ...
4
votes
1answer
216 views

Solution to part 1 of Day 7 Advent of Code in Scala

Does someone want to review my solution to part 1 of Day 7 Advent of Code in Scala? The assignment is here: http://adventofcode.com/day/7 Without repeating the whole text, in short: Given a text file ...
4
votes
1answer
818 views

Connect Four: Bitboard checking algorithm

I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning. The code below is a Clojure implementation of this bitboard algorithm. The whole code can be found here: ...
4
votes
1answer
81 views

Decoding of binary data (AIS) from socket

I am currently working on a AIS message decoder written in pure Python. It's just a little project to teach me some things about binary data, decoding, etc. Because I am only programming for about a ...
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. ...
4
votes
1answer
144 views

LEB128 in Haskell

I have implemented the LEB128 encoding for unsigned integers. I'm fairly new to Haskell so I have some doubt about the coding style I'm using, particularly the definition of ...
4
votes
1answer
54 views

Bit stream input in Factor

My attempt at writing code to input values of arbitrary bitlength based on a bytearray in Factor. Any comments on the correctness or style would be appreciated. ...
4
votes
1answer
4k views

Bitwise Flag code for Python

I am looking for feedback on this compact Python API for defining bitwise flags and setting/manipulating them. Under the hood, FlagType extends int and all ...
4
votes
0answers
88 views

RGB to YCbCr conversion in Java

I found a Go language code to do RGB to YCbCr conversion from here! I ported it to Java: ...
4
votes
0answers
78 views

Bit Vector implementation

Looking for a general review. ...
4
votes
0answers
116 views

Parallel popcount of packed bytes

I want to do a popcount of bytes in an array. Not a total popcount of everything, but lots of little popcounts for every individual byte to determine the neighborhood count in GoL. The following ...
3
votes
5answers
829 views

Largest binary gap

I took a task from Codility to find longest sequence of zeros in binary representation of an integer. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. ...
3
votes
3answers
367 views

Mockup in C of hardware implementation for multiplication of signed integers

I challenged myself to implement multiplication of signed integers using only +, -, >>,...
3
votes
3answers
482 views

Binary multiplication on elliptic curves in C++

I wrote a function in C++ which uses binary multiplication (a form of binary exponentation): ...
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) ...
3
votes
5answers
8k 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 ...
3
votes
2answers
143 views

Given bit strings, perform a bitwise addition

I'm looking for code-review, optimizations and best practices. This code is a Java adaptation of code on geeksforgeeks.org. Please verify if space complexity is \$O(n)\$ where n is number of bits, and ...
3
votes
2answers
516 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 ...
3
votes
3answers
467 views

Bit manipulation tool set

I made a tool set to facilitate bit manipulation as follows. Any comments are welcome, be it about the interface design, the implementation, or even the naming. Also, I want to make sure that the tool ...
3
votes
4answers
154 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 ...
3
votes
3answers
5k views

Addition of two IEEE754-32bit single precision floating point numbers in C++

I made a C++ program which takes a list of triplets with S.No. from a text file, where the triplet, i.e. a,b,c correspond to a+b=c (addition done using float data type). Now I convert a and b from hex ...
3
votes
3answers
3k views

Number of right cyclic shifts to give the biggest number

I am preparing to coding interview and I met this task today: You are given 30-bit unsigned integer N. A right cyclic shift of N by K bits is the result of performing a right cyclic shift of N by one ...
3
votes
2answers
473 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? ...
3
votes
2answers
88 views

Byte extension methods for common bitwise operations

I'd like to learn how I can optimize the following byte extension methods. I'm targeting .NET Standard 2.0 but I'm curious if there are any ways to go about some of this functionality using features ...
3
votes
2answers
290 views

Change Arithmetic Right Shift to Logical Right Shift

The following code is a solution to a textbook (Bryant&O'Hallaron: Computer Systems A programmer's Perspective 2nd Ed) problem in bit-level data manipulation (attempted for the challenge, not a ...
3
votes
5answers
129 views

Toggle four relays on/off based on bitmap stored in uint8_t

I am using a one-byte bitmap to control four relays. I am writing a function which will take the bitmap stored as uint8_t, look to see which bits are set/cleared, ...
3
votes
2answers
1k 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 ...
3
votes
2answers
239 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 ...
3
votes
1answer
959 views

Hamming Distance in Python

I was solving this Leetcode challenge about Hamming Distance. Would love feedback on my syntax and code style. Here's the challenge description: The Hamming distance between two integers is the ...
3
votes
1answer
604 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 ...
3
votes
1answer
354 views

Parsing an IP address (Bit manipulation)

Given a string representing of an IP address, such as "127.43.23.59", return a 32 bit integer representation of this string. The 32 bit integer is separated into four bytes, where each byte is one of ...
3
votes
1answer
1k views

Network byte order to host byte order

While doing a little project involving sniffing packets, I found myself programming a cross platform network to host byte order and host to network byte order, this is my first time developing a ...
3
votes
1answer
97 views

Division without using division, multiplication, or modulus operators

The task Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder. My solution <...
3
votes
2answers
542 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
2answers
247 views

Binary permissions system with bitwise operators + properties

I've written a simple permissions system that I need to be rather fast, yet useable by other developers who primarily use Java. Is a bitwise permissions system with properties a good compromise. Any ...
3
votes
1answer
43 views

PWM wave generation

This code takes a integer x of range [0,127] as input, returns an array of four 32-bit integers, with x bits set. I'm trying to ...
3
votes
1answer
5k views

Byte-to-binary conversion function

My implementation of a binary-to-bits converting function: ...
3
votes
1answer
180 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? ...
3
votes
1answer
328 views

Print Bits Part III

The last question I have been playing around with is a right rotate. I think it is also called a barrel roll. Please post if you have a better solution please post. I am still learning the art of bit ...
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 ...
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 ...
3
votes
5answers
490 views

Print the next smallest and next largest number that have the same number of 1 bits in their binary representation

Given an integer , print the next smallest and next largest number that have the same number of 1 bits in their binary representation. How can I improve this? ...
3
votes
1answer
75 views

Testing for divisibility of numbers - follow-up

(Here is the follow up question) I had another go at trying to speed up my program described in my previous question. JS1's answer was particulary helpful and now my code is only about 20% slower ...
3
votes
1answer
671 views

Implementing binary output to a file in C++11

I would like to have a class which supports outputting bits into a file. This is what I came up with but I fear there are many poor decisions. Should I inherit from a ...
3
votes
1answer
776 views

Finding patterns in a growing collection

If you look at the condition of If calculated Then in the code below, this is what slowing down the code. While the code provided seem fast with ...
3
votes
1answer
65 views

Set a specific bit to a specific value [closed]

This is the code I came up with: ...