/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
int ans = ~(~x|~y); //logical equivalent
return ans;
}
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
return (x >> (n << 3)) & 0xFF;
}
/*
* conditional - same as x ? y : z
* Example: conditional(2,4,5) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int conditional(int x, int y, int z) {
int a = !!x;
int b = ~a+0x01;
return (b & y) | (~b & z);
}
/*
* rotateLeft - Rotate x to the left by n
* Can assume that 0 <= n <= 31
* Examples: rotateLeft(0x87654321,4) = 0x76543218
* Legal ops: ~ & ^ | + << >>
* Max ops: 25
* Rating: 3
*/
int rotateLeft(int x, int n) {
int c = 127;
int c2 = 255;
return (((x>>1)&((c<<24)+(c2<<16)+(c2<<8)+c2))>>(31&(~n)))+(x<<n);
}
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
return ((~(~x+0x01) & ~x )>>0x1F) & 0x01;;
}
/*
* fitsShort - return 1 if x can be represented as a
* 16-bit, two's complement integer.
* Examples: fitsShort(33000) = 0, fitsShort(-32768) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 1
*/
int fitsShort(int x) {
return !(((x << (33+~16)) >> (33+~16)) ^ x);
}
/*
* byteSwap - swaps the nth byte and the mth byte
* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
* You may assume that 0 <= n <= 3, 0 <= m <= 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 2
* stored the two bytes, created third int that had
*/
int byteSwap(int x, int n, int m) {
int i = (((x & (0xFF<<(n<<3)))>>(n<<3))&0xFF)<<(m<<3);
int j = (((x & (0xFF<<(m<<3)))>>(m<<3))&0xFF)<<(n<<3);
int ans = (x & ~((0xFF<<(n<<3)) | (0xFF<<(m<<3))));
return ans|i|j;
}
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*
*/
int divpwr2(int x, int n) {
int mask = (1 << n) + ~0;
int equalizer = (x >> 31) & mask;
return (x + equalizer) >> n;}
/*
* negate - return -x
* Example: negate(1) = -1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int negate(int x) {
return ~x+1;
}
/*
* isAsciiDigit - return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')
* Example: isAsciiDigit(0x35) = 1.
* isAsciiDigit(0x3a) = 0.
* isAsciiDigit(0x05) = 0.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 3
* use xor with 30
*/
int isAsciiDigit(int x) {
int filter;
int checkThirty;
int checkSeven;
int checkNine;
filter = 0x30;
checkThirty = x ^ filter;
filter = 0x9;
checkNine = (checkThirty & ~filter);
filter = 0x7;
checkSeven = checkThirty & ~filter;
return !checkSeven | !checkNine;;
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int sign_y,sign_x,comb_sign_xy,override_byte,z;
sign_y=y>>31;
sign_x=x>>31;
comb_sign_xy=sign_x^sign_y;
override_byte=comb_sign_xy&sign_x;
z=((y+(~x)+1))>>31;
return !((z|comb_sign_xy)^override_byte);
}
/*
* ilog2 - return floor(log base 2 of x), where x > 0
* Example: ilog2(16) = 4
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int ilog2(int x) {
int high_bytes=(0xFF<<24) + (0xFF<<16);
int third_byte=0xFF<<8;
int high_bits=0xF0;
int mid_bits=0x0C;
int twos_bit=0x02;
int shift1 = (!!(x & high_bytes)) << 4;
x = x >> shift1;
int shift2 = (!!(x & third_byte)) << 3;
x = x >> shift2;
int shift3 = (!!(x & high_bits)) << 2;
x = x >> shift3;
int shift4 = (!!(x & mid_bits)) << 1;
x = x >> shift4;
int shift5 = (!!(x & twos_bit));
int ans = shift1 + shift2 + shift3 + shift4 + shift5;
return ans;
}
/*
* float_neg - Return bit-level equivalent of expression -f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_neg(unsigned uf) {
int signless = ((~(1<<31))&uf);
int exp = signless>>23;
if (exp==0xFF){
int fracshift=uf<<9;
if(fracshift!=0){
return uf;
}
}
return uf^(1<<31);
}
/*
* float_abs - Return bit-level equivalent of absolute value of f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representations of
* single-precision floating point values.
* When argument is NaN, return argument..
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 10
* Rating: 2
*/
unsigned float_abs(unsigned uf) {
int signless = ((~(1<<31))&uf);
int exp = signless>>23;
if (exp==0xFF){
int fracshift=uf<<9;
if(fracshift!=0){
return uf;
}
}
return uf&(~(1<<31));
}
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
unsigned frac = x;
if(x==0){
return 0;
}
unsigned sign=0;
if (x<0){
sign = 1<<31;
frac = -x;
}
int count = 0;
while((frac & 1<<31)==0){
frac=frac<<1;
count = count + 1;
}
frac = frac<<1;
count = count + 1;
int E = 32 - count;
int exp = E + 127;
int low_nine = frac & (0x000001ff);
int tenth = frac & (0x200);
frac = frac >> 9;
int ans = sign + (exp<<23) + frac;
//rounding
//int low_nine = frac & ((1<<8) + 0xFF);
//int tenth = frac & (1<<9);
if(low_nine > 0x00000100 || ((low_nine == 0x00000100) && (tenth>>9))){
ans = ans+1;
}
return ans;
}
{
// your code goes here
}
}