Byte swapping in java

Java program to swap two nibbles in a byte

In this tutorial, we will learn to swap two nibbles in a byte and also write its code in Java.

A byte is a combination of 8 bits and one nibble consists of 4-bits. Hence every byte has two nibbles.

For example, The number 150 can be represented as 10010110 in binary. 1001 is the upper nibble whereas 0110 is the lower nibble

after swapping both the nibbles we get 01101001 which is 105 in decimal.

Let’s see how we can swap the two nibbles of a byte:

  1. Perform bitwise AND operation with hexadecimal 0F to separate the lower nibble from the byte
  2. Perform bitwise AND operation with hexadecimal F0 to separate the upper nibble from the byte
  3. Take the right nibble and left shift by 4 positions
  4. Take the left nibble and right shift >> by 4 positions
  5. Finally, combine the nibble using the bitwise OR operator

Java program to swap two nibbles in a byte

We can combine all these five steps in a single line i.e. (num & 0x0F) > 4
where (num & 0F) separates the lower nibble and left shift by 4 positions and (num & 0xF0) >> 4 separates the upper nibble and right shift by 4 positions and finally we OR both of them to get the desired result

Java program to swap two nibbles of byte

Following is the code implementation in Java.

public class SwapNibble < public static void main(String[] args) < int num = 150; int swapnum; swapnum = ((num & 0x0F) > 4); System.out.println("Before swapping the nibble "+ num); System.out.println("After swapping the nibble "+ swapnum); > >

The output of the following program is

Before swapping the nibble 150 After swapping the nibble 105

Источник

Swap two nibbles in a byte

A nibble is a four-bit aggregation or half an octet. There are two nibbles in a byte.
Given a byte, swap the two nibbles in it. For example, 100 is represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal.

Method 1: To swap the nibbles, we can use bitwise &, bitwise ” operators. A byte can be represented using an unsigned char in C as size of char is 1 byte in a typical C compiler.

Below is the implementation of the above idea.

C++

C

Java

Python3

C#

PHP

Javascript

Time Complexity: O(1)
Auxiliary Space: O(1)

Explanation:
100 is 01100100 in binary. The operation can be split mainly in two parts
1) The expression “x & 0x0F” gives us last 4 bits of x. For x = 100, the result is 00000100. Using bitwise ‘2) The expression “x & 0xF0” gives us first four bits of x. For x = 100, the result is 01100000. Using bitwise ‘>>’ operator, we shift the digit to the right 4 times and make the first four bits as 0. The result after shift is 00000110.
At the end we use the bitwise OR ‘|’ operation of the two expressions explained above. The OR operator places first nibble to the end and last nibble to first. For x = 100, the value of (01000000) OR (00000110) gives the result 01000110 which is equal to 70 in decimal.
This article is contributed by Anuj Garg. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Another Approach:

Using binary instead of hexadecimal values. It is much clearer to beginners.

  • Take & of 00001111 with number to get right nibble i.e. 0b00001111 & N
  • ake & of 11110000 with number to get left nibble i.e. 0b11110000 & N
  • Left shift the right nibble obtained in step 1 by 4 positions to get it as left nibble in the final answer i.e.
  • Right shift the left nibble obtained in step 2 by 4 positions to get it as right nibble in final answer >>4
  • Do or( | ) operation between values obtained in step 3 & 4 to get the answer

Below is the implementation of the above approach:

Источник

Java Bit Byte swap an array of floats.

Byte swap an array of floats. The result of the swapping is put back into the specified array.

The method source code is listed as follows:

/** * Byte swap an array of floats. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(float[] array) < for (int i = 0; i

The complete source code is listed as follows:

//package com.java2s; public class Main < /**/* w w w . d e m o 2 s . c o m*/ * Byte swap an array of floats. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(float[] array) < for (int i = 0; i < array.length; i++) array[i] = swap(array[i]); >public static short swap(short value) < int b1 = value & 0xff; int b2 = (value >> 8) & 0xff; return (short) (b1 /** * Byte swap a single int value. * * @param value Value to byte swap. * @return Byte swapped representation. */ public static int swap(int value) < int b1 = (value >> 0) & 0xff; int b2 = (value >> 8) & 0xff; int b3 = (value >> 16) & 0xff; int b4 = (value >> 24) & 0xff; return b1 /** * Byte swap a single long value. * * @param value Value to byte swap. * @return Byte swapped representation. */ public static long swap(long value) < long b1 = (value >> 0) & 0xff; long b2 = (value >> 8) & 0xff; long b3 = (value >> 16) & 0xff; long b4 = (value >> 24) & 0xff; long b5 = (value >> 32) & 0xff; long b6 = (value >> 40) & 0xff; long b7 = (value >> 48) & 0xff; long b8 = (value >> 56) & 0xff; return b1 /** * Byte swap a single float value. * * @param value Value to byte swap. * @return Byte swapped representation. */ public static float swap(float value) < int intValue = Float.floatToIntBits(value); intValue = swap(intValue); return Float.intBitsToFloat(intValue); > /** * Byte swap a single double value. * * @param value Value to byte swap. * @return Byte swapped representation. */ public static double swap(double value) < long longValue = Double.doubleToLongBits(value); longValue = swap(longValue); return Double.longBitsToDouble(longValue); > /** * Byte swap an array of shorts. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(short[] array) < for (int i = 0; i < array.length; i++) array[i] = swap(array[i]); >/** * Byte swap an array of ints. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(int[] array) < for (int i = 0; i < array.length; i++) array[i] = swap(array[i]); >/** * Byte swap an array of longs. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(long[] array) < for (int i = 0; i < array.length; i++) array[i] = swap(array[i]); >/** * Byte swap an array of doubles. The result of the swapping * is put back into the specified array. * * @param array Array of values to swap */ public static void swap(double[] array) < for (int i = 0; i < array.length; i++) array[i] = swap(array[i]); >>

  • Java Bit Byte swap a single int value.
  • Java Bit Byte swap a single long value.
  • Java Bit Byte swap an array of doubles.
  • Java Bit Byte swap an array of floats.
  • Java Bit Byte swap an array of ints.
  • Java Bit Byte swap an array of longs.
  • Java Bit Byte swap an array of shorts.

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

To swap the byte in long using bit

send pies

posted 17 years ago

  • Report post to moderator
  • Hello,
    How can i swap the first and last byte in long datatype?Swap should be done using bit operation.
    Can i get the link related to such program please

    send pies

    posted 17 years ago

  • Report post to moderator
  • I haven’t tested the code but you should be able to do something like:

    Edit — now that I re-read this I may not fully understand your question — what do you mean «using bit»
    [ May 04, 2006: Message edited by: Scott Dunbar ]

    send pies

    posted 17 years ago

  • Report post to moderator
  • Hi,
    Thanks for your reply..
    The swap of bytes should be done using bit.

    send pies

    posted 17 years ago

  • Report post to moderator
  • long is 64 bits (8 bytes), so the identity mask in hex is:

    Now we want the first byte and last byte, so let me store these masks:

    Then we need to make the first byte the last and the last the first (I’m assuming your input is stored in a variable called input):

    I think that should do the trick. all we’ve done is to take the input, mask out the first and last two bytes, swap them by bit shifting, and then added the result back in (ignoring the previous first and last bytes).

    It would be cleaner if there were some way to simply ‘wrap round’ the bits when shifting, since then a shift of 8 bits would simply cause the first and last bytes to be swapped, but to my knowledge Java doesn’t support this.

    Let me know if this works (I haven’t actually tried it) and answers your question.

    Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
    Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )

    Источник

    Читайте также:  Алгоритм проверки инн python
    Оцените статью