binary Archives - SoftUni Global https://softuni.org/tag/binary/ Learn Programming and Start a Developer Job Thu, 28 Apr 2022 11:27:11 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.3 https://softuni.org/wp-content/uploads/2022/04/cropped-SoftUni-Global-Logo-Square-notext-32x32.png binary Archives - SoftUni Global https://softuni.org/tag/binary/ 32 32 Bitwise Operations in Programming [Dev Concepts #34] https://softuni.org/dev-concepts/bitwise-operations-in-programming/ https://softuni.org/dev-concepts/bitwise-operations-in-programming/#respond Wed, 27 Apr 2022 06:00:00 +0000 https://softuni.org/?p=18889 In this lesson you will get an idea of bitwise operations: how they work and why we need them in computer programming. We will explain and demonstrate the main bitwise operators and introduce the concept of bitmasks.

The post Bitwise Operations in Programming [Dev Concepts #34] appeared first on SoftUni Global.

]]>

In this article, we will also solve several practical problems using bitwise operations:

  • Get the last bit from an integer
  • Get the bit at a certain index from an integer
  • Change the bit at a certain index in an integer
  • Extract the bit before the last from an integer

If you are not familiar with bits and storing data on the computer you can read our previous articles about them here:

Bitwise Operations

First, let’s start with learning about the bitwise operations in programming. They work with the binary representations of the numbers, applying bit-by-bit calculations. For example, if we have two 8-bit numbers, we can apply a bitwise operation, which takes as input the first 8 bits and the second 8 bits and produces as a result new 8 bits.

A simple bitwise operator over a single argument is the “tilde” operator – the bitwise logical NOT (also called negation). The operator “tilde” turns all zeroes to ones and all ones to zeroes, like the “exclamation mark” operator for the Boolean expressions, but it works bit by bit. For example, if we have the binary number1 0 0″, its negation “tilde 1 0 0” is “0 1 1“.

OperatorTable

The table above illustrates the work of the bitwise OR, AND, and XOR operators.

  • The bitwise OR operator (denoted by the vertical bar in most programming languages) returns 1, if one of its input bits is 1, otherwise returns 0.
  • The bitwise AND operator (denoted by the ampersand in most programming languages) returns 1, if both of its input bits are 1, otherwise returns 0.
  • The bitwise exclusive OR (XOR) operator (denoted by the ampersand in most programming languages) returns 1 if one of its arguments is 1, but not both at the same time, otherwise returns 0.

Bit Shifts

Bit shifts are bitwise operations, where bits inside a number are moved (or shifted) to the left or the right. During the shifting operation, the bits that fall at invalid positions are lost, and the bits which come from missing positions are replaced by 0.

left-and-right-shift

Bit shifting can be applied for 8-bit, 16-bit, 32-bit, and 64-bit numbers, as well as for numbers of other sizes in bits. The bit size of the number being shifted defines the valid bit positions and where the bits get lost. Bits can be shifted by more than 1 position. For example, 5 shifted left twice is 20 and 5 shifted right twice is 1.

Why We Need Bitwise Operations?​

Processing bits is important for many fields of computer science, information technologies, and software systems, like networking protocols, data storage, and file systems, binary file formatsmemory management, data compression, data encryption, video streaming, Internet of things (IoT) systems, low-level programming, computer graphics, and many others.

Binary-and-Text-File

Data compression algorithms replace bit or byte sequences with shorter bit sequences. For example, the “DEFLATEalgorithm, used to compress data in the ZIP files, finds the most often sequences and replaces them with shorter sequences, while it preserves a dictionary between the original bit sequences and their shorter compressed form. This is done using heavy bit-level processing with bitwise operations.

Many binary file formats use bits to save space. For example, PNG images (the Portable Network Graphics image format) use 3 bits to specify the color format used (8-bit color, 24-bit color, 32-bit color with transparency). These 3 bits are located at a certain offset in the PNG image header bytes, so reading and writing the value encoded in these 3 bits require bitwise operations.

To sum it up, bitwise operators work with the binary representations of the numbers, applying bit-by-bit calculations. Bit shifts are bitwise operations, where bits inside a number are shifted to the left or the right. These concepts are an important aspect of many fields of computer science.

Lesson Topics

