0% found this document useful (0 votes)
5 views

Bit_Manipulation_Conditional_Statement_u

This paper discusses the concept of using bitwise operators in C++ to create conditional statements, highlighting the importance of understanding bit manipulation in programming. It explores various bitwise operations such as AND, OR, NOT, and XOR, and their applications in implementing conditional logic without traditional conditional operators. The research aims to enhance comprehension of bit-level operations and their efficiency in programming tasks.

Uploaded by

arunprince538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Bit_Manipulation_Conditional_Statement_u

This paper discusses the concept of using bitwise operators in C++ to create conditional statements, highlighting the importance of understanding bit manipulation in programming. It explores various bitwise operations such as AND, OR, NOT, and XOR, and their applications in implementing conditional logic without traditional conditional operators. The research aims to enhance comprehension of bit-level operations and their efficiency in programming tasks.

Uploaded by

arunprince538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DOI: 10.14421/ijid.2020.

09102 IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448


Vol. 9, No. 1, 2020, Pp. 9-14

Bit Manipulation: Conditional Statement using


Bit-wise operators with C++
Rahmawati Nafi'ah1, Wakhid Kurniawan2, Johan Setiawan3, Khoirul Umam4
Student of Department of Informatics
1, 2, 3, 4

Informatics Graduate Program, Faculty of Science and Technology


UIN Sunan Kalijaga Yogyakarta
2
PT. Cobra Dental Indonesia
3
Solusi Pembayaran Elektronik
4
Coding Collective
1
[email protected], [email protected], [email protected],
2
4
[email protected]

Article History
Received May 19th, 2020
Revised June 23rd, 2020
Accepted July 21st, 2020
Published July 21st, 2020

Abstract—All of information that manipulated by a computer is represented in the form of bits, so in the programming language it is
necessary to understand bitwise operations at the first. This paper aims to create a concept of making Conditional Statements with
Bitwise operators in C ++. By doing so, we hope that people is easy to understand the operation behind conditional statements. A
conditional operator is also known as a ternary operator. It takes three operands. A conditional operator is closely related with if else
statement. The method used is a literature study studying the bit manipulation algorithm in the C ++ language. The results obtained
are a function using bitwise operations in C ++ that implement conditional statements.

Keywords-bit-manipulation; conditional_statement; bitwise_operation; c++; ternary_operator.


IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448
Vol. 9, No. 1, 2020, Pp. 9-14
Meanwhile, the term byte is a grouping of binary digits (0 or
1 INTRODUCTION 1) which represent an information [7]. Laoudon said byte is a
All of information that manipulated by computer is string of bits, usually eight, used to store one number or character
represented by bit. Some information with the word yes and no, in a computer system [2]. One Byte is 8 bit units combined into
can be replaced with 1 and 0. Logic in a computer is represented one. Therefore, Byte is a unit of information that is larger than
by a two-state element, namely an off state (no current) and an bits. The first term 'byte' was coined and used by Dr. Werner
on state (there is a current) [1]. The bit is like a switch that can Buccholz in 1956, when he was working as a scientist at IBM
be switch On or Off. If it is turned off it will be like the computer [9].
reading the value 0 and if it is turned on it will read the valuo of The decimal number system is based on 10 numbers (0 to
1 [2]. Bit also can be described as true or false, live or dead, yes 9), the binary number system has only 2 numbers namely 0 and
or no, etc. 1. All data processed by a computer has the form of numbers 0
Modern computers store and process information and 1. In digital communication, these two values represent
represented as 2-valued signals. It can be easily represented, voltage level. One application, binary value 0 is used to express
stored, and sent on noisy and inaccurate cables. Electronic the absence of voltage, and binary value 1 for constant positive
circuits for storing and counting 2-valued signals are so simple voltage.
and reliable, so they can be integrated into billions of data on a The big letter ‘B’ represents bytes, while the small letter ‘b’
single silicon chip. The binary system only recognizes code 1 denotes bits. So uppercase letters mean larger bytes or units,
(high) and 0 (low) to describe a machine condition, so it is very while lowercase letters mean smaller bits or units. Usually
easy to process it according to its designation. transfer speed (bandwidth) is expressed in terms of bits, while
Most text characters use the ASCII standard which each data storage capacity is usually expressed in terms of bytes
character is represented by a unique byte-sized integer value. All [9]. In personal computers, data is stored and processed in 8-bit
of information inside the system such as disk files, programs, units called bytes. In ASCII code, each alphanumeric character
users data where saved in memory, and data were transferred is represented by a unique sequence of 7 bits. Although bits are
over the network also represented by many bits [3]. In a program used to measure digital transmission speed (bit rate), the capacity
with C language, the programmer creates a source program (or of storage (disk, files, databases, etc.) is measured in bytes
source file) with an editor and saves it in the form of a text file. [10][11][12].
The source program is a sequence of bits, each with a value of 0 Their basic types also called Intrinsic Types [13]. The first
or 1, organized in 8-bit chunks called bytes. Each byte represents five types are integers of non-descending length. For example,
some text character in the program [4]. the exact int length of each type depends on the implementation;
Programmer needs to understand the representation of for example int can be 16, 32 or 64 bits. All of these types can
numbers from machines, because they are not the same as be qualified as signed or not signed. The first has no effect on
integers and real numbers. The source code of program can be integer numbers (except characters) because the numbers are
read and understood by humans in the form of C ++ language. signed by default. When declaring an integer type as unsigned,
In order for the machine to understand it, the source program is there will not be a negative value but twice as many positive
translated into low-level machine language sequences [5]. values (plus one when we consider zero as positive or negative).
The char type can be used in two ways: for letters and numbers
The number system used by humans everyday is decimal, that are rather short. Char almost always has a length of 8 bits.
whereas computers tend to be easier to process binary number
systems [6]. Some literature about bits mentions that bits refer to Logic values are represented as BOOLs. Boolean variables
"binary digits". Mugivane said that a bit is a binary digit, the can store true and false. The length property that does not
smallest unit of information that can be stores either as 1 or 0 decrease applies in the same way as a floating-point number:
[7][8]. Bit is short for "Binary Digit", which means binary digit. float is shorter than or equal to double length, which in turn is
Binary digits are the smallest units in digital computing. A shorter than or equal in length to double length. Typical sizes are
computer does not use decimal numbers to store data. All 32 bits for float, 64 bits for double, and 80 bits for double length
computer data is stored in binary numbers. Only 2 different [13].
values can be expressed as one bit, either 0 or 1. In digital The bitwise operations can be applied for integer data type
telecommunications too, all voltage levels are converted to only. The C ++ programming language is very efficient to
binary data. The term "binary digit" or "bit" was introduced by manipulating bits. Work on byte, or data type like char, short,
John Tukey in 1947, when he was working as a scientist at Bell int, long, float, double, long double, pointer [14][11]. The
Laboratories [9]. operations with bits are used in data compression to reduce
According to Laudon, a binary digit representing the smallest space, encrypt the data for security problems. This operation is
unit of data in a computer system. It can only have one of two faster and closer to the system and optimizes the effectiveness of
states, representing 0 or 1 [2]. Binary numbers are two radix the program to a better level [15].
(base) numbers, the lowest step is 0 and the highest number is 1. Bit manipulation is the act of manipulating at the bit level (0
Every time you calculate by adding one unit, you will get: 0, 1, and 1). C++ programming language is one programming
10, 11, 100, 101, 110, 111, 1000 etc [1]. language that provides bit manipulation operators. According e-

This article is distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License. See for details: https://creativecommons.org/licenses/by-nc-nd/4.0/
10
IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448
Vol. 9, No. 1, 2020, Pp. 9-14
book Computer systems : a Programmer’s Perspective, one C++ also provides a set of logical operators ||, &&, and !,
useful feature of C++ is that it supports bit-wise Boolean which correspond to the Or, And, and Not operations of logic.
operations [4]. The symbols have used for the Boolean These can easily be confused with the bit-level operations, but
operations are exactly those used by C++: | for or, & for and, ~ their function is quite different. The logical operations treat any
for Not, and ^ for Exclusive-Or. These can be applied to any nonzero argument as representing True and argument 0 as
“integral” data type, that is, one declared as type char or int, with representing False. They return either 1 or 0, indicating a result
or without qualifiers such as short, long, long long, or unsigned of either True or False, respectively. C++ also provides a set of
[15]. shift operations for shifting bit patterns to the left and to the right
[4]. Operator << shows left shift and operator >> shows right
The & (bitwise AND) in C or C++ takes two numbers as shift [17].
operands and does AND on every bit of two numbers. The result
of AND is 1 only if both bits are 1. Table 1 shows this. The << (left shift) in C or C++ takes two numbers, left shifts
the bits of the first operand, the second operand decides the
number of places to shift. The >> (right shift) in C or C++ takes
Table 1 Bitwise AND two numbers, right shifts the bits of the first operand, the second
operand decides the number of places to shift. The right most
A B A&B bits are drop and new bits are inserted in from left. On the bases
0 0 0 of which bit to be inserted from the left, right shift is
0 1 0 divided into two type, logical right shift and arithmetic right shift
1 0 0 [18][17].
1 1 1
Table 5 illustrates a set of functions that manipulate and test
a set of bits. This function is a function obtained from the
The | (bitwise OR) in C or C++ takes two numbers as assignment of Computer Systems and Organization course. The
operands and does OR on every bit of two numbers. The result functions that included in Bit-Level Manipulation are bitXor,
of OR is 1 if any of the two bits is 1. Table 2 shows this. allOddBits, isAsciiDigit, conditional, logicalNeg. Each function
has a different description, rating and max operator. The
“description” provides an overview of the functions that must be
Table 2 Bitwise OR created. "Ranking" shows the level of difficulty (number of
points). "Max ops" gives the maximum number of operators that
A B A|B you can use to implement this function.The function is written
0 0 0 from the lowest rating of 1 to the highest rating of 4. Rating 1 is
0 1 1 given to the bitXor function, rating 2 on the allOddBits function,
1 0 1
rating 3 on the isAsciiDigit and conditional functions, and rating
1 1 1
4 on the logicalNeg function [19].

