Make calculations with hexadecimal/decimal numbers - java

I have the rather simple method naf
public int naf(int NN, int AA, int FF, int Q) {
int mike = FF + 0x20 * AA + 0x200 * NN;
if (Q == 1) {
mike += 0x4000;
}
return mike;
}
I can understand that this method accepts 4 integers and returns an integer. In the second line, the calculations are a bit confusing. I feel that the 0x parts have to do with hexadecimal number format, but the input arguments are in decimal. I have also the feeling that the 0x may make those calculations doable, but I am not certain.
What does the 0x offer and how can the method make hexadecimal calculations, when the input is decimal?

There's no such thing as hexadecimal calculations.
You have an integer expression, it is just that in it
two numbers are represented in hexadecimal format.
This
int mike = FF + 0x20*AA + 0x200 * NN;
is the same as:
int mike = FF + 2 * 16 * AA + 2 * 16 * 16 * NN;
which is the same as:
int mike = FF + 32 * AA + 512 * NN;

For a computer it doesn't matter how numbers are presented to you, be it decimal (base 10, as we're naturally familiar with), hexadecimal (base 16), octal (base 8), binary (base 2), you name it (though in Java only octal, decimal and hexadecimal are possible out of the box). Internally they are all stored as binary numbers, that's a series of zeros and ones.
In your code you are free to choose the format you feel most comfortable with. By default the Java compiler assumes you are feeding it decimal numbers, but by prefixing your number either with 0x or only 0 you tell it to interpret them as hexadecimal or octal numbers, respectively.
I suggest you try to read up on numeral systems.

Integer literals in java can be in the form of decimal (eg. 32), hexadecimal (eg. 0x20) and octal (eg. 040). The differences being decimal: no prefix, hexadecimal: prefix 0x and octal: prefix 0.
Thus,
int mike = FF + 0x20 * AA + 0x200 * NN;
is really just
int mike = FF + 32 * AA + 512 * NN;
Oh and the fact that FF is also on "hex form" doesn't matter at all, it's just a method parameter and might as well be called steven... :-)
Cheers,

Decimal or hexadecimal only applies when you express literals, e.g.:
int a = 0xfa; // hexadecimal notation for literal on the right hand side
int b = 251; // decimal notation for the literal on the right hand side
Once the values have been stored in variables the system doesn't care if you used decimal or hexadecimal notation to express the values that you stored:
int c = a + b; // numbers are added, regardless of you they were expressed above

Related

Integer object leading zero changing the value [duplicate]

This prints 83
System.out.println(0123)
However this prints 123
System.out.println(123)
Why does it work that way?
A leading zero denotes that the literal is expressed using octal (a base-8 number).
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).
Because integer literals starting with 0 are treated as octal numbers.
See section 3.10.1 of the JLS
Try this:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C.
For comparison, try
System.out.println(0x123);
In Java integer literals with a leading zero are octal integers (base 8).
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L and 0101L are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
Even as per the Regex patterns in Oracle docs,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
Thus, your number 0101 be it in Integer or Long format is treated as an Octal representation of a number.
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65
printf will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d" if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d", "%012d", etc...

Long starting with zero has wrong value [duplicate]

This prints 83
System.out.println(0123)
However this prints 123
System.out.println(123)
Why does it work that way?
A leading zero denotes that the literal is expressed using octal (a base-8 number).
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).
Because integer literals starting with 0 are treated as octal numbers.
See section 3.10.1 of the JLS
Try this:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C.
For comparison, try
System.out.println(0x123);
In Java integer literals with a leading zero are octal integers (base 8).
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L and 0101L are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
Even as per the Regex patterns in Oracle docs,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
Thus, your number 0101 be it in Integer or Long format is treated as an Octal representation of a number.
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65
printf will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d" if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d", "%012d", etc...

Trying to print a casted array of ints into chars,but I get message "integer number too large" [duplicate]

This prints 83
System.out.println(0123)
However this prints 123
System.out.println(123)
Why does it work that way?
A leading zero denotes that the literal is expressed using octal (a base-8 number).
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).
Because integer literals starting with 0 are treated as octal numbers.
See section 3.10.1 of the JLS
Try this:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C.
For comparison, try
System.out.println(0x123);
In Java integer literals with a leading zero are octal integers (base 8).
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L and 0101L are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
Even as per the Regex patterns in Oracle docs,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
Thus, your number 0101 be it in Integer or Long format is treated as an Octal representation of a number.
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65
printf will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d" if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d", "%012d", etc...

Integer massive [duplicate]

