Conversion of unsigned types in Java __java

Source: Internet
Author: User
Tags bitwise modifiers numeric value
Conversion of unsigned types in Java

Turn from: http://hi.baidu.com/jrckkyy/blog/item/9c813735b614a30891ef39a2.html

In Java, there is no unsigned unsigned data type, but the unsigned conversion can be done easily.

Scenario One: if flow (stream) data processing in Java, you can use the DataInputStream class to read the data in the stream unsigned.

Java provides support in this regard by using the Java.io.DataInputStream class object to complete the unsigned read of data in the convection, which provides the following methods:
(1) int readunsignedbyte ()//reads a 0~255 (0xFF) of data from the stream and returns it as an int data type. The returned data is equivalent to the so-called "BYTE" in the C + + language.
(2) int readunsignedshort ()//reads a 0~65535 (0xFFFF) of double-byte data from the stream and returns with data of type int. The returned data is equivalent to the so-called "WORD" in the C + + language and is returned in a "low address low byte" way, so programmers do not need additional conversions.

Scenario Two: Use the Java bitwise operator to complete the unsigned transformation.

Normally, the data types provided by Java are signed signed types, and can be obtained by bitwise operations of their corresponding unsigned values, see the code in several methods:

The public int getunsignedbyte (byte data) {////////0~255 (0xFF, or byte).
Return data&0x0ff;
}

public int Getunsignedbyte (short data) {//Convert data byte to 0~65535 (0xFFFF is WORD).
Return data&0x0ffff;
}

public long Getunsignedintt (int data) {//converts int data to 0~4294967295 (0xFFFFFFFF is DWORD).
Return data&0x0ffffffff;
}

Flexible use of these techniques, there is no "binary in Java in the full support of" the argument.

Http://hi.baidu.com/topjava/blog/item/723fed881df97299a5c2722b.html

In Java, there are only int types and no unsign integer and signed.

I have a problem now, a file turned into a inputstream
Original 8bit bit,32bit unsigned integer and signed integer
How should you turn to the basic data type in Java.

Now people tell me the following rules, but I will not turn, please help
Java type
8bit unsigned integer---> short
8bit signed integer---> byte
16bit unsigned integer---> int
16bit signed integer---> short
32bit unsigned integer---> long
32bit signed integer---> int

Comparison collection of basic data types between java,c,c++ languages

These problems are certain to occur when the underlying porting is to be carried out. Special sorting out the next.


Java language Basic data type

There are eight basic data types in Java, respectively

BYTE, short, int, long, float, double, char, Boolean

Integral type

Where byte, short, int, and long all represent integers, except that they have different ranges of values.

Byte value range is -128~127, occupies 1 bytes (-2 of 7 times to 2 of 7 Times Square-1)

The short value range is -32768~32767, occupies 2 bytes (-2 of 15 times to 2 of 15 Times Square-1)

int has a value range of ( -2147483648~2147483647), occupies 4 bytes (-2 of 31 times to 2 of 31 times-1)

Long takes a range of ( -9223372036854774808~9223372036854774807), takes up 8 bytes (-2 of 63 to 2 of 63 times-1)

You can see that the range of byte and short values is small, and long is too large, takes up more space, and basically an int satisfies our daily calculations, and int is the most integral type to use.

In general, if there is an integer number such as 35 in Java, then the number is int, and if we want it to be byte, we can add a b:35b to the data, indicating that it is byte, and that the same 35S represents the short type, 35L represents a long type, int we can add nothing, but if you want to represent a long type, you must add "L" to the data.

Floating-point type

float and double are data types that represent floating-point type, and the difference between them is that their accuracy is different

Float 3.402823e+38 ~ 1.401298e-45 (e+38 is multiplied by 38 times 10, likewise, e-45 is multiplied by 10 minus 45) to occupy 4 bytes

Double 1.797693e+308~ 4.9000000e-324 occupies 8 bytes

The double type is larger and more accurate than the float type, so the usual floating-point data is double with no declaration, and if you want to indicate that a data is float, you can add "F" behind the data.

Floating-point data is not exactly accurate, so it's normal to have a float at the end of the decimal point at the time of calculation.

Boolean (Boolean type)

This type has only two values, True and False (true and not true)

Boolean t = true;

Boolean f = false;

Char type (text type)

The data type used to hold the character, occupies 2 bytes, is Unicode encoded, and its first 128-byte encoding is compatible with ASCII

Characters are stored in \u0000~\uffff, and when defining character data, be aware that ", for example, ' 1 ' means the character ' 1 ' instead of the value 1,

char c = ' 1 ';

We try to output C look, System.out.println (c), the result is 1, and if we do this output System.out.println (c+0);

It turned out to be 49.

If we define C like this, look at

char c = ' \u0031 '; The result of the output is still 1, because the character ' 1 ' corresponds to the Unicode encoding is the \u0031

char c1 = ' h ', c2 = ' e ', c3= ' l ', c4= ' l ', c5 = ' o ';

System.out.print (C1); System.out.print (C2); System.out.print (C3); System.out.print (C4); Sytem.out.print (C5);

String

We've seen this in the previous definition:

String s = "Hello";

