processing negative number in “accounting” format

If you create an “as.acntngFmt” method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses(“acnt”). setClass(“acntngFmt”) # [1] “acntngFmt” setAs(“character”, “acntngFmt”, function(from) as.numeric( gsub(“\\)”, “”, gsub(“\\(“, “-“, from)))) Input <- “A, B, C (1.76), 1%, 3.50€ 2.00, 2%, 4.77€ 3.000, 3% , €5.68” DF <- read.csv(textConnection(Input), header = … Read more

2’s complement hex number to decimal in java

This seems to trick java into converting the number without forcing a positive result: Integer.valueOf(“FFFF”,16).shortValue(); // evaluates to -1 (short) Of course this sort of thing only works for 8, 16, 32, and 64-bit 2’s complement: Short.valueOf(“FF”,16).byteValue(); // -1 (byte) Integer.valueOf(“FFFF”,16).shortValue(); // -1 (short) Long.valueOf(“FFFFFFFF”,16).intValue(); // -1 (int) new BigInteger(“FFFFFFFFFFFFFFFF”,16).longValue(); // -1 (long) Example here.

Why does the most negative int value cause an error about ambiguous function overloads?

This is a very subtle error. What you are seeing is a consequence of there being no negative integer literals in C++. If we look at [lex.icon] we get that a integer-literal, integer-literal         decimal-literal integer-suffixopt         […] can be a decimal-literal, decimal-literal:         nonzero-digit         decimal-literal ’ opt digit where digit is [0-9] and nonzero-digit is [1-9] and … Read more

Java: right shift on negative number

Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>>. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Arithmetic shift >> will keep the sign bit. Unsigned shift >>> will not keep the sign bit (thus filling 0s). (images from Wikipedia) By the way, both arithmetic left shift and logical … Read more

Representation of negative numbers in C?

ISO C (C99 section 6.2.6.2/2 in this case but it carries forward to later iterations of the standard(a)) states that an implementation must choose one of three different representations for integral data types, two’s complement, ones’ complement or sign/magnitude (although it’s incredibly likely that the two’s complement implementations far outweigh the others). In all those … Read more