The ~ (bitwise NOT) in C or C++ takes one number and


inverts all bits of it . We can see this in Table 3. Table 5 Bit-Level Manipulation Functions

Name Description Rating Max Ops


Table 3 Bitwise NOT bitXor(x,y) x ˆ y using only & and ˜ 1 14

allOddBits(x) Are all odd-numbered bits 2 12


A ~A
set to 1?
0 1 isAsciiDigit(x) Is x an ASCII digit? 3 15
1 0
conditional(x, y, z) Same as C’s “x ? y : z” 3 16

logicalNeg(x) Compute !x without using ! 4 12


The ^ (bitwise XOR) in C or C++ takes two numbers as operator
operands and does XOR on every bit of two numbers. The result
of XOR is 1 if the two bits are different [16]. Table 4 shows this
operation. This paper focuses on the conditional function. In the
function there are three variables, namely x, y and z. The
description of this function is the same as C’s “x ? y : z”. The
Table 4 Bitwise XOR "Rating" for the conditional function is three. The "Max ops" for
the conditional function is sixteen. A C programming
A B A ^B conditional operator is also known as a ternary operator. It takes
0 0 0 three operands. The conditional operator is closely related with
0 1 1 if..else statement. Syntax of C programming conditional
1 0 1 operator is (condition) ? expression1 : expression2. If the
1 1 0

This article is distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License. See for details: https://creativecommons.org/licenses/by-nc-nd/4.0/
11
IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448
Vol. 9, No. 1, 2020, Pp. 9-14
condition is true then expression1 is executed else expression2 conditional function (x, y, z) where x = 2, y = 4 and z = 5 is 4.
is executed [20]. The operator for this function is ! , ~, &, ^, | , +, <<, >> . The
"Rating" ranks the difficulty level for this function is three. The
Our aim to conduct this research is to implement the "Max ops" gives the maximum number of operators that you can
conditional operation without using the conditional operator. use to implement this function is sixteen.
Instead, we should manipulate bits by applying several types of
bit operators to perform the conditional operation. Thus, at the In this program, the variables are local, not global. Local
end we will understand more bit manipulations. variables are variables that can only be used where the variable
is declared in a function scope. Global variables have the ability
In some cases, bit manipulation can avoid or reduce the need to be recognized by all existing program code whether in class
to repeat data structures and can provide a lot of folding speed, or in the main program (main program). It is because the
because bit manipulation is processed in parallel, but the code declaration of global variables is done outside the class and
can be more difficult to write and maintain. Hopefully, this outside the main program, but you should reduce the use of
article will make us more accustomed to and understand bit-level global variables because there is a possibility that this variable
representation and manipulation. will modified by instructions that use globally declared
variables. Each function has a different number of variables so
that they should be declared locally so that they don't interfere
2 METHOD when modified.
In this study, we use a research approach in the form of
literature study. Library studies relating to theoretical studies
and other references relating to values, culture and norms that 3.2 Result
develop in the social situation under study, besides library Based on the analysis, the formulas that were implemented
studies are very important in conducting research, this is because in C ++ and obtained in the form of functions can be seen in
research will not be separated from scientific literature [21]. Figure 1. The ternary operator x ? y : z will return y if x is true
Some references used are from several other researchers. The and z otherwise. In order to map an integer value to Boolean, 0
use of some of these references helps in deepening research. will be converted to false while any other values will be
mainly concerned with references about manipulation of bits, converted to true, then create an algorithm to map x to a new
bytes and bits. domain. Figure 1 depicts this function.

