Download presentation
Presentation is loading. Please wait.
1
Lesson 8 Binary File,Bit Operations
Hu Junfeng 2015/10/16
2
关于Trie(字典树) 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。 可以实现串频统计、求最长公共子串、字典索引(前缀匹配) 空间占用较大 —— 双数组Tire
3
周仕林同学实现的TRie 优先队列的排序方式用的则是它们若编码之后能够得到的压缩量做关键字排序。然后达到32768个元素之后每新插入一个元素就删掉原优先队列里面一个关键词最小的元素。这样子就能够得到前32768个最适合压缩的单词。 解压缩文件是jsa.txt.作业里面还有一个same.out是用来检测两个文件是否相同的程序,也就是判断是否是无损压缩。
5
优先队列 完全二叉树的顺序存储 按子树的大根或小根维护算法 初始堆的创建算法
6
The Data Hierarchy Bit - smallest data item Value of 0 or 1
Byte – 8 bits Used to store a character Decimal digits, letters, and special symbols Field - group of characters conveying meaning Example: your name Record – group of related fields Represented a struct or a class Example: In a payroll system, a record for a particular employee that contained his/her identification number, name, address, etc. File – group of related records Example: payroll file Database – group of related files
7
Binary Files Formatted Text files contain variable length records
must be accessed sequentially, processing all records from the start of file to access a particular record Binary Files (random access file) a file containing binary numbers that are the computer’s internal representation of each file component contain fixed length records can be accessed directly, directly accessing the record that is required sizeof operator that finds the number of bytes used for storage of a data type
8
In a Random Access File …
Data Data unformatted (stored as "raw bytes") in random access files All data of the same type (ints, for example) use the same memory All records of the same type have a fixed length Data not human readable
9
Random Access Access individual records without searching through other records Instant access to records in a file Data can be inserted without destroying other data Data previously stored can be updated or deleted without overwriting. Implemented using fixed length records Sequential files do not have fixed length records
10
Random Access a File -- fread ()
fread --Transfer bytes from a file to a location in memory Function fread requires four arguments ret = fread(buffer, size, num, myptr); the number of objects read buffer: Address of first memory cell to fill size: Size of one value num: Maximum number of elements to copy from the file into memory myptr: File pointer to a binary file opened in mode “rb” using function fopen
11
Binary File operation — fwrite()
fwrite - Transfer bytes from a location in memory to a file fwrite( &number, sizeof( int ), 1, myPtr ); &number - Location to transfer bytes from sizeof( int ) - Number of bytes to transfer 1 - For arrays, number of elements to transfer In this case, "one element" of an array is being transferred myPtr – file pointer
12
Binary File operation — fwrite() (cont.)
Writing structs fwrite( &myObject, sizeof (struct myStruct), 1, myPtr ); sizeof - Returns size in bytes of object in parentheses To write several array elements Pointer to array as first argument Number of elements to write as third argument
13
Creating a Binary File of Integers
14
Access Data in a Random Access File
fseek Sets file position pointer to a specific position fseek( myPtr, offset, symbolic_constant); myPtr - pointer to file offset - file position pointer (0 is first location) symbolic_constant - specifies where in file we are reading from SEEK_SET - seek starts at beginning of file SEEK_CUR - seek starts at current location in file SEEK_END - seek starts at end of file ftell Return the current position in a stream ftell( myptr)
15
Read file
17
Bit Operation Topics: Bit operators Using bitwise operations
Bit fields
18
Overview of Bit Operations
useful for systems programming where a single bit or group of bits has meaning bitwise operators can be used for manipulating bits in integral operands (char, short, int, long) bit fields can be defined in a struct declaration and accessed as struct members
19
Bitwise Operators & (bitwise AND) | (bitwise OR) ^ (bitwise XOR)
useful for masking bits and turning off bits don’t’ confuse with && (logical AND) | (bitwise OR) useful for turning on bits don’t confuse with || (logical OR) ^ (bitwise XOR) useful for checking if bits have different values ~ (one’s complement) useful for reversing the sense of bits (unary)
20
Bitwise Shift Operators
<< (left shift) useful for moving a bit or bit group to the left 0 filled in low order bits >> (right shift) useful for moving a bit or bit group to the right unsigned integer is 0 filled in high order bits signed integer treatment is machine dependent
21
Bitwise AND i j i & j 1
22
Bitwise OR i j i | j 1
23
Bitwise XOR i j i ^ j 1
24
Bitwise One’s Complement
1
25
Bitwise AND Illustration
1 i 1 j k = i & j isolate bit group 1 k
26
Bitwise OR Illustration
1 1 j k = i | j set this bit 1 k
27
Bitwise Left Shift Illustration
1 i k = i << 2 0 filled 1 k
28
Bitwise Right Shift Illustration
1 k = i >> 2 0 filled (if unsigned) 1 k
29
Using bitwise operations —— masking and setting
30
Complement notation
31
Exclusive OR
32
Odd or Even
33
Bits rotation
34
Structure Bit Fields general format: type is int (signed or unsigned)
type fieldname : n type is int (signed or unsigned) n is the number of bits in the field (must be an integer constant) can’t take the address ( & ) of a bit field can’t have an array of bit fields
35
Bit Field Declaration 5 bit fields set the bit struct flags {
unsigned int f1 : 1 ; unsigned int f2 : 1 ; unsigned int f3 : 1 ; unsigned int type : 4 ; unsigned int index : 9 ; }; main() { struct flags Flags; Flags.f3 = 1; } 5 bit fields set the bit
36
Packed data Count the number of one’s in the field of homework
37
作业,两道18号前提交(位运算、广搜) http://bailian.openjudge.cn/practice/1753
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.