Questions tagged [bit-shift]

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right.

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right, often written as << and >> respectively.

For example 4<<3 = 32 and 5>>1 = 2.

A zero shift returns the integer part, e.g. 3.14<<0 = 3.

2051 questions
0
votes
0 answers

Bit operations for formatting and parsing custom protocol header in C

I'm implementing a custom network protocol that stays between the application and transport layer (UDP). The protocol split the application layer data to smaller chunks (packets) and encapsulates them with some header fields. The headers of the…
Mike Pham
  • 437
  • 6
  • 17
0
votes
0 answers

kotlin type mismatch with shl operator

I am developing a small communications protocol over bluetooth for an arduino to transfer data to Android. Part of the protocol sends a header that contains a line count. I have successfully read the MSB and LSB of the count and must now re-assemble…
0
votes
2 answers

What if a cast operator is used in shift operators

The JLS says that The type of the shift expression is the promoted type of the left-hand operand. If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance.…
user12208242
  • 315
  • 1
  • 11
0
votes
1 answer

Last two digits of decimal number with a mask?

To get the last N digits of a decimal number I usually convert it to a string. For example, for two digits: n = 129387 int((str(n)[-2:])) # 87 With binary its relatively straightforward to do this with a mask, such as: n =…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
0
votes
1 answer

Bitshift operation with long unsigned - Off by one issue

I have a simple bitshift operation in c# that I am porting to JS. You can see it here: https://dotnetfiddle.net/Au62NB //C# code ulong v = 2630423132685782527; UInt32 v1 = (UInt32)(v >> 32); UInt32 v0 = (UInt32)((v << 32) >>…
Tony Gutierrez
  • 753
  • 1
  • 6
  • 16
0
votes
0 answers

Crash from 64-bit bitwise ops

I pack a handful of values inside a 64-bit integer at the end of a network packet. I extract this integer and then use bitwise ops to pull the packed individual values out. This is on 32-bit Android NDK code: uint64_t footer = *(uint64_t*)(payload +…
user1043761
  • 696
  • 6
  • 22
0
votes
1 answer

DDS_Octet Bit Shifting Issue: Right Shift Adds 1s and not 0s in C++

I'm having an issue shifting bits and I cannot seem to understand what is going on. I'm able to shift bits to the left perfectly fine but I am unable to shift bits to the right. Here's a look at what is going on: Shifting to the right (works as…
Walklikeapenguin
  • 127
  • 2
  • 10
0
votes
1 answer

Meaning of XOR shift

I came across an algorithm that uses a lot of XOR shift like so: std::uint16_t a = ... std::uint16_t b = a ^ ( a >> 4 ) I read XOR is used for all kind of good stuff, like finding parity, determining odd/even counts etc. So I'm wondering: Does this…
SamVanDonut
  • 361
  • 2
  • 14
0
votes
0 answers

Bit masking with predefined masks vs Bit shift operations?

I am a hobby programmer doing some things with Poker abstraction. I have cards encoded in 8 bits, the least significant half encodes the rank. the next two bits its suit-id (not suit per se, since then I would needlessly add complexity, It starts…
Some guy
  • 11
  • 4
0
votes
1 answer

How to transform a binary integer into it's partial by shifting it all right of the "."

Let's say we have a binary integer, which I'll call "A", like 101110101100100011. I want an efficient javascript function that makes the whole value partial (like the digits after a "."). So our function, which I'll call intToPart should return…
0
votes
2 answers

Converting 8 bits in binary to 1 byte

I have a string of 8 bits and I want to convert it into 1 byte. I am not sure why my function is not working properly. I have 8 bits stored into an array of 8 unsigned chars. This is my method so far: unsigned int bitsToBytes(unsigned char *bits) { …
user13965647
0
votes
1 answer

c# Left-shift operator << as the «for loop» «iterator»

I need to iterate through every bit of a byte. There are many ways to do it but my curiosity got piqued when trying to use a for loop with the "shift-left" operator as the iterator. This is what I would like to do. for (byte i = 0x01; i <= 0x80; i…
JPRoy 1957
  • 21
  • 2
0
votes
1 answer

Simple Assignment

Here's my problem. I have the following structure defined struct idt_reg{ unsigned short limit; unsigned int base; }__attribute__((packed)); In my code I do the following assignment unsigned short num_ids = idtr.limit >> 3; Now upon execution,…
Micky
  • 63
  • 4
0
votes
2 answers

How do I retrieve values from bits spread across multiple bytes in an array?

The Problem I have a integer array of 16 'options', the data for which stored in an array of 10 bytes. That means that each option takes up a certain number of bits and some options share bytes with others. B, T, P, I, 2, 3, e, S, p, O, t, E, U, V,…
Jeorge535
  • 33
  • 6
0
votes
3 answers

Why does a C compiler not throw a warning when using bitshifting on negative value?

Why I am not getting warning like shifting of negative number is not allowed when I use the following code: #include int main (){ int k = -5; unsigned int shift = 2; unsigned ans = (k >> shift); …
jailaxmi k
  • 89
  • 3
  • 11