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.
Questions tagged [byte-shifting]
77 questions
9
votes
4 answers
Best way to shift an array in C?
I have an array that holds a history of values, and when adding a new value, I need to shift all previous values one position to the left, to loose the oldest value and make room for the next.
I can think of two ways of doing this, by using…

Maestro
- 9,046
- 15
- 83
- 116
5
votes
2 answers
Fastest way to pack two nibbles into one byte
What would be the fastest way to pack two bytes into one? I have a large array of bytes. Each byte represents a number not larger than 15 (4-bit number). Because of that I could pack two bytes into one, placing the first byte into the higher nibble…

oipoistar
- 477
- 6
- 19
5
votes
5 answers
Read multiple bytes in Hexadecimal file using python
I have a hex file which appears as below:-
00000000 AA AA 11 FF EC FF E7 3E FA DA D8 78 39 75 89 4E
00000010 FD FD BF E5 FF DD FF AA E9 78 67 84 90 E4 87 83
00000020 9F E7 80 FD FE 73 75 78 93 47 58 93 EE 33 33 3F
I want to read 3rd and 4th byte.…

user3469616
- 53
- 1
- 1
- 3
5
votes
1 answer
MIPS - Shifting register values by a numeric value
Can someone explain this to me? The problem is:
sll $t2, $t0, 44
and the goal is to find the value of $t2 after the operation. The initial values are:
$t2 = 0x12345678
$t0 = 0xAAAAAAAA
I understand that the pseudocode translation of "sll $t2, $t0,…

user3025403
- 1,070
- 3
- 21
- 33
4
votes
2 answers
Java shifting bytes returns an unexpected result
I am trying to shift 2 bytes into a short. These 2 bytes represent an unsigned short that in turn represents a port. I've tried multiple ways to shift these bytes into a short on java. However, I constantly fail to do this correctly.
These are the…

Max
- 2,354
- 2
- 14
- 27
4
votes
1 answer
Does left shifting a byte change the signed bit in Java?
I have
byte i = 30;
i <<= 3;
System.out.println(i);
Which I believe would change 00011110 to 11110000 Does doing this actually put a one into the signed bit, or is the one that would be in the signed bit cut off and the signed bit is changed some…

kneedhelp
- 555
- 4
- 18
4
votes
2 answers
How to convert a string to a byte array which is compiled with a given charset in Go?
In java, we can use the method of String : byte[] getBytes(Charset charset) .
This method Encodes a String into a sequence of bytes using the given charset, storing the result into a new byte array.
But how to do this in GO?
Is there any similar way…

hardPass
- 19,033
- 19
- 40
- 42
3
votes
2 answers
Byte shift issue in an integer conversion
I read 3 bytes in a binary file which I need to convert into an integer.
I use this code to read the bytes :
LastNum last1Hz = new LastNum();
last1Hz.Freq = 1;
Byte[] LastNumBytes1Hz = new Byte[3];
Array.Copy(lap_info, (8 + (32 * k)),…

Alexus
- 276
- 1
- 5
- 22
3
votes
3 answers
Switching byte order without type punning
I Need to switch the order of bytes so that an int16 with contents (byte1, byte2) -> (byte2, byte1). I did this using a union:
union ConversionUnion
{
uint8_t m_8[4];
uint16_t m_16[2];
uint32_t m_32;
};
//use
uint16_t example =…

Stephen Eckels
- 435
- 6
- 17
3
votes
1 answer
Convert codepoint to utf-8 byte array in Java using shifting operations
I have used this answer to "manually" convert from unicode to UTF-8 code units. The problem is that I need the resulting UTF-8 to be contained in a byte array. How can I do that by using shifting operations whenever possible to go from hexadecimal…

randombee
- 699
- 1
- 5
- 26
3
votes
5 answers
Why left shifting in java changing the sign value
I am working on java.
I am wondering why java producing this output.
I am sharing the code here.
public class vvn {
public static void main(String[] args)
{
byte [] arr = new byte[4];
arr[0] = (byte)157;
arr[1] =…

optimus prime
- 764
- 1
- 13
- 39
3
votes
3 answers
Java bit unsigned shifting (>>>) give strange result
I have this code:
int i = 255;
byte b = (byte) i;
int c;
System.out.println(Integer.toBinaryString( i));
System.out.println("b = " + b); // b = -1
c=b>>>1;
System.out.println(Integer.toBinaryString( c));
System.out.println(c);
But I can't…

mechanikos
- 771
- 12
- 32
3
votes
4 answers
function that takes a long value and returns with its bytes in reverse order in C
I am doing a homework that requires me to write a function that takes a long value and returns with its bytes in reverse order in C, the prototype for the function is given, which is
long swapLong(long x)
and my code looks like this :
long…

Ted Woo
- 33
- 2
2
votes
3 answers
Help with byte shifting
I need to byte-shift a text file. I know absolutely nothing about perl, but I found a perfectly working piece of code in perl called moz-byteshift.pl (documentation). This does exactly what I want to do, but I need to do it in C#.
Here's the…

Andrew Ensley
- 11,611
- 16
- 61
- 73
2
votes
1 answer
Byte order functions on stm32 microcontroller
Are there any written functions for htonll,ntohll,ntohl,htonl like this one i've found here on forum:
uint32_t ntohl(uint32_t const net) {
uint8_t data[4] = {};
memcpy(&data, &net, sizeof(data));
return ((uint32_t) data[3] << 0)
…

mrunje
- 85
- 1
- 6