This prints 83
System.out.println(0123)
However this prints 123
System.out.println(123)
Why does it work that way?
A leading zero denotes that the literal is expressed using octal (a base-8 number).
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).
Because integer literals starting with 0 are treated as octal numbers.
See section 3.10.1 of the JLS
Try this:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C.
For comparison, try
System.out.println(0x123);
In Java integer literals with a leading zero are octal integers (base 8).
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L and 0101L are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
Even as per the Regex patterns in Oracle docs,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
Thus, your number 0101 be it in Integer or Long format is treated as an Octal representation of a number.
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65
printf will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d" if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d", "%012d", etc...

converting floating point to 32-bit fixed point in Java

I have to convert a floating point to 32-bit fixed point in Java .
Not able to understand what is a 32-bit fixed point ?
Can any body help with algorithm ?
A fixed-point number is a representation of a real number using a certain number of bits of a type for the integer part, and the remaining bits of the type for the fractional part. The number of bits representing each part is fixed (hence the name, fixed-point). An integer type is usually used to store fixed-point values.
Fixed-point numbers are usually used in systems which don't have floating point support, or need more speed than floating point can provide. Fixed-point calculations can be performed using the CPU's integer instructions.
A 32-bit fixed-point number would be stored in an 32-bit type such as int.
Normally each bit in an (unsigned in this case) integer type would represent an integer value 2^n as follows:
1 0 1 1 0 0 1 0 = 2^7 + 2^5 + 2^4 + 2^1 = 178
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
But if the type is used to store a fixed-point value, the bits are interpreted slightly differently:
1 0 1 1 0 0 1 0 = 2^3 + 2^1 + 2^0 + 2^-3 = 11.125
2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4
The fixed point number in the example above is called a 4.4 fixed-point number, since there are 4 bits in the integer part and 4 bits in the fractional part of the number. In a 32 bit type the fixed-point value would typically be in 16.16 format, but also could be 24.8, 28.4 or any other combination.
Converting from a floating-point value to a fixed-point value involves the following steps:
Multiply the float by 2^(number of fractional bits for the type), eg. 2^8 for 24.8
Round the result (just add 0.5) if necessary, and floor it (or cast to an integer type) leaving an integer value.
Assign this value into the fixed-point type.
Obviously you can lose some precision in the fractional part of the number. If the precision of the fractional part is important, the choice of fixed-point format can reflect this - eg. use 16.16 or 8.24 instead of 24.8.
Negative values can also be handled in the same way if your fixed-point number needs to be signed.
If my Java were stronger I'd attempt some code, but I usually write such things in C, so I won't attempt a Java version. Besides, stacker's version looks good to me, with the minor exception that it doesn't offer the possibility of rounding. He even shows you how to perform a multiplication (the shift is important!)
A very simple example for converting to fixed point, it shows how to convert and multiplies PI by2. The resulting is converted back to double to demonstrate that the mantissa wasn't lost during calculation with integers.
You could expand that easily with sin() and cos() lookup tables etc.
I would recommend if you plan to use fixed point to look for a java fixed point library.
public class Fix {
public static final int FIXED_POINT = 16;
public static final int ONE = 1 << FIXED_POINT;
public static int mul(int a, int b) {
return (int) ((long) a * (long) b >> FIXED_POINT);
}
public static int toFix( double val ) {
return (int) (val * ONE);
}
public static int intVal( int fix ) {
return fix >> FIXED_POINT;
}
public static double doubleVal( int fix ) {
return ((double) fix) / ONE;
}
public static void main(String[] args) {
int f1 = toFix( Math.PI );
int f2 = toFix( 2 );
int result = mul( f1, f2 );
System.out.println( "f1:" + f1 + "," + intVal( f1 ) );
System.out.println( "f2:" + f2 + "," + intVal( f2 ) );
System.out.println( "r:" + result +"," + intVal( result));
System.out.println( "double: " + doubleVal( result ));
}
}
OUTPUT
f1:205887,3
f2:131072,2
r:411774,6
double: 6.283172607421875
A fixed-point type is one that has a fixed number of decimal/binary places after the radix point. Or more generally, a type that can store multiples of 1/N for some positive integer N.
Internally, fixed-point numbers are stored as the value multiplied by the scaling factor. For example, 123.45 with a scaling factor of 100 is stored as if it were the integer 12345.
To convert the internal value of a fixed-point number to floating point, simply divide by the scaling factor. To convert the other way, multiply by the scaling factor and round to the nearest integer.
The definition of 32-bit fixed point could vary. The general idea of fixed point is that you have some fixed number of bits before and another fixed number of bits after the decimal point (or binary point). For a 32-bit one, the most common split is probably even (16 before, 16 after), but depending on the purpose there's no guarantee of that.
As far as the conversion goes, again it's open to some variation -- for example, if the input number is outside the range of the target, you might want to do any number of different things (e.g., in some cases wraparound could make sense, but in others saturation might be preferred).

Categories

Resources