4 Replies - 213 Views - Last Post: 15 July 2012 - 05:57 PM Rate Topic: -----

#1 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

bit-level operations

Posted 15 July 2012 - 03:54 AM

I found a source code from book.
But I don't understand the source code.
Can someone explain this source code deeply for me?
import java.util.Scanner;

public class Exercise46 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter an integer: ");
		int value = input.nextInt();
		    
		System.out.print("The 16 bits are ");
		int mask = 1;
		for (int i = 15; i >= 0; i--) {
		      int temp = value >> i;
		      int bit = temp & mask;
		      System.out.print(bit);
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: bit-level operations

#2 varsha0802  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 16-October 10

Re: bit-level operations

Posted 15 July 2012 - 05:57 AM

The >> operator shifts the bits of the variable by the value indicated after the operator. So, the bits of the variable value will be shifted right by the places indicated in i. The mask variable is storing value 1. So, when it will be Anded with temp, only the rightmost bit will be having value (0 or 1 depending on the bit value in temp), rest will have 0's (it'll mask all the bits except the rightmost one). So, the output will be 0 or 1 depending on the bit values in variable value.
Was This Post Helpful? 0
  • +
  • -

#3 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: bit-level operations

Posted 15 July 2012 - 08:13 AM

Can give me some example, like when the input is 5, what will happen?
I still can't trace the program!
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: bit-level operations

Posted 15 July 2012 - 09:28 AM

The code you posted prints the lower order 16 bits of the integer the user enters.

5 is 0101 in binary. A variable of type int is 32-bits in size, so the 5 is zero extended to make up the extra 29 bits, so its full form in this case is 0000 0000 0000 0000 0000 0000 0000 0101.

The variable mask is of value 1, or in binary after zero extension, 0000 0000 0000 0000 0000 0000 0000 0001

To make things quicker and easy, I'll change the loop so it only loops 4 times, as so:

for (int i = 3; i >= 0; i--) {
    ...
}



and use only the 0101 bits of value.

On the first iteration, i = 3, meaning value will be arithmetically right shifted ( >> ) by 3, meaning each bit in value will be move to the right by 3, so:

0101 arithmetically shifted to the right by 3 is 0000

0000 & 0001 = 0000 = 0

So, 0 is printed.

i = 2

0101 arithmetically shifted to the right by 2 is 0001

0001 & 0001 = 0001 = 1

So 1 is printed.

i = 1

0101 arithmetically shifted to the right by 1 is 0010

0010 & 0001 = 0000 = 0

So 0 is printed.

i = 0

0101 arithmetically shifted to the right by 0 is 0101

0101 & 0001 = 0001 = 1

So 1 is printed

Therefore, the final output is 0101, which is the first 4 bits of the number 5 in binary.

This post has been edited by CodingSup3rnatur@l-360: 15 July 2012 - 10:11 AM

Was This Post Helpful? 2
  • +
  • -

#5 UrIkOn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-November 11

Re: bit-level operations

Posted 15 July 2012 - 05:57 PM

Thank you for your explanation.
Now I can understand fully and better.
Thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1