Copyright © 1997-2009 Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.

Slides:



Advertisements
Similar presentations
Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
Advertisements

DATA REPRESENTATION CONVERSION.
Representations Example: Numbers –145 –CVL – –91 –
Level ISA3: Information Representation

Introduction to Programming with Java, for Beginners
Assembly Language and Computer Architecture Using C++ and Java
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter Chapter Goals Know the different types of numbers Describe positional notation.
Data Representation in Computers
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
2.1 2 Number Systems Foundations of Computer Science  Cengage Learning.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
CENG 311 Machine Representation/Numbers
Numbering systems.
Computers Organization & Assembly Language
Decimal Number System We are used to the decimal number system which is a positional number system The decimal number 4386 represents the value: 41000.
IT253: Computer Organization
Number Systems Spring Semester 2013Programming and Data Structure1.
Data Representation in Computer Systems
Logic Design Dr. Yosry A. Azzam. Binary systems Chapter 1.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Copyright 2005 Curt Hill Binary, Octal and Hexadecimal The number bases used in computers.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
CSC 221 Computer Organization and Assembly Language
CPS120: Introduction to Computer Science Computer Math: Converting to Decimal.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Set A formal collection of objects, which can be anything May be finite or infinite Can be defined by: – Listing the elements – Drawing a picture – Writing.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Chapter 2 Number Systems: Decimal, Binary, and Hex.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
Copyright © Curt Hill Numerics Representation and Errors.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
June 10, 2002© Howard Huang1 Number systems To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Copyright © – Curt Hill Types What they do.
Number Systems by Dr. Amin Danial Asham. References  Programmable Controllers- Theory and Implementation, 2nd Edition, L.A. Bryan and E.A. Bryan.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
Binary Values. Numbers Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645, 32 Negative Numbers.
Lec 3: Data Representation
Octal to Decimal Decimal Octal Binary Hexadecimal.
Chap. 2. Types, Operators, and Expressions
Number Systems and Bitwise Operation
Fundamentals & Ethics of Information Systems IS 201
Chapter 2 Bits, Data Types & Operations Integer Representation
Data Representation Data Types Complements Fixed Point Representation
Chapter 2 Number Systems.
Chapter 2 Number Systems.
Chapter 2 Number Systems.
Bitwise operators.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Chapter 2 Number Systems.
Section 6 Primitive Data Types
Presentation transcript:

Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus

Copyright © Curt Hill Aside Before discussing bitwise operators a more fundamental item needs to be introduced or reviewed Binary is the base 2 number system It is how things are represented in computers If an item cannot be represented in base two it cannot be stored on a computer

Copyright © Curt Hill Binary Base two number system Each digit is 0 or 1 and multiplied by a power of two Hence a byte with just one bit turned on is a power of two All numbers on a computer are represented in binary because of the hardware

Copyright © Curt Hill 8 bit example: x 2 0 = 1 1 x 2 1 = 2 1 x 2 2 = 4 1 x 2 3 = 8 1 x 2 4 = 16 1 x 2 5 = 32 1 x 2 6 = 64 1 x 2 7 = 128

Copyright © Curt Hill Other bases Binary has some problems, the largest of which is that it is very bulky Decimal from binary is not an easy conversion –It also obscures structure Most manufacturers use another power of two base to show things –Octal – base 8 = 2 3 –Hexadecimal – base 16 = 2 4

Copyright © Curt Hill Octal Base 8 has 8 digits, 0 – 7 and every place is raised to a power of 8 Conversion to and from binary is way too easy –Partition into groups of 3 bits from right to left –Convert the three bits to octal Preferred by certain manufacturers such as DEC and Intel

Copyright © Curt Hill Binary – Octal Conversion Start with binary Partition (right to left) in groups of Write digits How easy is that? Conversion of octal to binary is the reverse

Copyright © Curt Hill Hexadecimal Base 16 has 16 digits, 0 – 9, A-F and every place is raised to a power of 16 –A=10, B=11, C=12, D=13, E=14, F=15 Conversion to and from binary is also easy –Partition into groups of 4 bits –Convert the four bits to hexadecimal Preferred by certain manufacturers such as IBM and Burroughs

Copyright © Curt Hill Binary – Hexadecimal Conversion Start with binary Partition (right to left) in groups of Write digits 6 E 5 D How easy is that? Conversion of hexadecimal to binary is the reverse

Copyright © Curt Hill C Notation Since the C family of languages is occasionally well designed they all provide a notation for octal and hexadecimal constants An octal constant must start with a zero and have only octal digits: –031, A hexadecimal constant starts with a zero and then an x: –0x012cf8

Copyright © Curt Hill Purpose Bool values may contain one of two values: –true or false Boolean variables take one byte each Each byte consists of 8 bits, each with a possible value of 0 or 1 Bitwise operators allow us to use these bits like small arrays of bools

Copyright © Curt Hill Packing An integer is normally 4 bytes or 32 bits An array of 4 bools occupies 4 bytes In same space 4 booleans or 32 bits? Allocation is always by bytes so we cannot allocate 25 bits

Copyright © Curt Hill Boolean operations AND OR NOT XOR

Copyright © Curt Hill What is needed? Declare these clusters of booleans Initialize Apply the operators Obtain the results

Copyright © Curt Hill Declaration Any simple type may be used The preference is for integral types char gives 8 bits short gives 16 int gives 32 double gives 64

Copyright © Curt Hill Operators Bitwise operators are the same as the bool operators except one symbol instead of two –&& is boolean AND –& is bit AND –| is OR –^ is XOR

Copyright © Curt Hill Example int a, b, c; c = a | b This does 32 individual Ors in one operation –Usually a very fast operation ^ gives

Copyright © Curt Hill What about NOT? To accomplish NOT use XOR with all ones for one operand ^ gives Negation example int c; … c = c ^ 0xFFFFFFFF

Copyright © Curt Hill Accessing results Integers cannot be used as bools There is no subscripting operation for bits either How are they accessed? Using bit operations and constant integers

Copyright © Curt Hill Setting Values To set values –Add the power of two constants –c = ; To check the presence of a bit –AND it with constant and check for zero –c & 8 != 0 true 8 was present false 8 was not present

Copyright © Curt Hill Shifting There are also shift operations A left shift of one bit corresponds to multiply by 2 A right shift of one bit corresponds to divide by 2 Form is int << int –Left int is one to be shifted –Right is number of bits to shift

Copyright © Curt Hill Example Shift c = a << 2; –Same as multiply by 4 if a and c are not signed b = c >> a; –Shift the value from c to the right by a bits and put result in b

Copyright © Curt Hill In Theory and In Practice The usual use of bits are for named constants An Event object contains an id field, which has these flags among others –MOUSE_ENTER, MOUSE_DRAG, MOUSE_EXIT, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE –ACTION_EVENT –LIST_SELECT, LIST_DESELECT

Copyright © Curt Hill Sets Boolean algebra and set theory are orthogonal –Any set operation or proof may be converted into a boolean operation or proof or vice versa Thus an array of booleans may act like a set of small integers

Copyright © Curt Hill Set Operations The boolean AND is equivalent to the set Intersection –A & B could be either intersection or parallel And The boolean OR is the set Union The membership function of a set is merely determining if that integer is present