# Byte size shortened

**URL:** <https://discourse.processing.org/t/byte-size-shortened/13765>\
**Category:** Coding Questions\
**Created:** [September 2, 2019, 12:04pm UTC](https://discourse.processing.org/t/byte-size-shortened/13765 "2019-09-02T12:04:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![moimat56](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/moimat56/32/6491_2.png) [@moimat56](https://discourse.processing.org/u/moimat56)\
**Post date:** [September 2, 2019, 12:04pm UTC](https://discourse.processing.org/t/byte-size-shortened/13765/1 "2019-09-02T12:04:04Z")

</div>

Hello, i’m trying to rebuild a float number sent on a serial line.  
I had trouble working on a bitwise operation.

Code :

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/871195d5be4543e67302672057dcff164cf4b824.png)

I expected a byte to be **8bits** of length but I can only fill the array with **4bits** values (Ex : 0xF)

See the byte processing reference :

> Datatype for bytes, 8 bits of information storing numerical values from 127 to -128. Bytes are a convenient datatype for sending information to and from the serial port and for representing letters in a simpler format than the **char** datatype. The first time a variable is written, it must be declared with a statement expressing its datatype. Subsequent uses of this variable must not reference the datatype because Processing will think the variable is being declared again.

So my question is _ **why the byte definition declares a 4bits variable** _ ?

Thank you.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 2, 2019, 1:39pm UTC](https://discourse.processing.org/t/byte-size-shortened/13765/2 "2019-09-02T13:39:48Z")

</div>

- [Forum.Processing.org/two/discussion/12757/transmission-of-a-float-through-a-serial-link#Item\_5](http://Forum.Processing.org/two/discussion/12757/transmission-of-a-float-through-a-serial-link#Item_5)

```auto
// https://Discourse.Processing.org/t/byte-size-shortened/13765/2
// GoToLoop (2019/Sep/02)

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

static final int BYTES = Float.BYTES;

final byte[] bytes = new byte[BYTES];
final FloatBuffer buf = ByteBuffer.wrap(bytes).asFloatBuffer();

float f;

void setup() {
  println(buf.order(), java.nio.ByteOrder.nativeOrder(), ENTER);

  bytes[0] = (byte) 0xf0;
  bytes[1] = (byte) 0xaa;
  bytes[2] = (byte) 0xf8;
  bytes[3] = (byte) 0x30;

  println(bytes);
  println();

  f = bytes[0] << 030 | bytes[1] << 020 | bytes[2] << 010 | bytes[3];
  println(f); // -2000.0

  f = buf.get(0);
  println(f); // -4.2329994E29

  exit();
}

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [September 3, 2019, 9:10am UTC](https://discourse.processing.org/t/byte-size-shortened/13765/3 "2019-09-03T09:10:21Z")

</div>

> [@moimat56](#):
>
> I expected a byte to be **8bits** of length but I can only fill the array with **4bits** values (Ex : 0xF)

Actually you can use up to `0x7F`, which is the hex value for 127. And you can use `-0x80`, hex for -128. So the entire signed range of the byte is definable in hex, it just doesn’t work the way you expect it to.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 3, 2019, 9:49am UTC](https://discourse.processing.org/t/byte-size-shortened/13765/4 "2019-09-03T09:49:12Z")

</div>

```auto
println("JAVA number range");
println("System : " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") );
println("JAVA : " + System.getProperty("java.home") + " rev: " +javaVersionName);

println();
println("byte min: "+Byte.MIN_VALUE+ "\t\t\t max: "+Byte.MAX_VALUE);
println("short min: "+Short.MIN_VALUE+ "\t\t\t max: "+Short.MAX_VALUE);
println("int min: "+Integer.MIN_VALUE+"\t\t max: "+Integer.MAX_VALUE);
println("long min: "+Long.MIN_VALUE+ "\t max: "+Long.MAX_VALUE);
println("float min: "+Float.MIN_VALUE+ "\t\t\t max: "+Float.MAX_VALUE);
println("double min: "+Double.MIN_VALUE+ "\t\t\t max: "+Double.MAX_VALUE);

```

---

<div class="post-metadata">

**Author:** ![moimat56](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/moimat56/32/6491_2.png) [@moimat56](https://discourse.processing.org/u/moimat56)\
**Post date:** [September 13, 2019, 11:37am UTC](https://discourse.processing.org/t/byte-size-shortened/13765/5 "2019-09-13T11:37:36Z")

</div>

Why is it impossible to complete a byte with a value superior to 0x7F ?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 13, 2019, 2:20pm UTC](https://discourse.processing.org/t/byte-size-shortened/13765/6 "2019-09-13T14:20:09Z")

</div>

```auto
println(Byte.MIN_VALUE, Byte.MAX_VALUE, Byte.SIZE); // -128, 127, 8
println(hex(Byte.MIN_VALUE), hex(Byte.MAX_VALUE), (byte) 0x80); // 80, 7F, -128
exit();

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [September 13, 2019, 7:05pm UTC](https://discourse.processing.org/t/byte-size-shortened/13765/7 "2019-09-13T19:05:23Z")

</div>

> [@moimat56](#):
>
> Why is it impossible to complete a byte with a value superior to 0x7F ?

Because that is the highest number you can store in a signed byte. Add 1 and you get -0x80. Hex literals are still signed.

---

<div class="post-metadata">

**Author:** ![moimat56](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/moimat56/32/6491_2.png) [@moimat56](https://discourse.processing.org/u/moimat56)\
**Post date:** [September 21, 2019, 9:07am UTC](https://discourse.processing.org/t/byte-size-shortened/13765/8 "2019-09-21T09:07:49Z")

</div>

Thank you, i decided to simply send all the data under string format. There is finally no need to be this fast in the transmission.

Also I really like your work, keep up !
