India Jobs- IT Fresher Job in Delhi, Bangalore, Mumbai, Chennai.
www.Lkshya.com
Ask Your Query

Q. How are positive(+ve) and negative(-ve) numbers represented internally in Java?




Characters Left
6 + 5
Sami
Negative numbers in Java are represented using 2's complement. As we know that integers in Java occupy 4 bytes so to understand how a negative integer (say -4) is represented internally in Java, we first need to find the binary equivalent of the positive value of the integer (in this case 4) and subsequently by finding the 2's complement of that binary representation. Okay, so how do find 2's complement of a binary number? Simply by adding '1' to the 1's complement of that number. But, how to find 1's complement of a binary number then? Just by reversing the bits of the number i.e., changing 1s to 0s and 0s to 1s. An example may of some help here. ... int i = -4; ... Step #1: Binary Equivalent of the positive value (4 in this case) 0000 0000 0000 0000 0000 0000 0000 0100 Step #2: 1's complement of the binary rep of 4 by inverting the bits 1111 1111 1111 1111 1111 1111 1111 1011 Step #3: Finding 2's complement by adding 1 to the corresponding 1's complement 1111 1111 1111 1111 1111 1111 1111 1011 0000 0000 0000 0000 0000 0000 0000 0001 --------------------------------------- 1111 1111 1111 1111 1111 1111 1111 1100 Thus, we see that integer -4 is represented by the binary sequence (1111 1111 1111 1111 1111 1111 1111 1100) in Java.
Vivek
positive numbers are represented in binary using 1's complement, and negative numbers using 2's complement in Java.
Top Answered Questions Good at tech answer these

Some other Good Questions

Q:1 What is a Constructor in Java?

Q:2 How are positive(+ve) and negative(-ve) numbers represented internally in Java?

Q:3 What do Trojans, Viruses, and Other Malware do to our systems?

Q:4 Can abstract class have constructor in java?

Q:5 Difference between checked vs unchecked exception in Java?

Q:6 When is checked exception in Java?

Q:7 What is difference between == and equals() while comparing strings? Which one is reliable?

Q:10 Can we call a class a data type?

Q:11 What is the difference between #include and import statement?

Q:12 What is JIT Compiler?

Q:13 What is a swap space in Linux?

Q:14 What is the basic difference between BASH and DOS?

Q:15 How do you refer to the parallel port where devices such as printers are connected?

Q:16 How do you switch from one desktop environment to another, such as switching from KDE to Gnome?

Q:17 How does case sensitivity affect the way you use commands?

Q:18 Is it possible to use shortcut for a long pathname?

Q:19 How do you terminate an ongoing process?

Q:20 Write a command that will display all .txt files, including its individual permission in Linux.

India Jobs-How to get fresher software jobs in India| Search Jobs