Bit_Manipulation_Conditional_Statement_u
Bit_Manipulation_Conditional_Statement_u
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.
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].
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.
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