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)

4
votes
1answer
57 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
58 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 == ...
2
votes
2answers
91 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
64 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
60 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 ...
5
votes
1answer
93 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
997 views

Program that guesses your number using bitwise operations

I got the idea for this program from this site. functions.cpp: ...
0
votes
1answer
64 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
839 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
208 views

Check if a given integer is power of two

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

Bit Vector implementation

Looking for a general review. ...
4
votes
0answers
49 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
64 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
47 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
1answer
68 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}...
4
votes
1answer
132 views

Hackerrank Sum vs XoR

I'm in the process of improving my coding style and performance hence I decided to delve into bit manipulation on Hackerrank. I thought the question was straight forward but on large input, my ...
4
votes
1answer
70 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 ...
10
votes
3answers
505 views

Counting the number of “on” bits in an int

Here is some code I wrote as a solution to Programming Exercise 15.3 in Stephen Prata's C Primer Plus, 6th Edition. It is probably worth pointing out that the title of the chapter is Bit Fiddling. The ...
1
vote
0answers
70 views

Performance issue when dealing with bits

Here is the slow part of my code ...
1
vote
1answer
80 views

K&R 2.6 Exercise setbits(x,p,n,y) function

Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position <...
2
votes
1answer
448 views

Xoring in constant time in Java

I was inspired by this answer and the explanation and decided to support negative numbers. While testing, I noticed that the solution didn't handle the case with one number in the given range. A quick ...
3
votes
5answers
103 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? ...
7
votes
2answers
848 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 ...
0
votes
0answers
44 views

Getting TLE for SPOJ Bitplay challenge program

I have tried a naive way to solve this problem: Given a even number N (N ≤ 109) and an integer K, you have to find the greatest odd number M less than N such that the sum of digits in binary ...
3
votes
2answers
44 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 ...
12
votes
4answers
890 views

Return the next power of two

Given an unsigned integer \$N\$, I want to return \$P\$ with \$P\$ being a power of two such that \$P \ge N\$. Examples: 1 -> 1 2 -> 2 3 -> 4 5 -> 8 The goal is to provide a very fast ...
0
votes
0answers
40 views

Bit twiddling utils for Int and Long classes

I added some bit twiddling utils to Int and Long classes in Scala but I have highly duplicated code. Is there a way to DRY this? ...
2
votes
2answers
472 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 ...
4
votes
1answer
732 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: ...
0
votes
1answer
52 views

ANDing ints in Java [closed]

Is there any better way than doing: if ((newState & InputState.DEFAULT) == InputState.DEFAULT) ? My InputState.class defines static int variables to the ...
6
votes
2answers
536 views

Codility binary gap solution using regex

This is how I solved the binary gap problem: Find longest sequence of zeros, bounded by ones, in binary representation of an integer. However, it seems to be different from the answers out there. Is ...
4
votes
2answers
275 views

K&R 2-7 - Invert a bitfield from a number

This is my solution to the Exercise 2-7 of K&R C book. The assignment is: Write a function invert(x,p,n) that returns x ...
7
votes
1answer
61 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 ...
10
votes
1answer
241 views

Bit shifting and masking

I was wondering if someone could review this simple bit of code that originally was in C++, but now converted to C# as practice in using bit-shifting and creating a sort of a quasi-array functionality....
5
votes
4answers
293 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: ...
5
votes
2answers
149 views

Packing and unpacking bits (2)

Part 1 here. I improved my code from my previous question (linked above). ...
6
votes
2answers
258 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? ...
1
vote
1answer
45 views

Using JavaScript-bitwise AND and -bitwise NOT for setting a number to 0

Obviously is the XOR-operator is used in low-level programming-languages used a lot for setting a value back to all bits 0. For example: 11010 ^ 11010 => 00000 Brought my teacher to figuring a out ...
4
votes
5answers
128 views

Test four int of commonality in the lower four bits

The four parameters a, b, c and d can be ...
6
votes
1answer
94 views

Bit string builder for Java

I have this Java class for building bit strings. The set of methods it provides resembles that of java.lang.StringBuilder. See what I have: BitStringBuilder.java: ...
4
votes
1answer
63 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 ...
2
votes
0answers
48 views

Convert tri-gram of type wchar_t to an integer type

I am trying to convert a tri-gram containing 3 characters of type wchar_t to an integer type (an uint64_t at the present time). ...
3
votes
3answers
2k views

Calculating the 'binary gap' of a number

Inspired by this question and therefore this one I decided that clearly the best most readable way to solve this is with Regular Expressions. A binary gap within a positive integer N is any maximal ...
1
vote
0answers
31 views

Implement “at” lens for Data.Map

I've been working on adding an operation like Data.Lens.At.at to Data.Map. This uses an extremely fast, extremely small, and ...
1
vote
3answers
70 views

Byte array to floating point such as FPE2 in Java

Apple's System Management Controller uses FPE2 and SP78 as floating-point formats. While writing a Java library that that interfaces with the SMC, I ported someone's C function to convert read a FPE2-...
4
votes
3answers
294 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 ...
1
vote
0answers
60 views

A bitwise common prefix algorithm in Python

Is my algorithm slow because it has problems? Or, is any bitwise common prefix solution not going to give me the performance I'm looking for? After profiling my algorithm, I found that over 60% of ...
11
votes
1answer
236 views

Console app to teach bitwise operators

I'm creating a console app that has the purpose to teach you how to use C# bitwise operators. I have several classes which contain info about the different operators and I instantiate/add them in the ...