Questions tagged [byte-shifting]

Related to shifting bytes and byte manipulation. There are operators in most of the programming languages to shift bits which could be used to shift bytes.

77 questions
1
vote
2 answers

Android Kotlin Byte Shifting +=

I'm trying to convert a BLE native Byte handling helper to Kotlin. In the process, I noticed that in JAVA myByteArray[offset] += (byte)(exponent & 0xFF); it works as expected, but when converted to kotlin KOTLIN myByteArray[offset] += (exponent…
Sam
  • 5,342
  • 1
  • 23
  • 39
1
vote
2 answers

Shifting by a number is different than shifting by a variable

I have the following c code: int foo = 0; printf("output1: %08x\n", (~0x0) << (~0)); printf("output2: %08x", (~0x0) << (~foo)); which prints out: output1: ffffffff output2: 80000000 Why does shifting by the same number produce a different result?
Toby
  • 140
  • 6
1
vote
0 answers

Bytes shuffled while copying 2 bytes using memcpy

int main() { unsigned short crc = 0x00; unsigned char buffer[4] = {0x01,0x02,0x72,0xAE}; memcpy((void *)&crc, (void *)&buffer[2],2); printf("crc = 0x%x \n",crc); return 0; } for above program i was expecting crc value to be…
Jack
  • 694
  • 8
  • 20
1
vote
1 answer

C: Reading a whole file into buffer and byteswap the buf | ADDECEFA -> DEADFACE

EDIT: After doing some more research, it seems that what i actually need is big endian to middle endian and vice versa. so 12345678 -> 34127856 and back. Sorry for any confusion. I have a small file, exactly 16MB. I am reading the whole file into a…
james28909
  • 554
  • 2
  • 9
  • 20
1
vote
1 answer

How to reverse bytes order in javascript Uint32

I need to send an ArrayBuffer via chrome.sockets.udp plugin. The data is sent via Android ionic app to an emulator that runs on a computer. I need to send the following fields: var arrayBuffer = new ArrayBuffer(20); var dv = new…
1
vote
1 answer

How to combine two halves from two bytes to get a single byte

Let us say that byte1 = 00001111, and byte2 = 11110000. How would I take the second half of byte1, then take the first half of byte2 and combine them to get one byte of value 11111111. I have done some research, but I just cannot seem to wrap my…
Hobbs2000
  • 311
  • 1
  • 6
  • 19
1
vote
5 answers

How to fill high-end bits in a Java byte with '1' without knowing the last 1 in advance? (FAST FIX Negative Integer decoder)

I am writing a FIX/FAST decoder for negative numbers as described below: My question is: How to fill the high-end bits of a Java byte with 1s as it is described above? I am probably unaware of some bit manipulation magic I need to in this…
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
1
vote
3 answers

retrieving bits from bytes

Hey so I was wondering if someone could explain how this works, I have to retrieve the 3rd bit from a byte, it is a bool value, and I i'm confused about how this actually works, and also if I have the syntax correct. I keep coming on so many…
1
vote
1 answer

Swapping 2 Bytes of Integer

I have a method that receives 3 parameters: int x, int n, and int m. It returns an int with the nth and mth bytes of x swapped x is just a normal integer, set to any value. n and m are integers between 0 and 3. For example, let the hex…
the_pwner224
  • 99
  • 1
  • 1
  • 11
1
vote
1 answer

Bit shifting repeating pattern

I'm looking for a way to bit shifting this pattern: 6 24 96 All these numbers are multiples of 2 so I was thinking there would be a way to shift them I want to shift them so I get the pattern but it keeps repeating possibly in a…
zacharoni16
  • 305
  • 1
  • 4
  • 16
1
vote
3 answers

Creating a ISO-8859-1 string from a HEX-string in Java, shifting bits

I am trying to convert a HEX-sequence to a String encoded in either, ISO-8859-1, UTF-8 or UTF-16BE. That is, I have a String looking like: "0422043504410442" this represents the characters: "Test" in UTF-16BE. The code I used to convert between the…
ABeanSits
  • 1,725
  • 1
  • 17
  • 34
1
vote
6 answers

convert 24bit RGB to ARGB16

I have to read a 24bpp Bitmap and convert each pixel from RGB24 to ARGB16. I used the following code, #define ARGB16(a, r, g, b) ( ((a) << 15) | (r)|((g)<<5)|((b)<<10)) But I am not getting the required Output. Any help would be highly…
green
  • 107
  • 1
  • 6
  • 13
0
votes
2 answers

What's the meaning of shifting in fixed-point arithmetic when implementing it in C++?

I have a problem with understanding fixed-point arithmetic and its implementation in C++. I was trying to understand this code: #define scale 16 int DoubleToFixed(double num){ return num * ((double)(1 << scale)); } double FixedToDoble(int…
th3plus
  • 161
  • 2
  • 11
0
votes
1 answer

Python-numpy reading bytes and offset to signed int32

I have an operation to apply in python to more than 10 millions values. My problem is to optimise the actual operation. I have 2 working methods, numpy and python vanilla. Python vanilla operation: 1: My raw value is a 4 byte data: b'\x9a#\xe6\x00'…
Vincent Bénet
  • 1,212
  • 6
  • 20
0
votes
1 answer

Am I wrong with these shiftings?

I have a 14 bits address with leading zeros and I want to divide it into page number and offset. I was trying to solve it with shifting the address but my offset is getting wrong value, however the page number shows up correctly. Can you point where…
Qaimaq
  • 117
  • 7