Tag Archives: bit manipulation interview questions

Bit Manipülasyonları – 3. Kısım

Bit manipülasyonlarını, bilgisayar aritmetiğinde elde ettiğiniz verinin bitleri üzerinde değişiklikler/kontroller yaparak, elde etmek istediğiniz sonuca genellikle daha hızlı bir şekilde erişmenizi sağlayacak işlemler bütünü olarak adlandırabiliriz.

► Bit manipulation: https://en.wikipedia.org/wiki/Bit_manipulation

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and possibly other operations analogous to the boolean operators; there are also bit shifts and operations to count ones and zeros, find high and low one or zero, set, reset and test bits, extract and insert fields, mask and zero fields, gather and scatter bits to and from specified bit positions or fields. Integer arithmetic operators can also effect bit-operations in conjunction with the other operators.

► Bir manipülasyonları nerelerde kullanılır?

Eğer “bit manipülasyonu”nun faydalarını elde edemeyeceğiniz bir proje ise, muhtemelen bunları kullanmamalısınız. Tıpki bir çok enterprise uygulama gibi. İyi tarafları hızlı olmaları, kötü(?) tarafları anlaşılabilirliğin düşük olması, kod okunabilirliğini azaltmaları ve maintain edilebilmelerinin zorluğu diyebilirim. Bunun dışında, en küçük bir “performans” kaybı bile sizin için değerli ise, o zaman bunları kullanmayı düşünebilirsiniz. Örnek olarak, C ile driver geliştirirken, en derin seviyede bir matematiksel işlemler yapıyorsanız bunları kullanabilirsiniz. Genel olarak right shift ve left shift operatörleri, çarpma ve bölme işlemlerine göre genellikle hızlı çalışırlar. Tabi günümüz derleyicileri, bunları olabildiğince optimize ediyorlar hali hazırda.

        /// <summary>
        /// Sayını kaç adet bitten oluştuğunu bulur.
        /// </summary>
        static int BitCount(int n)
        {
            var bitCount = 0;

            while((1 << bitCount) <= n)
            {
                bitCount++;
            }

            return bitCount;

            // Örn: n = 5, yani 101, bizim 1 sayısını 3 defa ötelersek 1000
            // değerine, yani 5'ten büyük bir değer görüyoruz.
        }

        /// <summary>
        /// Belirli bir pozisyondaki bit değerini getirir.
        /// </summary>
        static int GetBit(int n, int position)
        {
            return (n >> position) & 1;

            // Ör: 11010 ve position = 3 olsun.
            // Sağa shift ettiğimizde elimizde 11 değeri kalır.
            // Bunu da 1 ile bitwise AND'lediğimizde, 
            // son bit eğer 1 ise 1, değilse 0 döndürür.
        }

        /// <summary>
        /// Sayı çift mi?
        /// </summary>
        static bool IsEven(int n)
        {
            return (n & 1) == 0;
        }

        /// <summary>
        /// Sayı pozitif mi?
        /// </summary>
        static bool IsPositive(int n)
        {
            if(n == 0)
            {
                return false;
            }

            return ((n >> 31) & 1) == 0;

            // 31 adet shift ile most significant bit'i buluruz.
            // Bu bit 0 ise pozitif, değilse negatiftir.
        }

Bit Manipülasyonları – 2. Kısım

Bit manipülasyonlarını, bilgisayar aritmetiğinde elde ettiğiniz verinin bitleri üzerinde değişiklikler/kontroller yaparak, elde etmek istediğiniz sonuca genellikle daha hızlı bir şekilde erişmenizi sağlayacak işlemler bütünü olarak adlandırabiliriz.

1. Kısım: https://youtu.be/OgphAV9JIKc

► Bit manipulation: https://en.wikipedia.org/wiki/Bit_manipulation

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and possibly other operations analogous to the boolean operators; there are also bit shifts and operations to count ones and zeros, find high and low one or zero, set, reset and test bits, extract and insert fields, mask and zero fields, gather and scatter bits to and from specified bit positions or fields. Integer arithmetic operators can also effect bit-operations in conjunction with the other operators.

Videoda olan metodların text hallerini şuradan alabilirsiniz: https://pastecode.io/s/jHQ2FokF1n

► Bir manipülasyonları nerelerde kullanılır?

Eğer “bit manipülasyonu”nun faydalarını elde edemeyeceğiniz bir proje ise, muhtemelen bunları kullanmamalısınız. Tıpki bir çok enterprise uygulama gibi. İyi tarafları hızlı olmaları, kötü(?) tarafları anlaşılabilirliğin düşük olması, kod okunabilirliğini azaltmaları ve maintain edilebilmelerinin zorluğu diyebilirim.

Bunun dışında, en küçük bir “performans” kaybı bile sizin için değerli ise, o zaman bunları kullanmayı düşünebilirsiniz. Örnek olarak, C ile driver geliştirirken, en derin seviyede bir matematiksel işlemler yapıyorsanız bunları kullanabilirsiniz. Genel olarak right shift ve left shift operatörleri, çarpma ve bölme işlemlerine göre genellikle hızlı çalışırlar. Tabi günümüz derleyicileri, bunları olabildiğince optimize ediyorlar hali hazırda.

Bit Manipülasyonları – 1. Kısım

Bit manipülasyonlarını, bilgisayar aritmetiğinde elde ettiğiniz verinin bitleri üzerinde değişiklikler/kontroller yaparak, elde etmek istediğiniz sonuca genellikle daha hızlı bir şekilde erişmenizi sağlayacak işlemler bütünü olarak adlandırabiliriz.

Ben de programlarınızı yazarken kullanabileceğiniz bazı temel bit manipülasyonlarını bir kaç kısımdan oluşacak şekilde anlatmaya çalışacağım bu seride.

► Bit manipulation: https://en.wikipedia.org/wiki/Bit_manipulation

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and possibly other operations analogous to the boolean operators; there are also bit shifts and operations to count ones and zeros, find high and low one or zero, set, reset and test bits, extract and insert fields, mask and zero fields, gather and scatter bits to and from specified bit positions or fields. Integer arithmetic operators can also effect bit-operations in conjunction with the other operators.

Videoda olan metodların text hallerini şuradan alabilirsiniz: https://pastecode.io/s/4W6HjKdEba

► Bir manipülasyonları nerelerde kullanılır?

Eğer “bit manipülasyonu”nun faydalarını elde edemeyeceğiniz bir proje ise, muhtemelen bunları kullanmamalısınız. Tıpki bir çok enterprise uygulama gibi. İyi tarafları hızlı olmaları, kötü(?) tarafları anlaşılabilirliğin düşük olması, kod okunabilirliğini azaltmaları ve maintain edilebilmelerinin zorluğu diyebilirim.

Bunun dışında, en küçük bir “performans” kaybı bile sizin için değerli ise, o zaman bunları kullanmayı düşünebilirsiniz. Örnek olarak, C ile driver geliştirirken, en derin seviyede bir matematiksel işlemler yapıyorsanız bunları kullanabilirsiniz. Genel olarak right shift ve left shift operatörleri, çarpma ve bölme işlemlerine göre genellikle hızlı çalışırlar. Tabi günümüz derleyicileri, bunları olabildiğince optimize ediyorlar hali hazırda.