System.out.println (s); the effect is the same as the 5 statements above, so what is a string? String is a string, it is not a basic data type, it is a class.


C Language Data type

1. Integer data type
C defines 5 integral types of data.
Integer Data type table

Serial number Type name Description Number of bytes Take value range
1 Signed Char Signed Single-byte integer types 1 -128~+127
2 Short int Short integral type 2 -32768~+32767
3 Int Integral type 4 -2147438648~+2147438647
4 Long int Long Integral type 4 -2147438648~+2147438647
5 Long Long int Long-length integer 8 -9223372036854775808~+-9223372036854775807

2. Unsigned integer type
There are also unsigned integer types for the due type.
unsigned integer type table

Serial number Type name Number of bytes Take value range
1 unsigned char 1 0~255
2 unsign Short int 2 0~65535
3 unsigned int 4 0~4294967295
4 unsigned long int 4 0~4294967295
5 unsign Long int 8 0~18446744073709551615

3. Integral type Constants
An integer constant is a constant that is used to represent an integer value, divided into short int, int, long int, and long int, which are four kinds. C default Integer (int). All types of integer constant feed representation table (suffix case-insensitive)

Serial number Data type Octal Decimal Hexadecimal
1 Integral type 0112 74 0x4a
2 Long-integer (L) 0112l 74l 0x4al
3 Long-length int (LL) 0112ll 74ll 0x4all
4 unsigned integral type (u) 0112u 74u 0x4au
5 unsigned long integral type (UL) 0112ul 74ul 0x4aul
6 unsigned long-length integer (ull) 0112ull 74ull 0x4aull

4. Character data type
There is only one type of character data in the C language, that is, char type data. Char is also commonly referred to as a character type directly. Characters occupy the least amount of memory space, typically taking up one byte, and integers stored in a char type variable can be represented as signed or unsigned values, depending on the compiler.

5. Character variables
A character variable is a variable that is used to store character numeric values. There are two types of character variables: signed and unsigned.

6. Floating-point data type
The C language defines three kinds of floating-point data types:
Float, single precision
double, double precision
long double, long double precision
The C standard has different rules for different types of floating-point numbers, and different compiler or hardware conditions, byte length is not the same.

Floating-point byte length, precision, order of magnitude range, and output input format table

Serial number Data type byte length Precision Order of magnitude range printf and scanf formats
1 Float (f) 4 7 -38~38 %f
2 Double 8 About 16 -308~308 %f
3 Long double (1) 12 About 19 -4932~4932 %llf

7. Floating-point precision
Floating-point precision is arranged from low to high to float, double, and long long double.

8. Float Type storage mode
Floating-point values are stored in memory in the form of scientific notation notation. The floating-point memory form consists of three parts:
1) Sign Bit
Symbol bit floating-point type has only one digit and is the highest bit. The bit is 1, representing a negative number, which is 0, and is not a negative number.
2) Exponential position
The exponential bit of floating-point type is stored in the complement form, which is the exponent part of the scientific counting method.
3) Base Position
The base bit is the last one of the floating-point type, which determines the precision of the numeric value.
Floating-point Storage segment Table

Serial number Data type Sign bit Point Digit Base bit Deviation value
1 Float 1 8 23 127
2 Double 1 11 52 1023
3 Long double 1 15 64 16383
C + + language basic data type

C + + basic data types have character type (char), integer type (int), float (float), double (double), and no value type (void), the following table gives the various base types and the number of bits in memory and the range that represents the values (ASCII code is stored for the character type):

Basic data Types

Type Type name Number of digits Range
Character type Char 8 -128------127
Integral type Int 16 -32768------32767
Floating-point type Float 32 3.4E-38------3.4E+38
Double-precision Double 64 1.7E-308------1.7E+308
No value type void 0 0

Type names can be decorated with modifiers (except void types), modifiers with signed (signed), unsigned (unsigned), short (shorter), and long (longer). Signed and unsigned are used for character or integral types, short and long can be used for integral types, and can be used for doubles, as shown in the following table:

basic data types and modifiers

Type Type name Number of digits Range
Character type Char 8 -128------127
Signed Char 8 -128------127
unsigned char 8 0------255
Integral type Int 16 -32768------32767
Signed int 16 -32768------32767
unsigned int 16 0------65535
Short int 16 -32768------32767
Signed Short int 16 -32768------32767
unsigned short int 16 0------65535
Long int 32 -2,147,483,648----2,147,483,647
Signed Long int 32 -2,147,483,648----2,147,483,647
unsigned long int 32 0----------------4,294,967,295
Floating-point type Float 32 3.4E-38------3.4E+38
Double-precision Double 64 1.7E-308------1.7E+308
Long double 80 3.4E-4932------1.1E+4932

int can be omitted when the int is decorated with signed, unsigned, short, and long.

The character type is actually a byte integral type , and floating-point and double-precision representations of real numbers.

Note: The storage lengths of integers are different for different compilation systems. For a 16-bit compilation system, int is 2 bytes, and for 32-bit compilation systems, int is 4 bytes. You can test with the sizeof operator. Such as:

cout<<sizeof (int) <<endl;

The output is the number of bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.