3 RESULT AND DISCUSSION


The implementation of bitwise operators has been described
in the journal Application of Bitwise Operators in C [18]. In that
journal, bitwise applications are implemented as a modulus
operator and as Boolean flags and bit masks. This research will
explain the application of bitwise operators on the conditional
function. This operation is hard to understand for some people
so this this research explains it in the understandable form.

3.1 Analysis Figure 1. Conditional Function.


The program code is in the bits.c file provided by the
lecturer in Computer Systems and Organization. In making this
function, there are some rules that must be fulfilled, such as
Regarding operators, the program has fulfilled the
expressions in functions that can only use integers 0 to 255 (not
requirements because it uses a legal operator (! << >> ~ & |)
allowed to use large constants like 0xffffffff), function
and does not exceed the maximum amount (7 times). Figure 1
arguments and local variables (no global variables), unary
explains that there are three variables with integer types namely
integer operations (! ~), binary integer operations (& ^ | + <<
x, y and z. It works by turning variable x into a mask: change all
>>). Programs are strictly prohibited from using control
of its bit into 1 if x is false, otherwise change all the bits into 0
constructs (if, do, while, for, switch, etc.), defining or using
if x is true. The next operation is either negate x and apply AND
macros, defining additional functions into files, calling other
operation to negated x and y or apply AND operator to x and z.
functions, using other operations (&&, ||, -, or ?: ), using all
At the end of this function, one of those operations’ result will
forms of casting, using data types other than Integer (arrays,
be returned.
structs, or unions).
In bits.c functions are used in the programming structure not
More details about the behavior of conditional functions are
in the procedure. In the procedure there are void keywords and
explained in comments on bits.c. The bits.c file gives an example
in the body there is no return value. Whereas in function there
of using a condition function, for example the result of a
is a data type followed by the name of the function and in the

This article is distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License. See for details: https://creativecommons.org/licenses/by-nc-nd/4.0/
12
IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448
Vol. 9, No. 1, 2020, Pp. 9-14
body part there is a return value. Function writing begins with
a data type that is int, which means integer followed by the
name of the function that is conditional and parentheses, which
contain a list of parameters. There are three parameters in the
function and all three use the int data type. This function will
get the values x, y and z from the input in the main program. In
the body section, there is a return value that will later be
displayed in the main program.
Figure 2 explains the program code for conditional function
calls. In C ++ programming, the program code cout or
character-out to display output to the screen. After << symbol
the programmer can write the text that will be displayed to the
screen, followed by quotation marks, text, quotation marks and
at the end of the command there is a semicolon (;) A sign \n is
a symbol to create a new line. An int variable is a variable
declaration with an integer data type. Figure 3. Result of programs.

4 CONCLUSION
To represent bits in C ++, the first must understand bitwise
operations, as a basis for constructing conditional statement
structures and algorithms if x is false then 1, if x is correct then
0. Based on the discussion in the previous session it produces an
equation if x is not wrong, don't omit the bit y, if x is not correct,
don't lose the z bit. From the concept or formula produced a
function that can represent conditional statements in C ++.
In this article, we hope that there will be articles that discuss
the more fundamental methods with more mature optimization
Figure 2. Main Script. methods, and also develop articles that discuss comparative
studies of several languages used by compiler / interpreter types.
It is hoped that readers can see a very useful thing in the
development of the system so that the non-functionality of the
In this program, there are three variables, namely xa, xb and application built can be achieved indirectly.
xc. cin command or character-input is a function to take input
from the keyboard. >> symbol is followed by a storage location
variable, and a screen will appear for inputting data through the
keyboard. In the program the value x will be represented by xa ACKNOWLEDGMENT
variable, the value of y is represented by xb variable and the This paper is one of the assignment from Computer Systems
value of z is represented by xc variable. After inputting of all and Organization course, the Department of Informatics
these numbers, the program will send data to the conditional Graduate Program, Faculty of Science and Technology UIN
function with xa, xb and xc parameters. Sunan Kalijaga Yogyakarta.

