• Post Reply Bookmark Topic Watch Topic
  • New Topic

help clarify signed, long, int  RSS feed

 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey everyone,
i know this seems a bit sad, but i need to get these all straight. my assignment states that my class must handle 'signed binary integer numbers...' in C++, we only worked with 'int x', or 'double y' and didn't really touched 'signed', 'unsigned' and 'long'. does signed and unsigned refer to positive and negative numbers? if so, do you declare 'signed long' or would it be 'signed long int' or 'signed long double'?
hope these questions make sense.

plus, one more question. I looked up a function on the Sun website and it's called 'toBinaryString'. if i want to use this in one of my classes, would this be correct:
toBinaryString(long i);
plus, do I need to import anything to use this?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, with the exception of boolean and char, all of the primitive types are signed. There is no unsigned int, for example.

does signed and unsigned refer to positive and negative numbers?

Kind of. If the data type is signed, then it is used to represent a range of numbers, from some minimul negative value to some maximum positive value.

If the data type is unsigned, then it is used to only represent a range of numbers starting from zero to some maximum positive value.
[ September 12, 2004: Message edited by: Dirk Schreckmann ]
 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Dirk--but what does 'signed' MEAN?

sorry, for some reason, your post hadn't loaded. nevermind above question. so you cannot create 'unsigned long' or 'unsigned int'? all i know is that long's value is from -2 ^ 63 to 2^63 -1. so can you declare something 'long i' like 'int i'?

thanks for the patience.
[ September 12, 2004: Message edited by: Anne Poe ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
plus, one more question. I looked up a function on the Sun website and it's called 'toBinaryString'. if i want to use this in one of my classes, would this be correct:
toBinaryString(long i);


Did you try it? Did it work? If not, is the error message giving you enough of a clue as to how to get things working?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does 'signed' MEAN?

It means that the data type is used to represent positive and negative values.

In the case of your program requirements, where you must "handle signed binary integer numbers," perhaps it means that you must handle input values, such as -101010 and +11011 (maybe without the + character).
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, in java all numbers are implicitly signed. what this means, is that each of the types of integers in java (byte, short, int, long) and the floats (float, double) can be expressed as positive or negative. Java uses the left-most bit of the number expressed in binary to hold the 'sign' 0 for a positive number, 1 for a negative number. In C, you can declare an number 'unsigned', so that an integer can hold a greater value. Example:


Also, names like 'byte', 'short', 'int', 'long', etc., are just ways in java to tell how many bits an integer can hold. A byte holds 8 bits, a
short holds 16, int 32, long 64. So, a long can hold a much larger number
than a byte, but takes up 8 times as much memory.
In Java, you can not declare an integer or float to be signed or unsigned, they are always signed. The primitive type 'char', however, is actually
an unsigned 16-bit integer (short), which means it can hold any number between 0-65535 (no negative numbers).

Lastly, if memory serves, toBinaryString is a method found in each of the 'wrapper' Classes for Java integers. If you wanted a long's binary string, you would use the toBinaryString(long i) method from the Class java.lang.Long, (Byte for byte, Short for short and Integer for int). I could be wrong about that, though, its been a while, and I don't have access to a jdk at the moment.
 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's my testing code:



here's 1 error i keep getting:
BinaryTest.java:11: cannot find symbol
symbol : method toBinaryString(long)
location: class BinaryTest
int x = toBinaryString(i);
^
1 error

I've tried changing int x to String x, but same error. Only thing I can think of is (in all my other examples), I've had to create an object (say: Binary object = new Binary() ;) and then say object.toBinaryString(i);
However, is that really needed? (I'll test it out, but I'm beyond clueless here).

(Marilyn added code tags)
[ September 12, 2004: Message edited by: Marilyn de Queiroz ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.lang.Long;

Note that all Classes, Interfaces and enums in the java.lang package are automatically imported. So, you don't have to explicitly do so.

Note that the toBinaryString(long) method is a static member of the Long class - i.e. it belongs to the Long class.

If you want to use it, you could use the following syntax.

Long.toBinaryString(myLong)

If you are using Java 2 v5, then you could declare a static import of the static members in the Long class

import static java.lang.Long.*;

and then use the following syntax to use this method.

toBinaryString(myLong)

Only thing I can think of is ... I've had to create an object and then say object.toBinaryString(i); However, is that really needed?

Not in this case, since the toBinaryString(long) method is a static member of the java.lang.Long class. If it were a (non-static) instance member, then you would have to create an instance (object) of the Long class in order to use it.

Making sense?
 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the explanation, Dirk.
Will test out ina couple of hours and let you know here
 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Dirk--
this is my program. i have one question. in C++, when you called a function, you had to store the answer in another variable. i.e., in C++, this'd be acceptable:
String c = toBinaryString(i);

however, if i use this for Java, would this be acceptable:
String c = toBinaryString(i); //say, i =52

OR just: toBinaryString(i) and use a getter function.

I am just testing this as we speak.
 
Anne Poe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind, no need to answer this. i've got it figured out, thanks for the replies!
i think i like java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!