In this tutorial, we cover the following topics:
  • Bitwise Operations

  • Bitwise Operators – Examples

  • Bit Shifts

  • Bitwise Operations Problems

  • Why We Need Bitwise Operations?

  • Bit Before the Last – Problems

Lesson Slides

The post Bitwise Operations in Programming [Dev Concepts #34] appeared first on SoftUni Global.

]]>
https://softuni.org/dev-concepts/bitwise-operations-in-programming/feed/ 0
Data Representation in Computer Memory [Dev Concepts #33] https://softuni.org/dev-concepts/data-representation-in-computer-memory/ https://softuni.org/dev-concepts/data-representation-in-computer-memory/#respond Thu, 31 Mar 2022 06:00:00 +0000 https://softuni.org/?p=16611 In this article of the series Dev Concepts, we take a look at the binary representation of integers, floating-point numbers, text, and unicode.

The post Data Representation in Computer Memory [Dev Concepts #33] appeared first on SoftUni Global.

]]>

In this lesson, we will talk about storing data in the computer memory. By the end of this article, you will know how to work with binary representation of integers, floating-point numbers, text, and Unicode.

Integer numbers are represented in the computer memory, as a sequence of bits: 8-bits, 16-bits, 24-bits, 32-bits, 64-bits, and others, but always a multiple of 8 (one byte). They can be signed or unsigned and depending on this, hold a positive, or negative value. Some values in the real world can only be positive – the number of students enrolled in a class. There can be also negative values in the real world such as daily temperature.

Positive 8-bit integers have a leading 0, followed by 7 other bits. Their format matches the pattern “0XXXXXXX” (positive sign + 7 significant bits). Their value is the decimal value of their significant bits (the last 7 bits).

Negative 8-bit integers have a leading one, followed by 7 other bits. Their format matches the pattern “1YYYYYYY” (negative sign + 7 significant bits). Their value is -128 (which is minus 2 to the power of 7) plus the decimal value of their significant bits.

8-bit-binary-integer

Example of signed 8-bit binary integer

The table below summarizes the ranges of the integer data types in most popular programming languages, which follow the underlying number representations that we discussed in this lesson. Most programming languages also have 64-bit signed and unsigned integers, which behave just like the other integer types but have significantly larger ranges.

ranges-of-integer-data-types

  • The 8-bit signed integers have a range from -128 to 127. This is the sbyte type in C# and the byte type in Java.
  • The 8-bit unsigned integers have a range from 0 to 255. This is the byte type in C#.
  • The 16-bit signed integers have a range from -32768 to 32767. This is the short type in Java, C#.
  • The 16-bit unsigned integers have a range from 0 to 65536. This is the ushort type in C#.
  • The 32-bit signed integers have a range from -231231-1 (which is from minus 2 billion to 2 billion roughly).  This is the int type in C#, Java, and most other languages. This 32-bit signed integer data type is the most often used in computer programming. Most developers write “int” when they need just a number, without worrying about the range of its possible values because the range of “int” is large enough for most use cases.

Representing Text

Computers represent text characters as unsigned integer numbers, which means that letters are sequences of bits, just like numbers.

The ASCII standard represents text characters as 8-bit integers. It is one of the oldest standards in the computer industry, which defines mappings between letters and unsigned integers. It simply assigns a unique number for each letter and thus allows letters to be encoded as numbers.

representing-textFor example, the letter “A” has ASCII code 65. The letter “B” has ASCII code 66. The “plus sign” has ASCII code 43. The hex and binary values are also shown and are useful in some situations.

Representing Unicode Text

The Unicode standard represents more than 100,000 text characters as 16-bit integers. Unlike ASCII it uses more bits per character and therefore it can represent texts in many languages and alphabets, like Latin, Cyrillic, Arabic, Chinese, Greek, Korean, Japanese, and many others. 

Here are a few examples of Unicode characters:

representing-unicode-text

  • The Latin letter “A” has Unicode number 65.
  • The Cyrillic letter “sht” has Unicode number 1097.
  • The Arabic letter “beh” has Unicode number 1576.
  • The “guitar” emoji symbol has Unicode number 127928.

In any programming language, we either declare data type before using a variable, or the language automatically assigns a specific data type. In this lesson, we have learned how computers store integer numbers, floating-point numbers, text, and other data. These concepts shouldn’t be taken lightly, and be careful with them!

Lesson Topics

