Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,...

Notes on Bit Manipulation

Finding nth bit of an integer:

If you want to get the nth bit of a number, use this mask:

1 << n

So if you left shift 1 n times, you get the mask.

Below is the expression to find the nth bit:

number & (1 << n) 

Setting the nth bit to 1:

number | (1 << n)

Clearing the nth bit (setting it to zero):

We want to apply & operator with 0 to the nth bit. The mask we need is:

~(1 << n)

So the expression to clear the nth bit is:

number & ~(1 << n)

Flipping the nth bit:

We want to apply XOR with 1. Below is the expression:

number ^ (1 << n)


Finding if a bit in a position is set:

One way to do that is to right shift the number n times. Than the nth bit is the first bit. The following expression is the answer:

(number >> n) & 1


Finding number of 1 bits in unsigned integer

Loop through the bits and check. (Look at hammingWeight() method code below)

Finding Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. (Look at hammingDistance() method below)

https://www.youtube.com/watch?v=7jkIUgLC29I

TODO:

  • Modify Bit
  • Write a func that counts the number of bits different between two numbers

Summary:

public class Main {

    static int getNthBit(int value, int n) {


        int mask = 1 << n;


        return (value & mask) == 0 ? 0 : 1;


    }


    static int setNthBit(int value, int n) {


        int mask = 1 << n;


        return (value | mask);

    }

    static int clearNthBit(int value, int n) {


        int mask = ~(1 << n);


        return (value & mask);

    }

    static int flipNthBit(int value, int n) {


        int mask = (1 << n);


        return (value ^ mask);


    }

    static boolean isBitSet(int value, int n) {


        int mask = 1 << n;


        return (value & mask) != 0;

    }

    static boolean isBitSet2(int value, int n) {


        int shifted = value >> n;


        return (shifted & 1) != 0;


    }

    public static int hammingWeight(int n) {


        int count = 0;


        for (int i = 0; i < 32; ++i) {


            if ((n & (1 << i)) != 0) {

                ++count;
            }

        }


        return count;


    }

    public static int hammingDistance(int x, int y) {


        int d = 0;


        for (int i = 0; i < 32; ++i) {


            if ((x & (1 << i)) != ((y & (1 << i)))) {

                ++d;
            }

        }


        return d;


    }


    public static void main(String[] args) {


        int x = 5;


        System.out.println(getNthBit(x, 1));


        System.out.println(setNthBit(x, 1));


        System.out.println(clearNthBit(x, 1));

    }
}






Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

Virtual Memory

NOTES ON COMPUTER ARCHITECTURE: Some important concepts in computer architecture