painless-operators-numeric.asciidoc 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. [[painless-operators-numeric]]
  2. === Operators: Numeric
  3. [[post-increment-operator]]
  4. ==== Post Increment
  5. Use the `post increment operator '++'` to INCREASE the value of a numeric type
  6. variable/field by `1`. An extra implicit cast is necessary to return the
  7. promoted numeric type value to the original numeric type value of the
  8. variable/field for the following types: `byte`, `short`, and `char`. If a
  9. variable/field is read as part of an expression the value is loaded prior to the
  10. increment.
  11. *Errors*
  12. * If the variable/field is a non-numeric type.
  13. *Grammar*
  14. [source,ANTLR4]
  15. ----
  16. post_increment: ( variable | field ) '++';
  17. ----
  18. *Promotion*
  19. [options="header",cols="<1,<1,<1"]
  20. |====
  21. | original | promoted | implicit
  22. | byte | int | byte
  23. | short | int | short
  24. | char | int | char
  25. | int | int |
  26. | long | long |
  27. | float | float |
  28. | double | double |
  29. | def | def |
  30. |====
  31. *Examples*
  32. * Post increment with different numeric types.
  33. +
  34. [source,Painless]
  35. ----
  36. <1> short i = 0;
  37. <2> i++;
  38. <3> long j = 1;
  39. <4> long k;
  40. <5> k = j++;
  41. ----
  42. +
  43. <1> declare `short i`;
  44. store `short 0` to `i`
  45. <2> load from `i` -> `short 0`;
  46. promote `short 0`: result `int`;
  47. add `int 0` and `int 1` -> `int 1`;
  48. implicit cast `int 1` to `short 1`;
  49. store `short 1` to `i`
  50. <3> declare `long j`;
  51. implicit cast `int 1` to `long 1` -> `long 1`;
  52. store `long 1` to `j`
  53. <4> declare `long k`;
  54. store default `long 0` to `k`
  55. <5> load from `j` -> `long 1`;
  56. store `long 1` to `k`;
  57. add `long 1` and `long 1` -> `long 2`;
  58. store `long 2` to `j`
  59. +
  60. * Post increment with the `def` type.
  61. +
  62. [source,Painless]
  63. ----
  64. <1> def x = 1;
  65. <2> x++;
  66. ----
  67. +
  68. <1> declare `def x`;
  69. implicit cast `int 1` to `def` -> `def`;
  70. store `def` to `x`
  71. <2> load from `x` -> `def`;
  72. implicit cast `def` to `int 1`;
  73. add `int 1` and `int 1` -> `int 2`;
  74. implicit cast `int 2` to `def`;
  75. store `def` to `x`
  76. [[post-decrement-operator]]
  77. ==== Post Decrement
  78. Use the `post decrement operator '--'` to DECREASE the value of a numeric type
  79. variable/field by `1`. An extra implicit cast is necessary to return the
  80. promoted numeric type value to the original numeric type value of the
  81. variable/field for the following types: `byte`, `short`, and `char`. If a
  82. variable/field is read as part of an expression the value is loaded prior to
  83. the decrement.
  84. *Errors*
  85. * If the variable/field is a non-numeric type.
  86. *Grammar*
  87. [source,ANTLR4]
  88. ----
  89. post_decrement: ( variable | field ) '--';
  90. ----
  91. *Promotion*
  92. [options="header",cols="<1,<1,<1"]
  93. |====
  94. | original | promoted | implicit
  95. | byte | int | byte
  96. | short | int | short
  97. | char | int | char
  98. | int | int |
  99. | long | long |
  100. | float | float |
  101. | double | double |
  102. | def | def |
  103. |====
  104. *Examples*
  105. * Post decrement with different numeric types.
  106. +
  107. [source,Painless]
  108. ----
  109. <1> short i = 0;
  110. <2> i--;
  111. <3> long j = 1;
  112. <4> long k;
  113. <5> k = j--;
  114. ----
  115. +
  116. <1> declare `short i`;
  117. store `short 0` to `i`
  118. <2> load from `i` -> `short 0`;
  119. promote `short 0`: result `int`;
  120. subtract `int 1` from `int 0` -> `int -1`;
  121. implicit cast `int -1` to `short -1`;
  122. store `short -1` to `i`
  123. <3> declare `long j`;
  124. implicit cast `int 1` to `long 1` -> `long 1`;
  125. store `long 1` to `j`
  126. <4> declare `long k`;
  127. store default `long 0` to `k`
  128. <5> load from `j` -> `long 1`;
  129. store `long 1` to `k`;
  130. subtract `long 1` from `long 1` -> `long 0`;
  131. store `long 0` to `j`
  132. +
  133. * Post decrement with the `def` type.
  134. +
  135. [source,Painless]
  136. ----
  137. <1> def x = 1;
  138. <2> x--;
  139. ----
  140. +
  141. <1> declare `def x`;
  142. implicit cast `int 1` to `def` -> `def`;
  143. store `def` to `x`
  144. <2> load from `x` -> `def`;
  145. implicit cast `def` to `int 1`;
  146. subtract `int 1` from `int 1` -> `int 0`;
  147. implicit cast `int 0` to `def`;
  148. store `def` to `x`
  149. [[pre-increment-operator]]
  150. ==== Pre Increment
  151. Use the `pre increment operator '++'` to INCREASE the value of a numeric type
  152. variable/field by `1`. An extra implicit cast is necessary to return the
  153. promoted numeric type value to the original numeric type value of the
  154. variable/field for the following types: `byte`, `short`, and `char`. If a
  155. variable/field is read as part of an expression the value is loaded after the
  156. increment.
  157. *Errors*
  158. * If the variable/field is a non-numeric type.
  159. *Grammar*
  160. [source,ANTLR4]
  161. ----
  162. pre_increment: '++' ( variable | field );
  163. ----
  164. *Promotion*
  165. [options="header",cols="<1,<1,<1"]
  166. |====
  167. | original | promoted | implicit
  168. | byte | int | byte
  169. | short | int | short
  170. | char | int | char
  171. | int | int |
  172. | long | long |
  173. | float | float |
  174. | double | double |
  175. | def | def |
  176. |====
  177. *Examples*
  178. * Pre increment with different numeric types.
  179. +
  180. [source,Painless]
  181. ----
  182. <1> short i = 0;
  183. <2> ++i;
  184. <3> long j = 1;
  185. <4> long k;
  186. <5> k = ++j;
  187. ----
  188. +
  189. <1> declare `short i`;
  190. store `short 0` to `i`
  191. <2> load from `i` -> `short 0`;
  192. promote `short 0`: result `int`;
  193. add `int 0` and `int 1` -> `int 1`;
  194. implicit cast `int 1` to `short 1`;
  195. store `short 1` to `i`
  196. <3> declare `long j`;
  197. implicit cast `int 1` to `long 1` -> `long 1`;
  198. store `long 1` to `j`
  199. <4> declare `long k`;
  200. store default `long 0` to `k`
  201. <5> load from `j` -> `long 1`;
  202. add `long 1` and `long 1` -> `long 2`;
  203. store `long 2` to `j`;
  204. store `long 2` to `k`
  205. +
  206. * Pre increment with the `def` type.
  207. +
  208. [source,Painless]
  209. ----
  210. <1> def x = 1;
  211. <2> ++x;
  212. ----
  213. +
  214. <1> declare `def x`;
  215. implicit cast `int 1` to `def` -> `def`;
  216. store `def` to `x`
  217. <2> load from `x` -> `def`;
  218. implicit cast `def` to `int 1`;
  219. add `int 1` and `int 1` -> `int 2`;
  220. implicit cast `int 2` to `def`;
  221. store `def` to `x`
  222. [[pre-decrement-operator]]
  223. ==== Pre Decrement
  224. Use the `pre decrement operator '--'` to DECREASE the value of a numeric type
  225. variable/field by `1`. An extra implicit cast is necessary to return the
  226. promoted numeric type value to the original numeric type value of the
  227. variable/field for the following types: `byte`, `short`, and `char`. If a
  228. variable/field is read as part of an expression the value is loaded after the
  229. decrement.
  230. *Errors*
  231. * If the variable/field is a non-numeric type.
  232. *Grammar*
  233. [source,ANTLR4]
  234. ----
  235. pre_increment: '--' ( variable | field );
  236. ----
  237. *Promotion*
  238. [options="header",cols="<1,<1,<1"]
  239. |====
  240. | original | promoted | implicit
  241. | byte | int | byte
  242. | short | int | short
  243. | char | int | char
  244. | int | int |
  245. | long | long |
  246. | float | float |
  247. | double | double |
  248. | def | def |
  249. |====
  250. *Examples*
  251. * Pre decrement with different numeric types.
  252. +
  253. [source,Painless]
  254. ----
  255. <1> short i = 0;
  256. <2> --i;
  257. <3> long j = 1;
  258. <4> long k;
  259. <5> k = --j;
  260. ----
  261. +
  262. <1> declare `short i`;
  263. store `short 0` to `i`
  264. <2> load from `i` -> `short 0`;
  265. promote `short 0`: result `int`;
  266. subtract `int 1` from `int 0` -> `int -1`;
  267. implicit cast `int -1` to `short -1`;
  268. store `short -1` to `i`
  269. <3> declare `long j`;
  270. implicit cast `int 1` to `long 1` -> `long 1`;
  271. store `long 1` to `j`
  272. <4> declare `long k`;
  273. store default `long 0` to `k`
  274. <5> load from `j` -> `long 1`;
  275. subtract `long 1` from `long 1` -> `long 0`;
  276. store `long 0` to `j`
  277. store `long 0` to `k`;
  278. +
  279. * Pre decrement operator with the `def` type.
  280. +
  281. [source,Painless]
  282. ----
  283. <1> def x = 1;
  284. <2> --x;
  285. ----
  286. +
  287. <1> declare `def x`;
  288. implicit cast `int 1` to `def` -> `def`;
  289. store `def` to `x`
  290. <2> load from `x` -> `def`;
  291. implicit cast `def` to `int 1`;
  292. subtract `int 1` from `int 1` -> `int 0`;
  293. implicit cast `int 0` to `def`;
  294. store `def` to `x`
  295. [[unary-positive-operator]]
  296. ==== Unary Positive
  297. Use the `unary positive operator '+'` to the preserve the IDENTITY of a
  298. numeric type value.
  299. *Errors*
  300. * If the value is a non-numeric type.
  301. *Grammar*
  302. [source,ANTLR4]
  303. ----
  304. unary_positive: '+' expression;
  305. ----
  306. *Examples*
  307. * Unary positive with different numeric types.
  308. +
  309. [source,Painless]
  310. ----
  311. <1> int x = +1;
  312. <2> long y = +x;
  313. ----
  314. +
  315. <1> declare `int x`;
  316. identity `int 1` -> `int 1`;
  317. store `int 1` to `x`
  318. <2> declare `long y`;
  319. load from `x` -> `int 1`;
  320. identity `int 1` -> `int 1`;
  321. implicit cast `int 1` to `long 1` -> `long 1`;
  322. store `long 1` to `y`
  323. +
  324. * Unary positive with the `def` type.
  325. +
  326. [source,Painless]
  327. ----
  328. <1> def z = +1;
  329. <2> int i = +z;
  330. ----
  331. <1> declare `def z`;
  332. identity `int 1` -> `int 1`;
  333. implicit cast `int 1` to `def`;
  334. store `def` to `z`
  335. <2> declare `int i`;
  336. load from `z` -> `def`;
  337. implicit cast `def` to `int 1`;
  338. identity `int 1` -> `int 1`;
  339. store `int 1` to `i`;
  340. [[unary-negative-operator]]
  341. ==== Unary Negative
  342. Use the `unary negative operator '-'` to NEGATE a numeric type value.
  343. *Errors*
  344. * If the value is a non-numeric type.
  345. *Grammar*
  346. [source,ANTLR4]
  347. ----
  348. unary_negative: '-' expression;
  349. ----
  350. *Examples*
  351. * Unary negative with different numeric types.
  352. +
  353. [source,Painless]
  354. ----
  355. <1> int x = -1;
  356. <2> long y = -x;
  357. ----
  358. +
  359. <1> declare `int x`;
  360. negate `int 1` -> `int -1`;
  361. store `int -1` to `x`
  362. <2> declare `long y`;
  363. load from `x` -> `int 1`;
  364. negate `int -1` -> `int 1`;
  365. implicit cast `int 1` to `long 1` -> `long 1`;
  366. store `long 1` to `y`
  367. +
  368. * Unary negative with the `def` type.
  369. +
  370. [source,Painless]
  371. ----
  372. <1> def z = -1;
  373. <2> int i = -z;
  374. ----
  375. <1> declare `def z`;
  376. negate `int 1` -> `int -1`;
  377. implicit cast `int -1` to `def`;
  378. store `def` to `z`
  379. <2> declare `int i`;
  380. load from `z` -> `def`;
  381. implicit cast `def` to `int -1`;
  382. negate `int -1` -> `int 1`;
  383. store `int 1` to `i`;
  384. [[bitwise-not-operator]]
  385. ==== Bitwise Not
  386. Use the `bitwise not operator '~'` to NOT each bit in an integer type value
  387. where a `1-bit` is flipped to a resultant `0-bit` and a `0-bit` is flipped to a
  388. resultant `1-bit`.
  389. *Errors*
  390. * If the value is a non-integer type.
  391. *Bits*
  392. [options="header",cols="<1,<1"]
  393. |====
  394. | original | result
  395. | 1 | 0
  396. | 0 | 1
  397. |====
  398. *Grammar*
  399. [source,ANTLR4]
  400. ----
  401. bitwise_not: '~' expression;
  402. ----
  403. *Promotion*
  404. [options="header",cols="<1,<1"]
  405. |====
  406. | original | promoted
  407. | byte | int
  408. | short | int
  409. | char | int
  410. | int | int
  411. | long | long
  412. | def | def
  413. |====
  414. *Examples*
  415. * Bitwise not with different numeric types.
  416. +
  417. [source,Painless]
  418. ----
  419. <1> byte b = 1;
  420. <2> int i = ~b;
  421. <3> long l = ~i;
  422. ----
  423. +
  424. <1> declare `byte x`;
  425. store `byte 1` to b
  426. <2> declare `int i`;
  427. load from `b` -> `byte 1`;
  428. implicit cast `byte 1` to `int 1` -> `int 1`;
  429. bitwise not `int 1` -> `int -2`;
  430. store `int -2` to `i`
  431. <3> declare `long l`;
  432. load from `i` -> `int -2`;
  433. implicit cast `int -2` to `long -2` -> `long -2`;
  434. bitwise not `long -2` -> `long 1`;
  435. store `long 1` to `l`
  436. +
  437. * Bitwise not with the `def` type.
  438. +
  439. [source,Painless]
  440. ----
  441. <1> def d = 1;
  442. <2> def e = ~d;
  443. ----
  444. +
  445. <1> declare `def d`;
  446. implicit cast `int 1` to `def` -> `def`;
  447. store `def` to `d`;
  448. <2> declare `def e`;
  449. load from `d` -> `def`;
  450. implicit cast `def` to `int 1` -> `int 1`;
  451. bitwise not `int 1` -> `int -2`;
  452. implicit cast `int 1` to `def` -> `def`;
  453. store `def` to `e`
  454. [[multiplication-operator]]
  455. ==== Multiplication
  456. Use the `multiplication operator '*'` to MULTIPLY together two numeric type
  457. values. Rules for resultant overflow and NaN values follow the JVM
  458. specification.
  459. *Errors*
  460. * If either of the values is a non-numeric type.
  461. *Grammar*
  462. [source,ANTLR4]
  463. ----
  464. multiplication: expression '*' expression;
  465. ----
  466. *Promotion*
  467. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  468. |====
  469. | | byte | short | char | int | long | float | double | def
  470. | byte | int | int | int | int | long | float | double | def
  471. | short | int | int | int | int | long | float | double | def
  472. | char | int | int | int | int | long | float | double | def
  473. | int | int | int | int | int | long | float | double | def
  474. | long | long | long | long | long | long | float | double | def
  475. | float | float | float | float | float | float | float | double | def
  476. | double | double | double | double | double | double | double | double | def
  477. | def | def | def | def | def | def | def | def | def
  478. |====
  479. *Examples*
  480. * Multiplication with different numeric types.
  481. +
  482. [source,Painless]
  483. ----
  484. <1> int i = 5*4;
  485. <2> double d = i*7.0;
  486. ----
  487. +
  488. <1> declare `int i`;
  489. multiply `int 4` by `int 5` -> `int 20`;
  490. store `int 20` in `i`
  491. <2> declare `double d`;
  492. load from `int i` -> `int 20`;
  493. promote `int 20` and `double 7.0`: result `double`;
  494. implicit cast `int 20` to `double 20.0` -> `double 20.0`;
  495. multiply `double 20.0` by `double 7.0` -> `double 140.0`;
  496. store `double 140.0` to `d`
  497. +
  498. * Multiplication with the `def` type.
  499. +
  500. [source,Painless]
  501. ----
  502. <1> def x = 5*4;
  503. <2> def y = x*2;
  504. ----
  505. <1> declare `def x`;
  506. multiply `int 5` by `int 4` -> `int 20`;
  507. implicit cast `int 20` to `def` -> `def`;
  508. store `def` in `x`
  509. <2> declare `def y`;
  510. load from `x` -> `def`;
  511. implicit cast `def` to `int 20`;
  512. multiply `int 20` by `int 2` -> `int 40`;
  513. implicit cast `int 40` to `def` -> `def`;
  514. store `def` to `y`
  515. [[division-operator]]
  516. ==== Division
  517. Use the `division operator '/'` to DIVIDE one numeric type value by another.
  518. Rules for NaN values and division by zero follow the JVM specification. Division
  519. with integer values drops the remainder of the resultant value.
  520. *Errors*
  521. * If either of the values is a non-numeric type.
  522. * If a left-hand side integer type value is divided by a right-hand side integer
  523. type value of `0`.
  524. *Grammar*
  525. [source,ANTLR4]
  526. ----
  527. division: expression '/' expression;
  528. ----
  529. *Promotion*
  530. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  531. |====
  532. | | byte | short | char | int | long | float | double | def
  533. | byte | int | int | int | int | long | float | double | def
  534. | short | int | int | int | int | long | float | double | def
  535. | char | int | int | int | int | long | float | double | def
  536. | int | int | int | int | int | long | float | double | def
  537. | long | long | long | long | long | long | float | double | def
  538. | float | float | float | float | float | float | float | double | def
  539. | double | double | double | double | double | double | double | double | def
  540. | def | def | def | def | def | def | def | def | def
  541. |====
  542. *Examples*
  543. * Division with different numeric types.
  544. +
  545. [source,Painless]
  546. ----
  547. <1> int i = 29/4;
  548. <2> double d = i/7.0;
  549. ----
  550. +
  551. <1> declare `int i`;
  552. divide `int 29` by `int 4` -> `int 7`;
  553. store `int 7` in `i`
  554. <2> declare `double d`;
  555. load from `int i` -> `int 7`;
  556. promote `int 7` and `double 7.0`: result `double`;
  557. implicit cast `int 7` to `double 7.0` -> `double 7.0`;
  558. divide `double 7.0` by `double 7.0` -> `double 1.0`;
  559. store `double 1.0` to `d`
  560. +
  561. * Division with the `def` type.
  562. +
  563. [source,Painless]
  564. ----
  565. <1> def x = 5/4;
  566. <2> def y = x/2;
  567. ----
  568. <1> declare `def x`;
  569. divide `int 5` by `int 4` -> `int 1`;
  570. implicit cast `int 1` to `def` -> `def`;
  571. store `def` in `x`
  572. <2> declare `def y`;
  573. load from `x` -> `def`;
  574. implicit cast `def` to `int 1`;
  575. divide `int 1` by `int 2` -> `int 0`;
  576. implicit cast `int 0` to `def` -> `def`;
  577. store `def` to `y`
  578. [[remainder-operator]]
  579. ==== Remainder
  580. Use the `remainder operator '%'` to calculate the REMAINDER for division
  581. between two numeric type values. Rules for NaN values and division by zero follow the JVM
  582. specification.
  583. *Errors*
  584. * If either of the values is a non-numeric type.
  585. *Grammar*
  586. [source,ANTLR4]
  587. ----
  588. remainder: expression '%' expression;
  589. ----
  590. *Promotion*
  591. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  592. |====
  593. | | byte | short | char | int | long | float | double | def
  594. | byte | int | int | int | int | long | float | double | def
  595. | short | int | int | int | int | long | float | double | def
  596. | char | int | int | int | int | long | float | double | def
  597. | int | int | int | int | int | long | float | double | def
  598. | long | long | long | long | long | long | float | double | def
  599. | float | float | float | float | float | float | float | double | def
  600. | double | double | double | double | double | double | double | double | def
  601. | def | def | def | def | def | def | def | def | def
  602. |====
  603. *Examples*
  604. * Remainder with different numeric types.
  605. +
  606. [source,Painless]
  607. ----
  608. <1> int i = 29%4;
  609. <2> double d = i%7.0;
  610. ----
  611. +
  612. <1> declare `int i`;
  613. remainder `int 29` by `int 4` -> `int 1`;
  614. store `int 7` in `i`
  615. <2> declare `double d`;
  616. load from `int i` -> `int 1`;
  617. promote `int 1` and `double 7.0`: result `double`;
  618. implicit cast `int 1` to `double 1.0` -> `double 1.0`;
  619. remainder `double 1.0` by `double 7.0` -> `double 1.0`;
  620. store `double 1.0` to `d`
  621. +
  622. * Remainder with the `def` type.
  623. +
  624. [source,Painless]
  625. ----
  626. <1> def x = 5%4;
  627. <2> def y = x%2;
  628. ----
  629. <1> declare `def x`;
  630. remainder `int 5` by `int 4` -> `int 1`;
  631. implicit cast `int 1` to `def` -> `def`;
  632. store `def` in `x`
  633. <2> declare `def y`;
  634. load from `x` -> `def`;
  635. implicit cast `def` to `int 1`;
  636. remainder `int 1` by `int 2` -> `int 1`;
  637. implicit cast `int 1` to `def` -> `def`;
  638. store `def` to `y`
  639. [[addition-operator]]
  640. ==== Addition
  641. Use the `addition operator '+'` to ADD together two numeric type values. Rules
  642. for resultant overflow and NaN values follow the JVM specification.
  643. *Errors*
  644. * If either of the values is a non-numeric type.
  645. *Grammar*
  646. [source,ANTLR4]
  647. ----
  648. addition: expression '+' expression;
  649. ----
  650. *Promotion*
  651. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  652. |====
  653. | | byte | short | char | int | long | float | double | def
  654. | byte | int | int | int | int | long | float | double | def
  655. | short | int | int | int | int | long | float | double | def
  656. | char | int | int | int | int | long | float | double | def
  657. | int | int | int | int | int | long | float | double | def
  658. | long | long | long | long | long | long | float | double | def
  659. | float | float | float | float | float | float | float | double | def
  660. | double | double | double | double | double | double | double | double | def
  661. | def | def | def | def | def | def | def | def | def
  662. |====
  663. *Examples*
  664. * Addition operator with different numeric types.
  665. +
  666. [source,Painless]
  667. ----
  668. <1> int i = 29+4;
  669. <2> double d = i+7.0;
  670. ----
  671. +
  672. <1> declare `int i`;
  673. add `int 29` and `int 4` -> `int 33`;
  674. store `int 33` in `i`
  675. <2> declare `double d`;
  676. load from `int i` -> `int 33`;
  677. promote `int 33` and `double 7.0`: result `double`;
  678. implicit cast `int 33` to `double 33.0` -> `double 33.0`;
  679. add `double 33.0` and `double 7.0` -> `double 40.0`;
  680. store `double 40.0` to `d`
  681. +
  682. * Addition with the `def` type.
  683. +
  684. [source,Painless]
  685. ----
  686. <1> def x = 5+4;
  687. <2> def y = x+2;
  688. ----
  689. <1> declare `def x`;
  690. add `int 5` and `int 4` -> `int 9`;
  691. implicit cast `int 9` to `def` -> `def`;
  692. store `def` in `x`
  693. <2> declare `def y`;
  694. load from `x` -> `def`;
  695. implicit cast `def` to `int 9`;
  696. add `int 9` and `int 2` -> `int 11`;
  697. implicit cast `int 11` to `def` -> `def`;
  698. store `def` to `y`
  699. [[subtraction-operator]]
  700. ==== Subtraction
  701. Use the `subtraction operator '-'` to SUBTRACT a right-hand side numeric type
  702. value from a left-hand side numeric type value. Rules for resultant overflow
  703. and NaN values follow the JVM specification.
  704. *Errors*
  705. * If either of the values is a non-numeric type.
  706. *Grammar*
  707. [source,ANTLR4]
  708. ----
  709. subtraction: expression '-' expression;
  710. ----
  711. *Promotion*
  712. [cols="<1,^1,^1,^1,^1,^1,^1,^1,^1"]
  713. |====
  714. | | byte | short | char | int | long | float | double | def
  715. | byte | int | int | int | int | long | float | double | def
  716. | short | int | int | int | int | long | float | double | def
  717. | char | int | int | int | int | long | float | double | def
  718. | int | int | int | int | int | long | float | double | def
  719. | long | long | long | long | long | long | float | double | def
  720. | float | float | float | float | float | float | float | double | def
  721. | double | double | double | double | double | double | double | double | def
  722. | def | def | def | def | def | def | def | def | def
  723. |====
  724. *Examples*
  725. * Subtraction with different numeric types.
  726. +
  727. [source,Painless]
  728. ----
  729. <1> int i = 29-4;
  730. <2> double d = i-7.5;
  731. ----
  732. +
  733. <1> declare `int i`;
  734. subtract `int 4` from `int 29` -> `int 25`;
  735. store `int 25` in `i`
  736. <2> declare `double d`
  737. load from `int i` -> `int 25`;
  738. promote `int 25` and `double 7.5`: result `double`;
  739. implicit cast `int 25` to `double 25.0` -> `double 25.0`;
  740. subtract `double 33.0` by `double 7.5` -> `double 25.5`;
  741. store `double 25.5` to `d`
  742. +
  743. * Subtraction with the `def` type.
  744. +
  745. [source,Painless]
  746. ----
  747. <1> def x = 5-4;
  748. <2> def y = x-2;
  749. ----
  750. <1> declare `def x`;
  751. subtract `int 4` and `int 5` -> `int 1`;
  752. implicit cast `int 1` to `def` -> `def`;
  753. store `def` in `x`
  754. <2> declare `def y`;
  755. load from `x` -> `def`;
  756. implicit cast `def` to `int 1`;
  757. subtract `int 2` from `int 1` -> `int -1`;
  758. implicit cast `int -1` to `def` -> `def`;
  759. store `def` to `y`
  760. [[left-shift-operator]]
  761. ==== Left Shift
  762. Use the `left shift operator '<<'` to SHIFT lower order bits to higher order
  763. bits in a left-hand side integer type value by the distance specified in a
  764. right-hand side integer type value.
  765. *Errors*
  766. * If either of the values is a non-integer type.
  767. * If the right-hand side value cannot be cast to an int type.
  768. *Grammar*
  769. [source,ANTLR4]
  770. ----
  771. left_shift: expression '<<' expression;
  772. ----
  773. *Promotion*
  774. The left-hand side integer type value is promoted as specified in the table
  775. below. The right-hand side integer type value is always implicitly cast to an
  776. `int` type value and truncated to the number of bits of the promoted type value.
  777. [options="header",cols="<1,<1"]
  778. |====
  779. | original | promoted
  780. | byte | int
  781. | short | int
  782. | char | int
  783. | int | int
  784. | long | long
  785. | def | def
  786. |====
  787. *Examples*
  788. * Left shift with different integer types.
  789. +
  790. [source,Painless]
  791. ----
  792. <1> int i = 4 << 1;
  793. <2> long l = i << 2L;
  794. ----
  795. +
  796. <1> declare `int i`;
  797. left shift `int 4` by `int 1` -> `int 8`;
  798. store `int 8` in `i`
  799. <2> declare `long l`
  800. load from `int i` -> `int 8`;
  801. implicit cast `long 2` to `int 2` -> `int 2`;
  802. left shift `int 8` by `int 2` -> `int 32`;
  803. implicit cast `int 32` to `long 32` -> `long 32`;
  804. store `long 32` to `l`
  805. +
  806. * Left shift with the `def` type.
  807. +
  808. [source,Painless]
  809. ----
  810. <1> def x = 4 << 2;
  811. <2> def y = x << 1;
  812. ----
  813. <1> declare `def x`;
  814. left shift `int 4` by `int 2` -> `int 16`;
  815. implicit cast `int 16` to `def` -> `def`;
  816. store `def` in `x`
  817. <2> declare `def y`;
  818. load from `x` -> `def`;
  819. implicit cast `def` to `int 16`;
  820. left shift `int 16` by `int 1` -> `int 32`;
  821. implicit cast `int 32` to `def` -> `def`;
  822. store `def` to `y`
  823. [[right-shift-operator]]
  824. ==== Right Shift
  825. Use the `right shift operator '>>'` to SHIFT higher order bits to lower order
  826. bits in a left-hand side integer type value by the distance specified in a
  827. right-hand side integer type value. The highest order bit of the left-hand side
  828. integer type value is preserved.
  829. *Errors*
  830. * If either of the values is a non-integer type.
  831. * If the right-hand side value cannot be cast to an int type.
  832. *Grammar*
  833. [source,ANTLR4]
  834. ----
  835. right_shift: expression '>>' expression;
  836. ----
  837. *Promotion*
  838. The left-hand side integer type value is promoted as specified in the table
  839. below. The right-hand side integer type value is always implicitly cast to an
  840. `int` type value and truncated to the number of bits of the promoted type value.
  841. [options="header",cols="<1,<1"]
  842. |====
  843. | original | promoted
  844. | byte | int
  845. | short | int
  846. | char | int
  847. | int | int
  848. | long | long
  849. | def | def
  850. |====
  851. *Examples*
  852. * Right shift with different integer types.
  853. +
  854. [source,Painless]
  855. ----
  856. <1> int i = 32 >> 1;
  857. <2> long l = i >> 2L;
  858. ----
  859. +
  860. <1> declare `int i`;
  861. right shift `int 32` by `int 1` -> `int 16`;
  862. store `int 16` in `i`
  863. <2> declare `long l`
  864. load from `int i` -> `int 16`;
  865. implicit cast `long 2` to `int 2` -> `int 2`;
  866. right shift `int 16` by `int 2` -> `int 4`;
  867. implicit cast `int 4` to `long 4` -> `long 4`;
  868. store `long 4` to `l`
  869. +
  870. * Right shift with the `def` type.
  871. +
  872. [source,Painless]
  873. ----
  874. <1> def x = 16 >> 2;
  875. <2> def y = x >> 1;
  876. ----
  877. <1> declare `def x`;
  878. right shift `int 16` by `int 2` -> `int 4`;
  879. implicit cast `int 4` to `def` -> `def`;
  880. store `def` in `x`
  881. <2> declare `def y`;
  882. load from `x` -> `def`;
  883. implicit cast `def` to `int 4`;
  884. right shift `int 4` by `int 1` -> `int 2`;
  885. implicit cast `int 2` to `def` -> `def`;
  886. store `def` to `y`
  887. [[unsigned-right-shift-operator]]
  888. ==== Unsigned Right Shift
  889. Use the `unsigned right shift operator '>>>'` to SHIFT higher order bits to
  890. lower order bits in a left-hand side integer type value by the distance
  891. specified in a right-hand side type integer value. The highest order bit of the
  892. left-hand side integer type value is *not* preserved.
  893. *Errors*
  894. * If either of the values is a non-integer type.
  895. * If the right-hand side value cannot be cast to an int type.
  896. *Grammar*
  897. [source,ANTLR4]
  898. ----
  899. unsigned_right_shift: expression '>>>' expression;
  900. ----
  901. *Promotion*
  902. The left-hand side integer type value is promoted as specified in the table
  903. below. The right-hand side integer type value is always implicitly cast to an
  904. `int` type value and truncated to the number of bits of the promoted type value.
  905. [options="header",cols="<1,<1"]
  906. |====
  907. | original | promoted
  908. | byte | int
  909. | short | int
  910. | char | int
  911. | int | int
  912. | long | long
  913. | def | def
  914. |====
  915. *Examples*
  916. * Unsigned right shift with different integer types.
  917. +
  918. [source,Painless]
  919. ----
  920. <1> int i = -1 >>> 29;
  921. <2> long l = i >>> 2L;
  922. ----
  923. +
  924. <1> declare `int i`;
  925. unsigned right shift `int -1` by `int 29` -> `int 7`;
  926. store `int 7` in `i`
  927. <2> declare `long l`
  928. load from `int i` -> `int 7`;
  929. implicit cast `long 2` to `int 2` -> `int 2`;
  930. unsigned right shift `int 7` by `int 2` -> `int 3`;
  931. implicit cast `int 3` to `long 3` -> `long 3`;
  932. store `long 3` to `l`
  933. +
  934. * Unsigned right shift with the `def` type.
  935. +
  936. [source,Painless]
  937. ----
  938. <1> def x = 16 >>> 2;
  939. <2> def y = x >>> 1;
  940. ----
  941. <1> declare `def x`;
  942. unsigned right shift `int 16` by `int 2` -> `int 4`;
  943. implicit cast `int 4` to `def` -> `def`;
  944. store `def` in `x`
  945. <2> declare `def y`;
  946. load from `x` -> `def`;
  947. implicit cast `def` to `int 4`;
  948. unsigned right shift `int 4` by `int 1` -> `int 2`;
  949. implicit cast `int 2` to `def` -> `def`;
  950. store `def` to `y`
  951. [[bitwise-and-operator]]
  952. ==== Bitwise And
  953. Use the `bitwise and operator '&'` to AND together each bit within two
  954. integer type values where if both bits at the same index are `1` the resultant
  955. bit is `1` and `0` otherwise.
  956. *Errors*
  957. * If either of the values is a non-integer type.
  958. *Bits*
  959. [cols="^1,^1,^1"]
  960. |====
  961. | | 1 | 0
  962. | 1 | 1 | 0
  963. | 0 | 0 | 0
  964. |====
  965. *Grammar*
  966. [source,ANTLR4]
  967. ----
  968. bitwise_and: expression '&' expression;
  969. ----
  970. *Promotion*
  971. [cols="<1,^1,^1,^1,^1,^1,^1"]
  972. |====
  973. | | byte | short | char | int | long | def
  974. | byte | int | int | int | int | long | def
  975. | short | int | int | int | int | long | def
  976. | char | int | int | int | int | long | def
  977. | int | int | int | int | int | long | def
  978. | long | long | long | long | long | long | def
  979. | def | def | def | def | def | def | def
  980. |====
  981. *Examples*
  982. * Bitwise and with different integer types.
  983. +
  984. [source,Painless]
  985. ----
  986. <1> int i = 5 & 6;
  987. <2> long l = i & 5L;
  988. ----
  989. +
  990. <1> declare `int i`;
  991. bitwise and `int 5` and `int 6` -> `int 4`;
  992. store `int 4` in `i`
  993. <2> declare `long l`
  994. load from `int i` -> `int 4`;
  995. promote `int 4` and `long 5`: result `long`;
  996. implicit cast `int 4` to `long 4` -> `long 4`;
  997. bitwise and `long 4` and `long 5` -> `long 4`;
  998. store `long 4` to `l`
  999. +
  1000. * Bitwise and with the `def` type.
  1001. +
  1002. [source,Painless]
  1003. ----
  1004. <1> def x = 15 & 6;
  1005. <2> def y = x & 5;
  1006. ----
  1007. <1> declare `def x`;
  1008. bitwise and `int 15` and `int 6` -> `int 6`;
  1009. implicit cast `int 6` to `def` -> `def`;
  1010. store `def` in `x`
  1011. <2> declare `def y`;
  1012. load from `x` -> `def`;
  1013. implicit cast `def` to `int 6`;
  1014. bitwise and `int 6` and `int 5` -> `int 4`;
  1015. implicit cast `int 4` to `def` -> `def`;
  1016. store `def` to `y`
  1017. [[bitwise-xor-operator]]
  1018. ==== Bitwise Xor
  1019. Use the `bitwise xor operator '^'` to XOR together each bit within two integer
  1020. type values where if one bit is a `1` and the other bit is a `0` at the same
  1021. index the resultant bit is `1` otherwise the resultant bit is `0`.
  1022. *Errors*
  1023. * If either of the values is a non-integer type.
  1024. *Bits*
  1025. The following table illustrates the resultant bit from the xoring of two bits.
  1026. [cols="^1,^1,^1"]
  1027. |====
  1028. | | 1 | 0
  1029. | 1 | 0 | 1
  1030. | 0 | 1 | 0
  1031. |====
  1032. *Grammar*
  1033. [source,ANTLR4]
  1034. ----
  1035. bitwise_and: expression '^' expression;
  1036. ----
  1037. *Promotion*
  1038. [cols="<1,^1,^1,^1,^1,^1,^1"]
  1039. |====
  1040. | | byte | short | char | int | long | def
  1041. | byte | int | int | int | int | long | def
  1042. | short | int | int | int | int | long | def
  1043. | char | int | int | int | int | long | def
  1044. | int | int | int | int | int | long | def
  1045. | long | long | long | long | long | long | def
  1046. | def | def | def | def | def | def | def
  1047. |====
  1048. *Examples*
  1049. * Bitwise xor with different integer types.
  1050. +
  1051. [source,Painless]
  1052. ----
  1053. <1> int i = 5 ^ 6;
  1054. <2> long l = i ^ 5L;
  1055. ----
  1056. +
  1057. <1> declare `int i`;
  1058. bitwise xor `int 5` and `int 6` -> `int 3`;
  1059. store `int 3` in `i`
  1060. <2> declare `long l`
  1061. load from `int i` -> `int 4`;
  1062. promote `int 3` and `long 5`: result `long`;
  1063. implicit cast `int 3` to `long 3` -> `long 3`;
  1064. bitwise xor `long 3` and `long 5` -> `long 6`;
  1065. store `long 6` to `l`
  1066. +
  1067. * Bitwise xor with the `def` type.
  1068. +
  1069. [source,Painless]
  1070. ----
  1071. <1> def x = 15 ^ 6;
  1072. <2> def y = x ^ 5;
  1073. ----
  1074. <1> declare `def x`;
  1075. bitwise xor `int 15` and `int 6` -> `int 9`;
  1076. implicit cast `int 9` to `def` -> `def`;
  1077. store `def` in `x`
  1078. <2> declare `def y`;
  1079. load from `x` -> `def`;
  1080. implicit cast `def` to `int 9`;
  1081. bitwise xor `int 9` and `int 5` -> `int 12`;
  1082. implicit cast `int 12` to `def` -> `def`;
  1083. store `def` to `y`
  1084. [[bitwise-or-operator]]
  1085. ==== Bitwise Or
  1086. Use the `bitwise or operator '|'` to OR together each bit within two integer
  1087. type values where if at least one bit is a `1` at the same index the resultant
  1088. bit is `1` otherwise the resultant bit is `0`.
  1089. *Errors*
  1090. * If either of the values is a non-integer type.
  1091. *Bits*
  1092. The following table illustrates the resultant bit from the oring of two bits.
  1093. [cols="^1,^1,^1"]
  1094. |====
  1095. | | 1 | 0
  1096. | 1 | 1 | 1
  1097. | 0 | 1 | 0
  1098. |====
  1099. *Grammar*
  1100. [source,ANTLR4]
  1101. ----
  1102. bitwise_and: expression '|' expression;
  1103. ----
  1104. *Promotion*
  1105. [cols="<1,^1,^1,^1,^1,^1,^1"]
  1106. |====
  1107. | | byte | short | char | int | long | def
  1108. | byte | int | int | int | int | long | def
  1109. | short | int | int | int | int | long | def
  1110. | char | int | int | int | int | long | def
  1111. | int | int | int | int | int | long | def
  1112. | long | long | long | long | long | long | def
  1113. | def | def | def | def | def | def | def
  1114. |====
  1115. *Examples*
  1116. * Bitwise or with different integer types.
  1117. +
  1118. [source,Painless]
  1119. ----
  1120. <1> int i = 5 | 6;
  1121. <2> long l = i | 8L;
  1122. ----
  1123. +
  1124. <1> declare `int i`;
  1125. bitwise or `int 5` and `int 6` -> `int 7`;
  1126. store `int 7` in `i`
  1127. <2> declare `long l`
  1128. load from `int i` -> `int 7`;
  1129. promote `int 7` and `long 8`: result `long`;
  1130. implicit cast `int 7` to `long 7` -> `long 7`;
  1131. bitwise or `long 7` and `long 8` -> `long 15`;
  1132. store `long 15` to `l`
  1133. +
  1134. * Bitwise or with the `def` type.
  1135. +
  1136. [source,Painless]
  1137. ----
  1138. <1> def x = 5 ^ 6;
  1139. <2> def y = x ^ 8;
  1140. ----
  1141. <1> declare `def x`;
  1142. bitwise or `int 5` and `int 6` -> `int 7`;
  1143. implicit cast `int 7` to `def` -> `def`;
  1144. store `def` in `x`
  1145. <2> declare `def y`;
  1146. load from `x` -> `def`;
  1147. implicit cast `def` to `int 7`;
  1148. bitwise or `int 7` and `int 8` -> `int 15`;
  1149. implicit cast `int 15` to `def` -> `def`;
  1150. store `def` to `y`