Hexadecimal: Set an 8-bit byte in an int

Myron King

In Java:

I have a 32-bit number given to me in hexadecimal form. I'm given a specific byte number (0-3, 0 for least-significant byte) and told that I need to replace that byte with another byte also given to me in hexadecimal form. Ex: 32-bit number 0xAAA5BBC6, replace byte 1 with 0x17 to get 0xAAA517C6.

I can't use any casting, multiplication, addition, subtraction, or conditionals. I cannot write any helper methods, or call any other method from this or another file to implement any method. Additionally, this has to be written in one line of code.

I believe I should be using masking, but I can't figure out where to start. Given a byte number, how can I change all 8 bits. Toggling one off or on is easy, but toggling all 8 bits to be a certain value?

morgano

In one line, assuming that bytes are being counted from the least significative byte:

int updatedValue = originalValue & ~(0xFF << (byteNumber << 3)) | ((((int)newByte) & 0xFF) << (byteNumber << 3));

Where:
originalValue is your original 32-bit integer
newByte is the byte you are given to replace the old byte
byteNumber is the byte number (0-3)

what the code does is the following:

  • create a mask to "delete" the old byte (clear the bits of this byte). In order to create the mask:

    • create a mask of a byte of all bits set (all in 1) 0xFF
    • offset this mask to the position of the byte to be "deleted", it has to be 8 times the number of the byte to be "deleted". As I can't multiply (part of your restrictions), then I offset this number 3 bits to the left (which is equivalent to multiply by 8, remember that displacing the bits one position to the left is equilavent to multiply by 2, so an offset by 3 will be 2 * 2 * 2 = 8) this is done by this piece of code: (byteNumber << 3)
    • "toggle" the bits of the mask with ~, so I have the mask to "delete" the byte: ~(0xFF << (byteNumber << 3)) at this point you mask would be, say FFFF00FF if you wanted to clear byte #1
  • perform an and-bit-wise operation between the original number and the mask created in the first step: ~(0xFF << (byteNumber << 3))

  • create a 32bit integer with the new byte and offset its bits to the position of the byte. Again, the offset is done with (byteNumber << 3) which has already been explained.
  • perform an or bit-wise operation with the result of the second step to set the bits of the new byte (this is the line of code, the final step)

Now, the reason for which I'm doing ((int)newByte) & 0xFF) instead of just ((int)newByte)) or just newByte, is that the JVM promotes to int the operator byte before doing the operation <<, this can have undesired effects if your newByte is bigger than 0x7F (for instance, a value of 0x80 would be cast to int as 0xFFFFFF80 instead of 0x00000080). By doing ((int)newByte) & 0xFF) I'm doing the promotion to int by myself and clear undesired bits just in case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set Specific Bit in Byte Array

From Dev

Byte conversion from 32 bit to 8 bit

From Dev

Set a specific bit in an int

From Dev

Why is the default alignment for `int64_t` 8 byte on 32 bit x86 architecture?

From Dev

Byte array to an 8 bit truecolor image

From Dev

Comparison of byte value and hexadecimal

From Dev

Compare a byte and hexadecimal

From Dev

Get last set bit position for an int

From Dev

Convert a 64 bit integer into 8 separate 1 byte integers in python

From Dev

How to get 8 byte pointer value on 64 bit machine on gdb?

From Dev

How to convert a 8-bit number to an actual byte in Python?

From Dev

Regular expression for validating 64 bit (8 byte) MAC addresses?

From Dev

PHP convert 64 bit printable number to 8 byte binary string?

From Dev

Consider 8th Bit in Python Byte to make Postive Integer

From Dev

Java: Convert a hexadecimal encoded String to a hexadecimal byte

From Dev

Check if byte has more than one bit set

From Dev

8 bit int vs 32 bit int on a 32 bit architechture (GCC)

From Dev

Converting a hexadecimal String to byte array

From Dev

converting hexadecimal string to byte array

From Dev

Adding one byte to a hexadecimal number

From Dev

Converting a hexadecimal String to byte array

From Dev

Java: Simply convert a decimal byte to a hexadecimal byte

From Dev

Reading out a 16bit timer on an 8bit system without a latch for the high/low byte

From Dev

Storing a hexadecimal number in an int

From Dev

how to convert hexadecimal to int

From Dev

How Int8 (byte) operations can be useful for deep learning?

From Dev

How to convert Int to 8-byte little-endian

From Dev

How to split an unsigned long int (32 bit) into 8 nibbles?

From Dev

Bit expand byte array

Related Related

  1. 1

    Set Specific Bit in Byte Array

  2. 2

    Byte conversion from 32 bit to 8 bit

  3. 3

    Set a specific bit in an int

  4. 4

    Why is the default alignment for `int64_t` 8 byte on 32 bit x86 architecture?

  5. 5

    Byte array to an 8 bit truecolor image

  6. 6

    Comparison of byte value and hexadecimal

  7. 7

    Compare a byte and hexadecimal

  8. 8

    Get last set bit position for an int

  9. 9

    Convert a 64 bit integer into 8 separate 1 byte integers in python

  10. 10

    How to get 8 byte pointer value on 64 bit machine on gdb?

  11. 11

    How to convert a 8-bit number to an actual byte in Python?

  12. 12

    Regular expression for validating 64 bit (8 byte) MAC addresses?

  13. 13

    PHP convert 64 bit printable number to 8 byte binary string?

  14. 14

    Consider 8th Bit in Python Byte to make Postive Integer

  15. 15

    Java: Convert a hexadecimal encoded String to a hexadecimal byte

  16. 16

    Check if byte has more than one bit set

  17. 17

    8 bit int vs 32 bit int on a 32 bit architechture (GCC)

  18. 18

    Converting a hexadecimal String to byte array

  19. 19

    converting hexadecimal string to byte array

  20. 20

    Adding one byte to a hexadecimal number

  21. 21

    Converting a hexadecimal String to byte array

  22. 22

    Java: Simply convert a decimal byte to a hexadecimal byte

  23. 23

    Reading out a 16bit timer on an 8bit system without a latch for the high/low byte

  24. 24

    Storing a hexadecimal number in an int

  25. 25

    how to convert hexadecimal to int

  26. 26

    How Int8 (byte) operations can be useful for deep learning?

  27. 27

    How to convert Int to 8-byte little-endian

  28. 28

    How to split an unsigned long int (32 bit) into 8 nibbles?

  29. 29

    Bit expand byte array

HotTag

Archive