Simple Machine

util
Class UnsignedByte

java.lang.Object
  extended by util.UnsignedByte

public class UnsignedByte
extends Object

An UnsignedByte is like a Byte, but its values range from 0 to 255 instead of -128 to 127.

Most languages have a native unsigned-byte type (e.g., C, C++, C#), but Java doesn't.

When manipulating bytes as bit sequences, as we do in the CPU implementation, it is helpful to treat them as unsigned. The key difference between unsigned and signed in our context is what happens when you assign an (unsigned) byte to an integer. If a signed byte is assigned, then the resulting integer is sign extended (i.e., 0xff becomes 0xffffffff). If an unsigned byte is assigned, then the resulting integer is zero extended (i.e., 0xff becomes 0x000000ff). The former behaviour is what you want if the program thinks of the byte as a signed integer (i.e., -1 stays -1). The later is what you want if the program (in our case the CPU class) thinks of the byte as a sequence of bits.


Constructor Summary
UnsignedByte(Byte aByte)
          Create an unsigned byte from a byte.
UnsignedByte(int anInt)
          Create an unsigned byte from an integer.
 
Method Summary
 boolean equals(Object o)
          Like Java Numbers, two UnsignedByte's are equal if their values are equal.
 long value()
          Get the value of the unsigned byte.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnsignedByte

public UnsignedByte(Byte aByte)
Create an unsigned byte from a byte.


UnsignedByte

public UnsignedByte(int anInt)
Create an unsigned byte from an integer.

Method Detail

value

public long value()
Get the value of the unsigned byte.

Returns:
zero-extended long version of the unsigned byte's value.

equals

public boolean equals(Object o)
Like Java Numbers, two UnsignedByte's are equal if their values are equal.

Overrides:
equals in class Object

Simple Machine

Copyright © 2010, 2011 Mike Feeley. All Rights Reserved.