Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Binary and Hexadecimal numeral systems

Similar presentations


Presentation on theme: "Searching Binary and Hexadecimal numeral systems"— Presentation transcript:

1 Searching Binary and Hexadecimal numeral systems
Lecture 9 Searching Binary and Hexadecimal numeral systems

2 Outcomes Understand Sequential search
Understand searching for extremes Understand Binary search Understand Binary numbers Understand Hexadecimal

3 Sequential (Linear Search)
Start at the beginning of the array and compare each accessed array element to the value you’re searching for.

4 Sequential Search Function SeqSearch(arr() As Integer, sValue As Integer) As Integer Dim index As Integer For index = 0 To arr.Length - 1 If arr(index) = sValue Then Return arr(index) End If Next Return Nothing End Function

5 Sequential Search No prerequisites Worst-case performance - O(n)

6 Search for Min Function FindMin(arr() As Integer) As Integer Dim min As Integer = arr(0) Dim index As Integer For index = 1 To arr.Length - 1 If arr(index) < min Then min = arr(index) End If Next Return min End Function

7 Search for Max Function FindMax(arr() As Integer) As Integer Dim max As Integer = arr(0) Dim index As Integer For index = 1 To arr.Length - 1 If arr(index) > max Then max = arr(index) End If Next Return max End Function

8 Binary Search 50 12 38 25 75 19 6 44 32 56 69 82 94 88 62

9 Binary Search 50 25 12 15 19 15

10 Binary Search Possible only over sorted array
Worst-case performance – O(log2n)

11 Binary Search Public Function BinSearch(arr() As Integer, value As Integer) As Integer Dim upperBound, lowerBound, mid As Integer upperBound = arr.Length - 1 lowerBound = 0 While (lowerBound <= upperBound) mid = (upperBound + lowerBound) \ 2 If arr(mid) = value Then Return mid ElseIf value < arr(mid) Then upperBound = mid - 1 Else lowerBound = mid + 1 End If End While Return -1 End Function

12 Recursive Binary Search
Public Function RbinSearch(arr() As Integer, value As Integer, lower As Integer, upper As Integer) As Integer If (lower > upper) Then Return -1 Else Dim mid As Integer mid = (upper + lower) \ 2 If value < arr(mid) Then RbinSearch(arr, value, lower, mid - 1) ElseIf value = arr(mid) Then Return mid RbinSearch(arr, value, mid + 1, upper) End If End Function

13 Binary Binary numbers and arithmetic let you represent any amount you want using just two digits: 0 and 1. Binary numbers is the core for any computer system since they represent the only states possible: Presence and absence of electrical charge

14 Conversion to decimal Each digit in a binary number represents 2 in the corresponding power:         In order to convert a binary number to decimal you just have to sum up the numbers

15 Conversion to decimal = 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 = = 255 If a number is 0, just ignore corresponding power of two as 0*2N = 0 10102 = 1*23 + 0*22 + 1*21 + 0*20 = = 10

16 Addition 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0, and carry 1 to the next more significant bit

17 Addition 11101 1 1 1

18 Subtraction 0 - 0 = 0 0 - 1 = 1, and borrow 1 from the next more significant bit 1 - 0 = 1 1 - 1 = 0

19 Subtraction 11101 1 1

20 Multiplication 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0
1 x 1 = 1, and no carry or borrow bits

21 Multiplication 1101 * 0000

22 Decimal to binary 100 50 25 12 6 3 1 1 1 1

23 Hexadecimal Because binary numbers are rather unwieldy, programmers prefer to use a more compact way to represent them. Historically, octal (base 8) and hexadecimal (base 16, or hex) have been used.

24 Hexadecimal Octal has the advantage that it uses only familiar digits, but it groups digits by threes, which is inconvenient for word sizes that are multiples of 4. So it is seldom used nowadays. Hexadecimal works nicely for bytes and for word sizes that are multiples of 4, but requires the introduction of 6 new digits.

25 Binary and Hexadecimal
Hexadecimal works by grouping binary bits into groups of 4 (starting from the right). Each group is assigned a hex digit value. The digits are the same as for decimal up to 9, and then letters A through F are used for 10 through 15. 0000 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A 0011 = 3 1011 = B 0100 = 4 1100 = C 0101 = 5 1101 = D 0110 = 6 1110 = E 0111 = 7 1111 = F Thus the 16-bit binary number converted to hex is B2A9

26 The RGB Color System HTML styles define only 16 colors that are guaranteed to be recognized by name. So many web developers use “RGB” values to define colors. RGB values are based on the Red, Green, Blue primary color system. Just about every color that the human eye can perceive can be represented by a combination of specific amounts of each primary color. To define colors numerically, each of the three primary colors is assigned a numerical strength. The three strengths of Red, Green, and Blue define the color.

27 Expressing Colors in Hex
It is convenient to define the strengths of the primary colors using 8-bit numbers, or bytes. An 8-bit number can be represented using two hex digits. The value of the hex number ranges from 00 to FF, corresponding to a range from 0 to 255 decimal. A strength of 0 means the color is completely absent. A strength of FF or 255 means the color is at its maximum. An RGB color is thus represented by three 8-bit numbers, or six hex digits. In HTML, a value is signified as hexadecimal by prefixing it with a ‘#’ sign. For example, "#E703A0" would be such a value.

28 Expressing Colors in Hex
"#000000" Black (all colors off) "#FFFFFF" White (all colors max) "#FF0000" Red (red max, green & blue off) "#00FF00" Green "#0000FF" Blue "#FFFF00" Yellow (max red & green) "#AEEEEE" Turquoise "#808080" Gray (all colors half max)

29 The End


Download ppt "Searching Binary and Hexadecimal numeral systems"

Similar presentations


Ads by Google