fork(48) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. /*
  11.  * bitAnd - x&y using only ~ and |
  12.  * Example: bitAnd(6, 5) = 4
  13.  * Legal ops: ~ |
  14.  * Max ops: 8
  15.  * Rating: 1
  16.  */
  17. int bitAnd(int x, int y) {
  18. int ans = ~(~x|~y); //logical equivalent
  19. return ans;
  20. }
  21. /*
  22.  * getByte - Extract byte n from word x
  23.  * Bytes numbered from 0 (LSB) to 3 (MSB)
  24.  * Examples: getByte(0x12345678,1) = 0x56
  25.  * Legal ops: ! ~ & ^ | + << >>
  26.  * Max ops: 6
  27.  * Rating: 2
  28.  */
  29. int getByte(int x, int n) {
  30. return (x >> (n << 3)) & 0xFF;
  31. }
  32. /*
  33.  * conditional - same as x ? y : z
  34.  * Example: conditional(2,4,5) = 4
  35.  * Legal ops: ! ~ & ^ | + << >>
  36.  * Max ops: 16
  37.  * Rating: 3
  38.  */
  39. int conditional(int x, int y, int z) {
  40. int a = !!x;
  41. int b = ~a+0x01;
  42. return (b & y) | (~b & z);
  43.  
  44. }
  45. /*
  46.  * rotateLeft - Rotate x to the left by n
  47.  * Can assume that 0 <= n <= 31
  48.  * Examples: rotateLeft(0x87654321,4) = 0x76543218
  49.  * Legal ops: ~ & ^ | + << >>
  50.  * Max ops: 25
  51.  * Rating: 3
  52.  */
  53. int rotateLeft(int x, int n) {
  54. int c = 127;
  55. int c2 = 255;
  56. return (((x>>1)&((c<<24)+(c2<<16)+(c2<<8)+c2))>>(31&(~n)))+(x<<n);
  57. }
  58. /*
  59.  * bang - Compute !x without using !
  60.  * Examples: bang(3) = 0, bang(0) = 1
  61.  * Legal ops: ~ & ^ | + << >>
  62.  * Max ops: 12
  63.  * Rating: 4
  64.  */
  65. int bang(int x) {
  66. return ((~(~x+0x01) & ~x )>>0x1F) & 0x01;;
  67. }
  68. /*
  69.  * fitsShort - return 1 if x can be represented as a
  70.  * 16-bit, two's complement integer.
  71.  * Examples: fitsShort(33000) = 0, fitsShort(-32768) = 1
  72.  * Legal ops: ! ~ & ^ | + << >>
  73.  * Max ops: 8
  74.  * Rating: 1
  75.  */
  76. int fitsShort(int x) {
  77. return !(((x << (33+~16)) >> (33+~16)) ^ x);
  78. }
  79. /*
  80.  * byteSwap - swaps the nth byte and the mth byte
  81.  * Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
  82.  * byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
  83.  * You may assume that 0 <= n <= 3, 0 <= m <= 3
  84.  * Legal ops: ! ~ & ^ | + << >>
  85.  * Max ops: 25
  86.  * Rating: 2
  87.  * stored the two bytes, created third int that had
  88.  */
  89. int byteSwap(int x, int n, int m) {
  90. int i = (((x & (0xFF<<(n<<3)))>>(n<<3))&0xFF)<<(m<<3);
  91. int j = (((x & (0xFF<<(m<<3)))>>(m<<3))&0xFF)<<(n<<3);
  92. int ans = (x & ~((0xFF<<(n<<3)) | (0xFF<<(m<<3))));
  93. return ans|i|j;
  94. }
  95. /*
  96.  * divpwr2 - Compute x/(2^n), for 0 <= n <= 30
  97.  * Round toward zero
  98.  * Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
  99.  * Legal ops: ! ~ & ^ | + << >>
  100.  * Max ops: 15
  101.  * Rating: 2
  102.  *
  103.  */
  104. int divpwr2(int x, int n) {
  105. int mask = (1 << n) + ~0;
  106. int equalizer = (x >> 31) & mask;
  107. return (x + equalizer) >> n;}
  108. /*
  109.  * negate - return -x
  110.  * Example: negate(1) = -1.
  111.  * Legal ops: ! ~ & ^ | + << >>
  112.  * Max ops: 5
  113.  * Rating: 2
  114.  */
  115. int negate(int x) {
  116. return ~x+1;
  117. }
  118. /*
  119.  * isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
  120.  * Example: isAsciiDigit(0x35) = 1.
  121.  * isAsciiDigit(0x3a) = 0.
  122.  * isAsciiDigit(0x05) = 0.
  123.  * Legal ops: ! ~ & ^ | + << >>
  124.  * Max ops: 15
  125.  * Rating: 3
  126.  * use xor with 30
  127.  */
  128. int isAsciiDigit(int x) {
  129. int filter;
  130. int checkThirty;
  131. int checkSeven;
  132. int checkNine;
  133. filter = 0x30;
  134. checkThirty = x ^ filter;
  135. filter = 0x9;
  136. checkNine = (checkThirty & ~filter);
  137. filter = 0x7;
  138. checkSeven = checkThirty & ~filter;
  139. return !checkSeven | !checkNine;;
  140. }
  141. /*
  142.  * isLessOrEqual - if x <= y then return 1, else return 0
  143.  * Example: isLessOrEqual(4,5) = 1.
  144.  * Legal ops: ! ~ & ^ | + << >>
  145.  * Max ops: 24
  146.  * Rating: 3
  147.  */
  148. int isLessOrEqual(int x, int y) {
  149. int sign_y,sign_x,comb_sign_xy,override_byte,z;
  150.  
  151. sign_y=y>>31;
  152. sign_x=x>>31;
  153. comb_sign_xy=sign_x^sign_y;
  154. override_byte=comb_sign_xy&sign_x;
  155.  
  156. z=((y+(~x)+1))>>31;
  157.  
  158. return !((z|comb_sign_xy)^override_byte);
  159. }
  160. /*
  161.  * ilog2 - return floor(log base 2 of x), where x > 0
  162.  * Example: ilog2(16) = 4
  163.  * Legal ops: ! ~ & ^ | + << >>
  164.  * Max ops: 90
  165.  * Rating: 4
  166.  */
  167. int ilog2(int x) {
  168. int high_bytes=(0xFF<<24) + (0xFF<<16);
  169. int third_byte=0xFF<<8;
  170. int high_bits=0xF0;
  171. int mid_bits=0x0C;
  172. int twos_bit=0x02;
  173. int shift1 = (!!(x & high_bytes)) << 4;
  174. x = x >> shift1;
  175. int shift2 = (!!(x & third_byte)) << 3;
  176. x = x >> shift2;
  177. int shift3 = (!!(x & high_bits)) << 2;
  178. x = x >> shift3;
  179. int shift4 = (!!(x & mid_bits)) << 1;
  180. x = x >> shift4;
  181. int shift5 = (!!(x & twos_bit));
  182. int ans = shift1 + shift2 + shift3 + shift4 + shift5;
  183. return ans;
  184. }
  185. /*
  186.  * float_neg - Return bit-level equivalent of expression -f for
  187.  * floating point argument f.
  188.  * Both the argument and result are passed as unsigned int's, but
  189.  * they are to be interpreted as the bit-level representations of
  190.  * single-precision floating point values.
  191.  * When argument is NaN, return argument.
  192.  * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  193.  * Max ops: 10
  194.  * Rating: 2
  195.  */
  196. unsigned float_neg(unsigned uf) {
  197. int signless = ((~(1<<31))&uf);
  198. int exp = signless>>23;
  199. if (exp==0xFF){
  200. int fracshift=uf<<9;
  201. if(fracshift!=0){
  202. return uf;
  203. }
  204. }
  205. return uf^(1<<31);
  206.  
  207. }
  208. /*
  209.  * float_abs - Return bit-level equivalent of absolute value of f for
  210.  * floating point argument f.
  211.  * Both the argument and result are passed as unsigned int's, but
  212.  * they are to be interpreted as the bit-level representations of
  213.  * single-precision floating point values.
  214.  * When argument is NaN, return argument..
  215.  * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  216.  * Max ops: 10
  217.  * Rating: 2
  218.  */
  219. unsigned float_abs(unsigned uf) {
  220. int signless = ((~(1<<31))&uf);
  221. int exp = signless>>23;
  222. if (exp==0xFF){
  223. int fracshift=uf<<9;
  224. if(fracshift!=0){
  225. return uf;
  226. }
  227. }
  228. return uf&(~(1<<31));
  229. }
  230. /*
  231.  * float_i2f - Return bit-level equivalent of expression (float) x
  232.  * Result is returned as unsigned int, but
  233.  * it is to be interpreted as the bit-level representation of a
  234.  * single-precision floating point values.
  235.  * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  236.  * Max ops: 30
  237.  * Rating: 4
  238.  */
  239. unsigned float_i2f(int x) {
  240. unsigned frac = x;
  241. if(x==0){
  242. return 0;
  243. }
  244. unsigned sign=0;
  245. if (x<0){
  246. sign = 1<<31;
  247. frac = -x;
  248. }
  249. int count = 0;
  250. while((frac & 1<<31)==0){
  251. frac=frac<<1;
  252. count = count + 1;
  253. }
  254. frac = frac<<1;
  255. count = count + 1;
  256. int E = 32 - count;
  257. int exp = E + 127;
  258. int low_nine = frac & (0x000001ff);
  259. int tenth = frac & (0x200);
  260. frac = frac >> 9;
  261. int ans = sign + (exp<<23) + frac;
  262. //rounding
  263. //int low_nine = frac & ((1<<8) + 0xFF);
  264. //int tenth = frac & (1<<9);
  265. if(low_nine > 0x00000100 || ((low_nine == 0x00000100) && (tenth>>9))){
  266. ans = ans+1;
  267. }
  268. return ans;
  269.  
  270. }
  271. public static void main (String[] args) throws java.lang.Exception
  272. {
  273. // your code goes here
  274. }
  275. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:196: error: cannot find symbol
