Mod operations and its application in bit-wise operations Complete this if state
ID: 3812117 • Letter: M
Question
Mod operations and its application in bit-wise operations
Complete this if statement to print if a value is odd:
if ( ______________________________________________ )
{
System.out.print(value+” is an odd number!”);
}
------------------------------------------------------------------
Describe what do these operations do?
1. someValue%(1<<8)
2. (someValue<<29)>>29
3. someValue%(1<<29)
4. someValue%(1<<N) //where N is an int
plase help me! Thanks in advance! (java programming)
Explanation / Answer
To check whether a given number is odd or not, the number when divided by 2, gives a remainder
of 1 is called odd, and which gives a remainder of 0 is called even.
if ( value % 2 == 1 )
{
System.out.print(value+” is an odd number!”);
}
someValue%(1<<8) A decimal value 1 is shifted to the left by 8 bits.
So, a value of 0000 0000 0001 will now make it: 0001 0000 0000 = 2^8 = 256.
Therefore, the number someValue will be modulo divided with 256, which means the expression
will give the remainder when someValue is divided by 256.
For example 91 % 256 = 91. 260 % 256 = 4.
(someValue<<29)>>29 A decimal value entered by the user is shifted to the left by 29 bits.
And then the result is shifted right by 29 bits again. Which means if the number is 32 bit,
the value after shifting it to the left by 29 bits, and then shifting back to the right by
29 bits will now preserve only the 2 least significant bits Therefore, can store values in
the range: -4 to 3.
someValue%(1<<29) A decimal value 1 shifted to the left by 29 bits.
So, a value will now be 2^29 = 536870912.
Therefore, the number someValue will be modulo divided with 536870912, which means the
expression will give the remainder when someValue is divided by 536870912.
someValue%(1<<N) A decimal value 1 shifted to the left by N bits.
So, a value will now be 2^N.
Therefore, the number someValue will be modulo divided with 2^N.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.