DEV Community

Discussion on: Daily Challenge #255 - Is There an Odd Bit?

Collapse
 
aminnairi profile image
Amin

Go

package odd

func AnyOdd(number uint) uint8  {
    for index := 0; number != 0; index, number = index + 1, number / 2 {
        if index % 2 != 0 && number % 2 == 1 {
            return 1
        }
    }

    return 0
}

Tests

package odd_test

import (
    "testing"
    "github.com/aminnairi/odd"
)

func TestOnes(t *testing.T) {
    var expectation uint8 = 1

    ones := []uint{170, 2, 7, 10}

    for _, value := range ones {
        result := odd.AnyOdd(value)

        if result != expectation {
            t.Errorf("oddAnyOdd(%d) should be %d, %d received", value, expectation, result)
        }
    }
}

func TestZeros(t *testing.T) {
    var expectation uint8 = 0

    zeros := []uint{5, 21, 85, 341}

    for _, value := range zeros {
        result := odd.AnyOdd(value)

        if result != expectation {
            t.Errorf("oddAnyOdd(%d) should be %d, %d received", value, expectation, result)
        }
    }
}