3.3 Discussion
REFERENCES
In Figure 3 you can see the results of the conditional
[1] F. Gulo, “Aplikasi Pembelajaran Hidrografi Menggunakan Metode
function program. After running the program will bring up the Computer Assisted Instruction (CAI),” J. Ris. Komput., vol. Vol. 3 No.,
title, then followed the writing enter the value x : . The user will no. Desember, pp. 34–37, 2018.
input the value, for example 2. Continued with the display Enter [2] Laudon, Management Information Systems Thirteenth Edition Global
the value of y, then the user enters the value of y that is 4. input Edition. 2014.
will appear last Enter the value of z and the user enters the value [3] S. Janakiraman, K. Thenmozhi, J. B. B. Rayappan, and R. Amirtharajan,
5. After completing inputting, the screen will appear writing “Lightweight chaotic image encryption algorithm for real-time embedded
The result is: followed the conditional result is 4 based on the system: Implementation and analysis on 32-bit microcontroller,”
Microprocess. Microsyst., vol. 56, pp. 1–12, 2018.
input provided.
[4] R. E. Bryant and D. R. O’Hallaron, Computer Systems : a Programmer’s
Perspective -Second edition. 2011.

This article is distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License. See for details: https://creativecommons.org/licenses/by-nc-nd/4.0/
13
IJID (International Journal on Informatics for Development), e-ISSN: 2549-7448
Vol. 9, No. 1, 2020, Pp. 9-14
[5] A. Sanchez-Stern, P. Panchekha, S. Lerner, and Z. Tatlock, “Finding root [14] U. Sidi, M. Ben Abdellah, M. Fez, D. Chenouni, M. Berrada, and A.
causes of floating point error,” ACM SIGPLAN Not., vol. 53, no. 4, pp. Tahiri, “Paper—A Serious Game for Learning C Programming Language
256–269, 2018. Concepts Using Solo Taxonomy A Serious Game for Learning C
[6] K. Ismail and N. A. Rachman, “Dekoder Empat Bit Binary Code Decimal Programming Language Concepts Using Solo Taxonomy Alaeeddine
( Bcd ) Untuk Dua Digit Seven Segment,” Semin. Nas. Telekomun. dan Yassine,” iJET, pp. 110–127, 2017.
Inform., vol. Bandung, 2, no. Selisik, pp. 325–330, 2018. [15] P. Lindstrom, S. Lloyd, and J. Hittinger, “Universal coding of the reals:
[7] F. I. Mugivane, Introduction To Computer. 2014. Alternatives to IEEE floating point,” ACM Int. Conf. Proceeding Ser., no.
March, 2018.
[8] M. Sari and A. Prasetyo, “Program Konversi Sistem Bilangan Desimal ke
Biner dan Oktal Menggunakan Pemrograman C++ Berbasis Codeblock,” [16] K. Yordzhev, “The Bitwise Operations Related to a Fast Sorting
Dec. 2018. Algorithm,” Int. J. Adv. Comput. Sci. Appl., vol. 4, no. 9, 2013.
[9] “Pengertian & Perbedaan antara bit (b) dengan Byte (B) – JalaWave [17] I. Corporation, “[3]Intel ® 64 and IA-32 Architectures Software
Connection,” Nov-2016. [Online]. Available: Developer ’ s Manual Documentation Changes,” System, vol. 3, no.
https://www.jalawave.net.id/?p=413. [Accessed: 10-Jun-2020]. 253665, 2011.
[10] E. Isbn, “Table of Contents Getting Help in an Integrated Development [18] R. Chandra, S. Rawat, and T. Jain, “Application of Bitwise Operators in
Environment,” no. August 1997, 2003. C,” Int. J. Sci. Eng. Res., vol. 4, no. 11, pp. 1–4, 2013.
[11] R. E. Bryant and D. R. O. Hallaron, Computer Systems. A Programmer’s [19] A. Burud and P. Bhaskar, “Design and Implementation of FPGA Based
Perspective [3rd ed.]. Boston: Pearson, 2016. 32 Bit Floating Point Processor for DSP Application,” Proc. - 2018 4th
Int. Conf. Comput. Commun. Control Autom. ICCUBEA 2018, no. ref 15,
[12] C. Sinthanayothin and W. Bholsithi, “Dental application: The steps
pp. 1–5, 2018.
toward the implementation of the cephsmile plus services,” Int. J. Adv.
Comput. Technol., vol. 3, no. 3, pp. 210–221, 2011. [20] “C Programming Conditional Operator (?:) - Trytoprogram.” [Online].
Available: http://www.trytoprogram.com/c-programming/c-conditional-
[13] Peter Gottschling, Discovering Modern C++. An Intensive Course for
operator/ . [Accessed: 10-Jun-2020].
Scientists, Engineers,and Programmers. Pearson Education, 2016.
[21] U. Suharsaputra, Metode Penelitian Kuantitatif, Kualitatif, dan Tindakan.
Bandung: Alfabeta, 2012.

This article is distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International
License. See for details: https://creativecommons.org/licenses/by-nc-nd/4.0/
14

You might also like