unsigned float_neg(unsigned uf) {
                   ^
  symbol:   class unsigned
  location: class Ideone
Main.java:196: error: cannot find symbol
unsigned float_neg(unsigned uf) {
^
  symbol:   class unsigned
  location: class Ideone
Main.java:219: error: cannot find symbol
unsigned float_abs(unsigned uf) {
                   ^
  symbol:   class unsigned
  location: class Ideone
Main.java:219: error: cannot find symbol
unsigned float_abs(unsigned uf) {
^
  symbol:   class unsigned
  location: class Ideone
Main.java:239: error: cannot find symbol
unsigned float_i2f(int x) {
^
  symbol:   class unsigned
  location: class Ideone
Main.java:40: error: bad operand type int for unary operator '!'
  int a = !!x;
           ^
Main.java:77: error: bad operand type int for unary operator '!'
  return !(((x << (33+~16)) >> (33+~16)) ^ x);
         ^
Main.java:139: error: bad operand type int for unary operator '!'
    return !checkSeven | !checkNine;;
           ^
Main.java:139: error: bad operand type int for unary operator '!'
    return !checkSeven | !checkNine;;
                         ^
Main.java:158: error: bad operand type int for unary operator '!'
    return !((z|comb_sign_xy)^override_byte);
           ^
Main.java:173: error: bad operand type int for unary operator '!'
  int shift1 = (!!(x & high_bytes)) << 4;
                 ^
Main.java:175: error: bad operand type int for unary operator '!'
  int shift2 = (!!(x & third_byte)) << 3;
                 ^
Main.java:177: error: bad operand type int for unary operator '!'
  int shift3 = (!!(x & high_bits)) << 2;
                 ^
Main.java:179: error: bad operand type int for unary operator '!'
  int shift4 = (!!(x & mid_bits)) << 1;
                 ^
Main.java:181: error: bad operand type int for unary operator '!'
  int shift5 = (!!(x & twos_bit));
                 ^
Main.java:240: error: cannot find symbol
  unsigned frac = x;
  ^
  symbol:   class unsigned
  location: class Ideone
Main.java:244: error: cannot find symbol
  unsigned sign=0;
  ^
  symbol:   class unsigned
  location: class Ideone
Main.java:265: error: bad operand types for binary operator '&&'
  if(low_nine > 0x00000100 || ((low_nine == 0x00000100) && (tenth>>9))){
                                                        ^
  first type:  boolean
  second type: int
18 errors
stdout
Standard output is empty