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
6
votes
2answers
57 views

Count all X-shaped bits from 8 integers

I am given 8 positive 32-bit integer numbers. The task is to write program to count all X-bits. X-bits are groups of 9 bits (3 rows x 3 columns) forming the letter "X". task is to count all X-bits ...
3
votes
1answer
55 views

Set a specific bit to a specific value [on hold]

This is the code I came up with: ...
5
votes
1answer
100 views

pythonic way to split bytes

I have incoming data from an absolute rotary encoder. It is 24 bits wide. 12 bits are the number of turns and 12 bits are the angle. I'm reading the data with an Arduino and sending it to the ...
4
votes
0answers
41 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 ...
2
votes
1answer
29 views

Indeterminate-length big-endian to little-endian conversion in Python

I have a need to convert wire encoding (big-endian) to little-endian in Python. The conversion needs to happen as a separate function; I am not allowed to change the function that reads the byte ...
5
votes
2answers
120 views

Templated division by a power of 2

I've implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases when the developer know ...
6
votes
0answers
39 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 ...
1
vote
2answers
87 views

Find out whether number is of Power of Two

The task is taken from leetcode Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: ...
7
votes
2answers
135 views

Register “%b” conversion specifier

I'm writing a library to register the b, B conversion specifiers and make them work the closest possible to ...
1
vote
1answer
71 views

Go command-line tool to do some simple byte manipulation

I have some files containing 16-bit words in big-endian order, and a tool to process them that assumes little-endian order. I therefore need to swap the order of each pair of bytes in my files. This ...
3
votes
0answers
57 views

Java code to convert BigDecimal to/from .NET decimal

Background In many languages we have built-in data types for representing decimal floating point numbers. In .NET that's decimal, and in Java we have ...
4
votes
0answers
71 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: ...
3
votes
1answer
86 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 <...
4
votes
1answer
106 views

Python binary calculator

My task was to build a calculator in Python supporting binary addition etc. To begin I define a couple of custom exceptions : ...
3
votes
1answer
453 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 ...
6
votes
3answers
244 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: <...
3
votes
5answers
509 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
2answers
120 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 ...
0
votes
1answer
32 views

Implementing carryless multiplication using normal multiplication

In carryless multiplication the partial products are XORed (ie addition without carry) together rather than added normally. The partial products themselves are still the product of a power of two (...
-1
votes
2answers
65 views

Given an integer number, invert its bits. You cannot use the ~ operator

Here is my code: ...
0
votes
1answer
28 views

Given an integer, return its binary representation. You cannot use any Integer.toXXXString methods

Here is my code: ...
4
votes
2answers
129 views

C bit mask calculator written using GTK+ 3

I've been studying C on my own using K&R 2nd Edition, and started exploring GTK+ to practice some of the concepts I've learned by making GUI applications. This is the first GUI application I've ...
6
votes
4answers
199 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, ...
2
votes
1answer
80 views

C++ class for a flag register in a Chip8 emulator

In order to learn C++ I decided to code a Chip8 emulator following a tutorial. When I came across the idea of a flag register and decided it would be fun to implement in order to get familiar with ...
1
vote
1answer
63 views

Maximise XOR of two elements

I have a number x and I need to choose another number y from the given range [l, r] such ...
0
votes
1answer
51 views

Kademlia bucketing

Kademlia is a DHT (distributed hash table) which uses a specific type of routing table, dependent on the Hamming distance between the "addresses" of two peers on the network. I'm attempting to ...
5
votes
2answers
121 views

Circular-shift with variable bitlength

For some memory masking operations I need a bitwise-rotation functionality for unsigned integral types. I came across solutions based on the wiki article, but have not found a variation for a function ...
3
votes
1answer
91 views

K&R Exercise 2-6 : Inserting part of a bitset into another

"Inserting" part of a bitset into another This is Exercise 2-6 on the K&R book, 2nd edition. The reader is asked to: Write a function setbits(x,p,n,y) that returns x with the n bits that begin ...
1
vote
1answer
173 views

Adder method of a binary calculator

This is the adder method of my binary calculator. I did not cover for the case that someone passes a non binary number into the calculator yet. I am pretty sure I have covered every other edge case ...
4
votes
3answers
180 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
144 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
111 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
73 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
67 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
681 views

Bitwise comparison of any structure

What I tried to do is compare arbitrary structures independent of the == operator implementation. ...
5
votes
1answer
108 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
466 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
171 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
2k views

Reversing the bits of a 32-bit integer

Here is my code: ...
6
votes
1answer
439 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
53 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
140 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
284 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
537 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
958 views

Associating a name with each bit in a bitmask

I have the following code: ...
6
votes
5answers
340 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
472 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
501 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
103 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 ...