Binary numbers
“They say that a bad programmer is one who believes that there are 1000 bytes in one kilobyte, and a good programmer is one who believes that there are 1024 meters in one kilometer.” Many people understand this joke, since everyone knows that in processes related to computer science and computer technology, there are many values expressed as powers of two, that is, numbers of the form 2^K, where K is some non-negative integer. Let's call such numbers binary. These are numbers such as 1, 2, 4, 8, 16, 32, etc. Indeed, when it comes to memory size or monitor screen resolution, we often come across binary numbers. All this is connected with the principle of storing information in computer memory. An integer N is given. You need to determine whether it is binary. Write a public static boolean isBinary(int n) function that returns true if n is a power of two, false otherwise. Add. task: Try to solve the problem without using conditions and ternary operators ("?:"). Solution:public static boolean isBinary(int n) { return n > 0 && (n & n - 1) == 0; }
GO TO FULL VERSION