In this tutorial we cover the following topics:
  • Representation of Data

  • Representing Integers in Memory

  • Representation of Signed Integers

  • Largest and Smallest Signed Integers

  • Integers and Their Ranges in Programming

  • Representing Real Numbers

  • Storing Floating-Point Numbers

  • Representing Text and Unicode Text

  • Sequences of Characters

Lesson Slides

The post Data Representation in Computer Memory [Dev Concepts #33] appeared first on SoftUni Global.

]]>
https://softuni.org/dev-concepts/data-representation-in-computer-memory/feed/ 0
Numeral Systems in Programming [Dev Concepts #32] https://softuni.org/dev-concepts/numeral-systems-in-programming/ https://softuni.org/dev-concepts/numeral-systems-in-programming/#respond Thu, 24 Mar 2022 06:00:00 +0000 https://softuni.org/?p=15750 In this article of the series Dev Concepts, we take a look at Binary, Decimal, Hexadecimal, and Conversions.

The post Numeral Systems in Programming [Dev Concepts #32] appeared first on SoftUni Global.

]]>

In this lesson, we will talk about numeral systems, which are widely used in computer programming. By the end of it, you will know how to use the binarydecimal, and hexadecimal numeral systems, their characteristics, and how to convert integers from one numeral system to another.

Numeral systems represent numbers in written form using sequences of digits. For example: the digit “4“, followed by the digit “2” in the traditional decimal system used by humans, represents the number “42.

Many systems can be used to represent numbers, like the Hindu–Arabic numerals, the Roman numerals, and the Hebrew numerals. In computer science, specific numeral systems are of big importance: the positional numeral systems. In the positional numeral systems, the value of each digit depends on its position. In the integer numbers, the digits on the left have a bigger weight than the digits, staying on the right.

Positional numeral systems use the so-called base (a number like 2, 10, or 16) that specifies how many digits are used to represent a number. For example, the decimal system uses 10 digits: 1, 2, 3, 4, 5, 6, 7, 8, 9, and 0. The binary system uses only two digits: 1 and 0. The hexadecimal system uses 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

hex-to-decimal

On the image, you can see the decimal, binary and hexadecimal representations of the numbers 3045, and 60.

Decimal numbers use a positional numeral system of base 10. Decimal numbers are the traditional numbers used by humans in their everyday life.

Decimal numbers are represented by the following 10 digits: 012345678, and 9.

Each position in a decimal number corresponds to a certain power of 10. The rightmost position is multiplied by 1 (which is 10 raised to the power of 0), the next position on the left is multiplied by 10 (which is 10 raised to the power of 1), the next position on the left is multiplied by 100 (which is 10 raised to the power of 2), and so on. 

Four hundred and one is equal to:

  • 4 multiplied to 10 to the power of 2 + 0 multiplied to 10 to the power of 1 + 1 multiplied to 10 to the power of 0
  • which is equal to 4 multiplied by 100 + 0 multiplied by 10 + 1 multiplied by 1
  • which is equal to 400 + 0 + 1
  • which is equal to 401

conversion-model

We can think of decimal numbers as polynomials of their digits in the following form:

conversion-formula

The binary numeral systemis fundamental for computer systems. It uses base 2 and only two digits: 1 and 0Binary numbers (numbers of base 2) are sequences of zeroes and ones. For example: 5 (in decimal) is equal to 1 0 1 in binary. We denote binary numbers with a small suffix “b” at the end.

Hexadecimal numbers (also known as hex numbers) are widely used in computer science. Hex numbers use base 16 and are represented by a sequence of hex digits. The hex digits are the following literals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Note that after 9 the next digit is A, which has a decimal value of 10. The next hex digits after A are B, C, D, E, and F and they have decimal values of 11121314, and 15. These decimal values are used when we convert a hex number to a decimal value.

That is the main idea about numeral systems. They are used by humans, and computers, to write numbers using digits. It is something that you shouldn’t take lightly, and be careful!

Lesson Topics

In this tutorial we cover the following topics:
  • Numeral Systems

  • Decimal Numbers

  • Binary Numbers

  • Binary and Decimal Conversion

  • Hexadecimal Numbers

  • Hex to Decimal Conversion

  • Hex to Binary Conversion

Lesson Slides

The post Numeral Systems in Programming [Dev Concepts #32] appeared first on SoftUni Global.

]]>
https://softuni.org/dev-concepts/numeral-systems-in-programming/feed/ 0