CMPUT201W20B2 Week 11
Table of Contents
1 Week11
1.1 Copyright Statement
If you are in CMPUT201 at UAlberta this code is released in the public domain to you.
Otherwise it is (c) 2020 Abram Hindle, Hazel Campbell AGPL3.0+
1.1.1 License
CMPUT 201 C Notes Copyright (C) 2020 Abram Hindle, Hazel Campbell
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
1.1.2 Hazel Code is licensed under AGPL3.0+
Hazel's code is also found here https://github.com/hazelybell/examples/tree/C-2020-01
Hazel code is licensed: The example code is licensed under the AGPL3+ license, unless otherwise noted.
1.2 Alternative version
Checkout the .txt, the .pdf, and the .html version
1.3 Init ORG-MODE
;; I need this for org-mode to work well ;; If we have a new org-mode use ob-shell ;; otherwise use ob-sh --- but not both! (if (require 'ob-shell nil 'noerror) (progn (org-babel-do-load-languages 'org-babel-load-languages '((shell . t)))) (progn (require 'ob-sh) (org-babel-do-load-languages 'org-babel-load-languages '((sh . t))))) (org-babel-do-load-languages 'org-babel-load-languages '((C . t))) (org-babel-do-load-languages 'org-babel-load-languages '((python . t))) (setq org-src-fontify-natively t) (setq org-confirm-babel-evaluate nil) ;; danger! (custom-set-faces '(org-block ((t (:inherit shadow :foreground "black")))) '(org-code ((t (:inherit shadow :foreground "black")))))
1.3.1 Org export
(org-html-export-to-html) (org-latex-export-to-pdf) (org-ascii-export-to-ascii)
1.3.2 Org Template
Copy and paste this to demo C
#include <stdio.h> int main(int argc, char**argv) { return 0; }
1.4 Remember how to compile?
gcc -std=c99 -pedantic -Wall -Wextra -ftrapv -ggdb3 -o programname programname.c
1.5 Numbers!
Computers love powers of 2 because we calculate everything via bits.
Bases used on computers:
2, 8, 10, 16
- 32 in base 16 is 2*pow(16,1) = 0x20
- 32 in base 10 is 3*pow(10,1) + 2*pow(10,0) = 3*10 + 2*1 = 32
- 32 in base 8 is 4*pow(8,1) = 4*8 = 32 = 040
- 32 in base 2 is 1*pow(2,5) = 0b100000
- 31 in base 16 is 1*pow(16,1) + 15*pow(16,0)
- 31 in base 10 is 3*pow(10,1) + 1*pow(10,0)
- 31 in base 8 is 3*pow(8,1) + 7*pow(8,0)
- 31 in base 2 is 1*pow(2,4) + 1*pow(2,3) + 1*pow(2,2) + 1*pow(2,1) + 1*pow(2,0)
- Notation for base 2 for 31: 0b11111 # not available in C, good in python
- Notation for base 8 for 31: 037 # available in C , good in python
- Notation for base 10 for 31: 31 # available in C , good in python
- Notation for base 16 for 31: 0x1F # available in C , good in python
1.5.1 Binary
Base 2: powers of 2 Digits: 0,1 0: 0b00000 8: 0b01000 1: 0b00001 9: 0b01001 2: 0b00010 10: 0b01010 3: 0b00011 11: 0b01011 4: 0b00100 12: 0b01100 5: 0b00101 13: 0b01101 6: 0b00110 14: 0b01110 7: 0b00111 15: 0b01111 16: 0b10000
1.5.2 Octal
#include <stdio.h> int main() { printf("%d\n", 037); }
31
Base 8: powers of 8 3 bits Digits: 0,1,2,3,4,5,6,7 0: 000 8: 010 1: 001 9: 011 2: 002 10: 012 3: 003 11: 013 4: 004 12: 014 5: 005 13: 015 6: 006 14: 016 7: 007 15: 017 16: 020 07: 7 = 7 077: 7*8 + 7 = 63 0777: 7*8*8 + 7*8 + 7 = 511 07777: 7*8*8*8 + 7*8*8 + 7*8 + 7 = 4095
1.5.3 Base10 Review
Base 10: power of 10 Digits: 0,1,2,3,4,5,6,7,8,9 ~4 bits - not a power of 2 0: 0 8: 8 1: 1 9: 9 2: 2 10: 10 3: 3 11: 11 4: 4 12: 12 5: 5 13: 13 6: 6 14: 14 7: 7 15: 15 16: 16
1.5.4 Hex Review
Base 16: power of 16 Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F or Digits: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f 0: 0x00 8: 0x08 1: 0x01 9: 0x09 2: 0x02 10: 0x0A 3: 0x03 11: 0x0B 4: 0x04 12: 0x0C 5: 0x05 13: 0x0D 6: 0x06 14: 0x0E 7: 0x07 15: 0x0F 16: 0x10 0xF = 15 = 15 = 2^4 - 1 0xFF = 15*16 + 15 = 255 = 2^8 - 1 0xFFF = 15*16*16 + 15*16 + 15 = 4095 = 2^12 - 1 0xFFFF = 15*16*16*16 + 15*16*16 + 15*16 + 15 = 65535 = 2^16 - 1 Digit Lookup 0: 0x0 0b0000 8: 0x8 0b1000 1: 0x1 0b0001 9: 0x9 0b1001 2: 0x2 0b0010 10: 0xA 0b1010 3: 0x3 0b0011 11: 0xB 0b1011 4: 0x4 0b0100 12: 0xC 0b1100 5: 0x5 0b0101 13: 0xD 0b1101 6: 0x6 0b0110 14: 0xE 0b1110 7: 0x7 0b0111 15: 0xF 0b1111 1 nibble = 4 bits = 1 hex digit
1.6 Bitwise Operators
Bitwise operators operate at the bit-level for variables such as integers.
Typically try to avoid anything signed or floating point when doing bitwise operations unless you are sure.
Also be aware that structs have padding so you might get unexpected results.
1.6.1 Left Shift
0b000001 << 1 = 0b000010 0b000001 << 2 = 0b000100 0b000001 << 3 = 0b001000 0b101011 << 3 = 0b011000
Integer Multiplication by 2. Shifts bits to the left. Do not use negative values.
WARNING: Shifting on signed integers is not safe or portable.
#include <stdio.h> #include <inttypes.h> int main() { uint64_t u64 = 1; uint32_t u32 = 1; uint16_t u16 = 1; int64_t i64 = 1; int32_t i32 = 1; int16_t i16 = 1; for (int i = 0 ; i < 64; i++) { u64 = u64 << 1; i64 = i64 << 1; u32 = u32 << 1; i32 = i32 << 1; u16 = u16 << 1; i16 = i16 << 1; printf("64: %20llu %20lld\n", u64, i64); printf("32: %20lu %20ld\n", u32, i32); printf("16: %20hu %20hd\n", u16, i16); } }
64: 2 2 32: 2 2 16: 2 2 64: 4 4 32: 4 4 16: 4 4 64: 8 8 32: 8 8 16: 8 8 64: 16 16 32: 16 16 16: 16 16 64: 32 32 32: 32 32 16: 32 32 64: 64 64 32: 64 64 16: 64 64 64: 128 128 32: 128 128 16: 128 128 64: 256 256 32: 256 256 16: 256 256 64: 512 512 32: 512 512 16: 512 512 64: 1024 1024 32: 1024 1024 16: 1024 1024 64: 2048 2048 32: 2048 2048 16: 2048 2048 64: 4096 4096 32: 4096 4096 16: 4096 4096 64: 8192 8192 32: 8192 8192 16: 8192 8192 64: 16384 16384 32: 16384 16384 16: 16384 16384 64: 32768 32768 32: 32768 32768 16: 32768 -32768 64: 65536 65536 32: 65536 65536 16: 0 0 64:<PHONE_NUMBER>72 32:<PHONE_NUMBER>72 16: 0 0 64:<PHONE_NUMBER>44 32:<PHONE_NUMBER>44 16: 0 0 64:<PHONE_NUMBER>88 32:<PHONE_NUMBER>88 16: 0 0 64: 1048576 1048576 32: 1048576 1048576 16: 0 0 64: 2097152 2097152 32: 2097152 2097152 16: 0 0 64: 4194304 4194304 32: 4194304 4194304 16: 0 0 64: 8388608 8388608 32: 8388608 8388608 16: 0 0 64: 16777216 16777216 32: 16777216 16777216 16: 0 0 64: 33554432 33554432 32: 33554432 33554432 16: 0 0 64: 67108864 67108864 32: 67108864 67108864 16: 0 0 64: 134217728 134217728 32: 134217728 134217728 16: 0 0 64: 268435456 268435456 32: 268435456 268435456 16: 0 0 64: 536870912 536870912 32: 536870912 536870912 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32:<PHONE_NUMBER><PHONE_NUMBER> 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32:<PHONE_NUMBER><PHONE_NUMBER> 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 0 0 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 0 0 16: 0 0 64:<PHONE_NUMBER>4<PHONE_NUMBER>4 32: 0 0 16: 0 0 64:<PHONE_NUMBER>8<PHONE_NUMBER>8 32: 0 0 16: 0 0 64:<PHONE_NUMBER>6<PHONE_NUMBER>6 32: 0 0 16: 0 0 64:<PHONE_NUMBER>72<PHONE_NUMBER>72 32: 0 0 16: 0 0 64:<PHONE_NUMBER>44<PHONE_NUMBER>44 32: 0 0 16: 0 0 64:<PHONE_NUMBER>88<PHONE_NUMBER>88 32: 0 0 16: 0 0 64:<PHONE_NUMBER>776<PHONE_NUMBER>776 32: 0 0 16: 0 0 64:<PHONE_NUMBER>552<PHONE_NUMBER>552 32: 0 0 16: 0 0 64:<PHONE_NUMBER>104<PHONE_NUMBER>104 32: 0 0 16: 0 0 64:<PHONE_NUMBER>208<PHONE_NUMBER>208 32: 0 0 16: 0 0 64:<PHONE_NUMBER>4416<PHONE_NUMBER>4416 32: 0 0 16: 0 0 64:<PHONE_NUMBER>8832<PHONE_NUMBER>8832 32: 0 0 16: 0 0 64:<PHONE_NUMBER>7664<PHONE_NUMBER>7664 32: 0 0 16: 0 0 64:<PHONE_NUMBER>55328<PHONE_NUMBER>55328 32: 0 0 16: 0 0 64:<PHONE_NUMBER>10656<PHONE_NUMBER>10656 32: 0 0 16: 0 0 64:<PHONE_NUMBER>21312<PHONE_NUMBER>21312 32: 0 0 16: 0 0 64:<PHONE_NUMBER>842624<PHONE_NUMBER>842624 32: 0 0 16: 0 0 64:<PHONE_NUMBER>685248<PHONE_NUMBER>685248 32: 0 0 16: 0 0 64:<PHONE_NUMBER>370496<PHONE_NUMBER>370496 32: 0 0 16: 0 0 64:<PHONE_NUMBER>740992<PHONE_NUMBER>740992 32: 0 0 16: 0 0 64:<PHONE_NUMBER>9481984<PHONE_NUMBER>9481984 32: 0 0 16: 0 0 64:<PHONE_NUMBER>8963968<PHONE_NUMBER>8963968 32: 0 0 16: 0 0 64:<PHONE_NUMBER>7927936<PHONE_NUMBER>7927936 32: 0 0 16: 0 0 64:<PHONE_NUMBER>75855872<PHONE_NUMBER>75855872 32: 0 0 16: 0 0 64:<PHONE_NUMBER>51711744<PHONE_NUMBER>51711744 32: 0 0 16: 0 0 64:<PHONE_NUMBER>03423488<PHONE_NUMBER>03423488 32: 0 0 16: 0 0 64:<PHONE_NUMBER>606846976<PHONE_NUMBER>606846976 32: 0 0 16: 0 0 64:<PHONE_NUMBER>213693952<PHONE_NUMBER>213693952 32: 0 0 16: 0 0 64:<PHONE_NUMBER>427387904<PHONE_NUMBER>427387904 32: 0 0 16: 0 0 64:<PHONE_NUMBER>854775808 -9223372036854775808 32: 0 0 16: 0 0 64: 0 0 32: 0 0 16: 0 0
1.6.2 Right Shift
Integer Division by 2. Shifts bits to the right. Do not use negative values.
WARNING: Shifting on signed integers is not safe or portable.
#include <stdio.h> #include <inttypes.h> int main() { // 0xF = 0b1111 // 0x7 = 0b0111 uint64_t u64 = 0x7FFFFFFFFFFFFFFF; uint32_t u32 = 0x7FFFFFFF; uint16_t u16 = 0x7FFF; int64_t i64 = 0x7FFFFFFFFFFFFFFF; int32_t i32 = 0x7FFFFFFF; uint16_t i16 = 0x7FFF; for (int i = 0 ; i < 64; i++) { u64 = u64 >> 1; i64 = i64 >> 1; u32 = u32 >> 1; i32 = i32 >> 1; u16 = u16 >> 1; i16 = i16 >> 1; printf("64: %20llu %20lld\n", u64, i64); printf("32: %20lu %20ld\n", u32, i32); printf("16: %20zu %20zd\n", u16, i16); } }
64:<PHONE_NUMBER>427387903<PHONE_NUMBER>427387903 32:<PHONE_NUMBER><PHONE_NUMBER> 16: 16383 16383 64:<PHONE_NUMBER>213693951<PHONE_NUMBER>213693951 32: 536870911 536870911 16: 8191 8191 64:<PHONE_NUMBER>606846975<PHONE_NUMBER>606846975 32: 268435455 268435455 16: 4095 4095 64:<PHONE_NUMBER>03423487<PHONE_NUMBER>03423487 32: 134217727 134217727 16: 2047 2047 64:<PHONE_NUMBER>51711743<PHONE_NUMBER>51711743 32: 67108863 67108863 16: 1023 1023 64:<PHONE_NUMBER>75855871<PHONE_NUMBER>75855871 32: 33554431 33554431 16: 511 511 64:<PHONE_NUMBER>7927935<PHONE_NUMBER>7927935 32: 16777215 16777215 16: 255 255 64:<PHONE_NUMBER>8963967<PHONE_NUMBER>8963967 32: 8388607 8388607 16: 127 127 64:<PHONE_NUMBER>9481983<PHONE_NUMBER>9481983 32: 4194303 4194303 16: 63 63 64:<PHONE_NUMBER>740991<PHONE_NUMBER>740991 32: 2097151 2097151 16: 31 31 64:<PHONE_NUMBER>370495<PHONE_NUMBER>370495 32: 1048575 1048575 16: 15 15 64:<PHONE_NUMBER>685247<PHONE_NUMBER>685247 32:<PHONE_NUMBER>87 16: 7 7 64:<PHONE_NUMBER>842623<PHONE_NUMBER>842623 32:<PHONE_NUMBER>43 16: 3 3 64:<PHONE_NUMBER>21311<PHONE_NUMBER>21311 32:<PHONE_NUMBER>71 16: 1 1 64:<PHONE_NUMBER>10655<PHONE_NUMBER>10655 32: 65535 65535 16: 0 0 64:<PHONE_NUMBER>55327<PHONE_NUMBER>55327 32: 32767 32767 16: 0 0 64:<PHONE_NUMBER>7663<PHONE_NUMBER>7663 32: 16383 16383 16: 0 0 64:<PHONE_NUMBER>8831<PHONE_NUMBER>8831 32: 8191 8191 16: 0 0 64:<PHONE_NUMBER>4415<PHONE_NUMBER>4415 32: 4095 4095 16: 0 0 64:<PHONE_NUMBER>207<PHONE_NUMBER>207 32: 2047 2047 16: 0 0 64:<PHONE_NUMBER>103<PHONE_NUMBER>103 32: 1023 1023 16: 0 0 64:<PHONE_NUMBER>551<PHONE_NUMBER>551 32: 511 511 16: 0 0 64:<PHONE_NUMBER>775<PHONE_NUMBER>775 32: 255 255 16: 0 0 64:<PHONE_NUMBER>87<PHONE_NUMBER>87 32: 127 127 16: 0 0 64:<PHONE_NUMBER>43<PHONE_NUMBER>43 32: 63 63 16: 0 0 64:<PHONE_NUMBER>71<PHONE_NUMBER>71 32: 31 31 16: 0 0 64:<PHONE_NUMBER>5<PHONE_NUMBER>5 32: 15 15 16: 0 0 64:<PHONE_NUMBER>7<PHONE_NUMBER>7 32: 7 7 16: 0 0 64:<PHONE_NUMBER>3<PHONE_NUMBER>3 32: 3 3 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 1 1 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 0 0 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 0 0 16: 0 0 64:<PHONE_NUMBER><PHONE_NUMBER> 32: 0 0 16: 0 0 64: 536870911 536870911 32: 0 0 16: 0 0 64: 268435455 268435455 32: 0 0 16: 0 0 64: 134217727 134217727 32: 0 0 16: 0 0 64: 67108863 67108863 32: 0 0 16: 0 0 64: 33554431 33554431 32: 0 0 16: 0 0 64: 16777215 16777215 32: 0 0 16: 0 0 64: 8388607 8388607 32: 0 0 16: 0 0 64: 4194303 4194303 32: 0 0 16: 0 0 64: 2097151 2097151 32: 0 0 16: 0 0 64: 1048575 1048575 32: 0 0 16: 0 0 64:<PHONE_NUMBER>87 32: 0 0 16: 0 0 64:<PHONE_NUMBER>43 32: 0 0 16: 0 0 64:<PHONE_NUMBER>71 32: 0 0 16: 0 0 64: 65535 65535 32: 0 0 16: 0 0 64: 32767 32767 32: 0 0 16: 0 0 64: 16383 16383 32: 0 0 16: 0 0 64: 8191 8191 32: 0 0 16: 0 0 64: 4095 4095 32: 0 0 16: 0 0 64: 2047 2047 32: 0 0 16: 0 0 64: 1023 1023 32: 0 0 16: 0 0 64: 511 511 32: 0 0 16: 0 0 64: 255 255 32: 0 0 16: 0 0 64: 127 127 32: 0 0 16: 0 0 64: 63 63 32: 0 0 16: 0 0 64: 31 31 32: 0 0 16: 0 0 64: 15 15 32: 0 0 16: 0 0 64: 7 7 32: 0 0 16: 0 0 64: 3 3 32: 0 0 16: 0 0 64: 1 1 32: 0 0 16: 0 0 64: 0 0 32: 0 0 16: 0 0 64: 0 0 32: 0 0 16: 0 0
1.6.3 Could we access the bits with shifts?
#include <stdio.h> #include <inttypes.h> #define BIT(x,b,y) ((x<<(b-y-1))>>(b-1)) #define BITS 32 int main() { // 0x7 = 0b0111 uint32_t u32 = 0xFF770ABE; char str[BITS+1] = { '\0' }; for (int i = 0 ; i < BITS; i++) { str[BITS-1-i] = (BIT(u32,BITS,i))?'1':'0'; } printf("u32: %s\n", str); }
u32:<PHONE_NUMBER>1101110000101010111110
- Bitprinter.h
#ifndef _BITPRINTER_H_ #define _BITPRINTER_H_ #include <inttypes.h> // Warning some bad hacks here #define BIT(x,b,y) ((x<<(b-y-1))>>(b-1)) #define MAXBITSTRBITS 129 static char _bitstr[MAXBITSTRBITS] = { '\0' }; static char * bitString(uint64_t value, const unsigned int bits) { for (unsigned int i = 0 ; i < bits; i++) { _bitstr[bits-1-i] = (BIT(value,bits,i))?'1':'0'; } _bitstr[bits] = '\0'; return _bitstr; } static char * bitString64(uint64_t value) { return bitString(value,64); } static char * bitString32(uint32_t value) { return bitString(value,32); } static char * bitString16(uint16_t value) { return bitString(value,16); } static char * bitString8(uint8_t value) { return bitString(value,8); } #endif
1.6.4 bitwise and
The & operator is bitwise complement. It means every bit of an integer is and'd.
The & operator is a binary operator.
- 0 & 0 -> 0
- 0 & 1 -> 0
- 1 & 0 -> 0
- 1 & 1 -> 1
0b11111111011101110000101010111110 & 0b1111111111111111111111111111111111111 0b11111111011101110000101010111110 & 0b0000000000000000000000000000000000000 0b0
0b011 & 0b010 == 0b010
0x00000001 & 0xFFFFFFFF = 0x1
// how to check a flag if (0x10 & input) { }
#include <stdio.h> #include <inttypes.h> int main() { uint32_t pressF = 0xFFFFFFFF; printf("Anding 1 bit\n"); for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("%12lu & %12lu = %12lu - 0x%08x\n", pressF, bit, pressF & bit, pressF & bit); } for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("%08x & %08x = %08x\n", pressF, bit, pressF & bit); } }
Anding 1 bit<PHONE_NUMBER> & 1 = 1 - 0x00000001<PHONE_NUMBER> & 2 = 2 - 0x00000002<PHONE_NUMBER> & 4 = 4 - 0x00000004<PHONE_NUMBER> & 8 = 8 - 0x00000008<PHONE_NUMBER> & 16 = 16 - 0x00000010<PHONE_NUMBER> & 32 = 32 - 0x00000020<PHONE_NUMBER> & 64 = 64 - 0x00000040<PHONE_NUMBER> & 128 = 128 - 0x00000080<PHONE_NUMBER> & 256 = 256 - 0x00000100<PHONE_NUMBER> & 512 = 512 - 0x00000200<PHONE_NUMBER> & 1024 = 1024 - 0x00000400<PHONE_NUMBER> & 2048 = 2048 - 0x00000800<PHONE_NUMBER> & 4096 = 4096 - 0x00001000<PHONE_NUMBER> & 8192 = 8192 - 0x00002000<PHONE_NUMBER> & 16384 = 16384 - 0x00004000<PHONE_NUMBER> & 32768 = 32768 - 0x00008000<PHONE_NUMBER> & 65536 = 65536 - 0x00010000<PHONE_NUMBER> & 131072 = 131072 - 0x00020000<PHONE_NUMBER> & 262144 = 262144 - 0x00040000<PHONE_NUMBER> & 524288 = 524288 - 0x00080000<PHONE_NUMBER> & 1048576 = 1048576 - 0x00100000<PHONE_NUMBER> & 2097152 = 2097152 - 0x00200000<PHONE_NUMBER> & 4194304 = 4194304 - 0x00400000<PHONE_NUMBER> & 8388608 = 8388608 - 0x00800000<PHONE_NUMBER> & 16777216 = 16777216 - 0x01000000<PHONE_NUMBER> & 33554432 = 33554432 - 0x02000000<PHONE_NUMBER> & 67108864 = 67108864 - 0x04000000<PHONE_NUMBER> & 134217728 = 134217728 - 0x08000000<PHONE_NUMBER> & 268435456 = 268435456 - 0x10000000<PHONE_NUMBER> & 536870912 = 536870912 - 0x20000000<PHONE_NUMBER> &<PHONE_NUMBER> =<PHONE_NUMBER> - 0x40000000<PHONE_NUMBER> &<PHONE_NUMBER> =<PHONE_NUMBER> - 0x80000000 ffffffff & 00000001 = 00000001 ffffffff & 00000002 = 00000002 ffffffff & 00000004 = 00000004 ffffffff & 00000008 = 00000008 ffffffff & 00000010 = 00000010 ffffffff & 00000020 = 00000020 ffffffff & 00000040 = 00000040 ffffffff & 00000080 = 00000080 ffffffff & 00000100 = 00000100 ffffffff & 00000200 = 00000200 ffffffff & 00000400 = 00000400 ffffffff & 00000800 = 00000800 ffffffff & 00001000 = 00001000 ffffffff & 00002000 = 00002000 ffffffff & 00004000 = 00004000 ffffffff & 00008000 = 00008000 ffffffff & 00010000 = 00010000 ffffffff & 00020000 = 00020000 ffffffff & 00040000 = 00040000 ffffffff & 00080000 = 00080000 ffffffff & 00100000 = 00100000 ffffffff & 00200000 = 00200000 ffffffff & 00400000 = 00400000 ffffffff & 00800000 = 00800000 ffffffff & 01000000 = 01000000 ffffffff & 02000000 = 02000000 ffffffff & 04000000 = 04000000 ffffffff & 08000000 = 08000000 ffffffff & 10000000 = 10000000 ffffffff & 20000000 = 20000000 ffffffff & 40000000 = 40000000 ffffffff & 80000000 = 80000000
- Uses of &
- Checking for bits
#include <inttypes.h> #include <stdio.h> // 0x1L MUST be used 0x1 causes bugs #define BIT(x,y) (x & (0x1L << y)) int main() { printf("%lu\n",sizeof(0x1L)); printf("%lu\n",sizeof(0x1)); }
8 4
#include <inttypes.h> #include <stdio.h> // 0x1L MUST be used 0x1 causes bugs #define BIT(x,y) (x & (0x1L << y)) int main() { uint64_t interesting = 0x0123456789ABCDEF; puts("Lets see some bits!"); for (size_t i = 64 ; i > 0; i--) { putchar(BIT(interesting, i-1)?'1':'0'); } putchar('\n'); for (size_t i = 0 ; i < 64; i++) { putchar('0' + i%10); } putchar('\n'); }
Lets see some bits!<PHONE_NUMBER>100011010001010110011110001001101010111100110111101111<PHONE_NUMBER>012345678901234567890123456789012345678901234567890123
- Bit Printing
& is way safer than shift for bit printing.
#include <inttypes.h> #include <stdio.h> // 0x1L MUST be used 0x1 causes bugs #define BIT(x,y) (x & (0x1L << y)) #define MAXBITSTRBITS 129 static char _bitstr[MAXBITSTRBITS] = { '\0' }; static char * bitString(uint64_t value, const unsigned int bits) { // iterator must be uint64_t for (uint64_t i = 0 ; i < bits; i++) { char bit = (BIT(value,i))?'1':'0'; _bitstr[bits-1-i] = bit; } _bitstr[bits] = '\0'; return _bitstr; } int main() { uint64_t interesting[] = { 0x7F, 0xFF, 0xFFF, 0xABE4BEEF, 0x7777777777, 0xFFFFFFFFFF, 0xABCDEF01234, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, }; const size_t nints = sizeof(interesting)/sizeof(uint64_t); printf("Interesting numbers!\n"); for (size_t i = 0 ; i < nints; i++) { printf("%16lx %20llu %s\n", interesting[i], interesting[i], bitString(interesting[i],64) ); } printf("Interesting[0] & Interesting[1]\n"); for (size_t i = 0 ; i < nints; i++) { printf("%s\n", bitString(interesting[i] & interesting[(i+1)%nints],64) ); } }
Interesting numbers! 7f<PHONE_NUMBER>000000000000000000000000000000000000000000000000001111111 ff<PHONE_NUMBER>000000000000000000000000000000000000000000000000011111111 fff 4095<PHONE_NUMBER>000000000000000000000000000000000000000000111111111111 abe4beef<PHONE_NUMBER><PHONE_NUMBER>000000000000000000000010101011111001001011111011101111<PHONE_NUMBER><PHONE_NUMBER>95<PHONE_NUMBER>000000000000000111011101110111011101110111011101110111 ffffffffff<PHONE_NUMBER>775<PHONE_NUMBER>000000000000001111111111111111111111111111111111111111 abcdef01234<PHONE_NUMBER>4660<PHONE_NUMBER>000000000010101011110011011110111100000001001000110100 7fffffffffffffff<PHONE_NUMBER>854775807<PHONE_NUMBER>111111111111111111111111111111111111111111111111111111 ffffffffffffffff<PHONE_NUMBER>3709551615<PHONE_NUMBER>111111111111111111111111111111111111111111111111111111 Interesting[0] & Interesting[1]<PHONE_NUMBER>000000000000000000000000000000000000000000000001111111<PHONE_NUMBER>000000000000000000000000000000000000000000000011111111<PHONE_NUMBER>000000000000000000000000000000000000000000111011101111<PHONE_NUMBER>000000000000000000000000100011011001000011011001100111<PHONE_NUMBER>000000000000000111011101110111011101110111011101110111<PHONE_NUMBER>000000000000001011110011011110111100000001001000110100<PHONE_NUMBER>000000000010101011110011011110111100000001001000110100<PHONE_NUMBER>111111111111111111111111111111111111111111111111111111<PHONE_NUMBER>000000000000000000000000000000000000000000000001111111
- Checking for bits
1.6.5 bitwise OR
The | operator is bitwise OR. It is the or operator between bits.
The | operator is a binary operator.
- 0 | 0 -> 0
- 0 | 1 -> 1
- 1 | 0 -> 1
- 1 | 1 -> 1
#include <stdio.h> #include <inttypes.h> int main() { uint32_t pressF = 0xFFFFFFFF; printf("ORing 1 bit\n"); for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("0x%08x | 0x%08x = 0x%08x\n", pressF, bit, pressF | bit); } pressF = 0x11111111; for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("0x%08x | 0x%08x = 0x%08x\n", pressF, bit, pressF | bit); } }
ORing 1 bit 0xffffffff | 0x00000001 = 0xffffffff 0xffffffff | 0x00000002 = 0xffffffff 0xffffffff | 0x00000004 = 0xffffffff 0xffffffff | 0x00000008 = 0xffffffff 0xffffffff | 0x00000010 = 0xffffffff 0xffffffff | 0x00000020 = 0xffffffff 0xffffffff | 0x00000040 = 0xffffffff 0xffffffff | 0x00000080 = 0xffffffff 0xffffffff | 0x00000100 = 0xffffffff 0xffffffff | 0x00000200 = 0xffffffff 0xffffffff | 0x00000400 = 0xffffffff 0xffffffff | 0x00000800 = 0xffffffff 0xffffffff | 0x00001000 = 0xffffffff 0xffffffff | 0x00002000 = 0xffffffff 0xffffffff | 0x00004000 = 0xffffffff 0xffffffff | 0x00008000 = 0xffffffff 0xffffffff | 0x00010000 = 0xffffffff 0xffffffff | 0x00020000 = 0xffffffff 0xffffffff | 0x00040000 = 0xffffffff 0xffffffff | 0x00080000 = 0xffffffff 0xffffffff | 0x00100000 = 0xffffffff 0xffffffff | 0x00200000 = 0xffffffff 0xffffffff | 0x00400000 = 0xffffffff 0xffffffff | 0x00800000 = 0xffffffff 0xffffffff | 0x01000000 = 0xffffffff 0xffffffff | 0x02000000 = 0xffffffff 0xffffffff | 0x04000000 = 0xffffffff 0xffffffff | 0x08000000 = 0xffffffff 0xffffffff | 0x10000000 = 0xffffffff 0xffffffff | 0x20000000 = 0xffffffff 0xffffffff | 0x40000000 = 0xffffffff 0xffffffff | 0x80000000 = 0xffffffff 0x11111111 | 0x00000001 = 0x11111111 0x11111111 | 0x00000002 = 0x11111113 0x11111111 | 0x00000004 = 0x11111115 0x11111111 | 0x00000008 = 0x11111119 0x11111111 | 0x00000010 = 0x11111111 0x11111111 | 0x00000020 = 0x11111131 0x11111111 | 0x00000040 = 0x11111151 0x11111111 | 0x00000080 = 0x11111191 0x11111111 | 0x00000100 = 0x11111111 0x11111111 | 0x00000200 = 0x11111311 0x11111111 | 0x00000400 = 0x11111511 0x11111111 | 0x00000800 = 0x11111911 0x11111111 | 0x00001000 = 0x11111111 0x11111111 | 0x00002000 = 0x11113111 0x11111111 | 0x00004000 = 0x11115111 0x11111111 | 0x00008000 = 0x11119111 0x11111111 | 0x00010000 = 0x11111111 0x11111111 | 0x00020000 = 0x11131111 0x11111111 | 0x00040000 = 0x11151111 0x11111111 | 0x00080000 = 0x11191111 0x11111111 | 0x00100000 = 0x11111111 0x11111111 | 0x00200000 = 0x11311111 0x11111111 | 0x00400000 = 0x11511111 0x11111111 | 0x00800000 = 0x11911111 0x11111111 | 0x01000000 = 0x11111111 0x11111111 | 0x02000000 = 0x13111111 0x11111111 | 0x04000000 = 0x15111111 0x11111111 | 0x08000000 = 0x19111111 0x11111111 | 0x10000000 = 0x11111111 0x11111111 | 0x20000000 = 0x31111111 0x11111111 | 0x40000000 = 0x51111111 0x11111111 | 0x80000000 = 0x91111111
1.6.6 bitwise XOR
The ^ operator is bitwise exclusive OR. It is the XOR operator between bits. It differs from or because when both inputs bits are hot the result in 0.
The ^ operator is a binary operator.
- 0 | 0 -> 0
- 0 | 1 -> 1
- 1 | 0 -> 1
- 1 | 1 -> 0
#include <stdio.h> #include <inttypes.h> int main() { uint32_t pressF = 0xFFFFFFFF; printf("ORing 1 bit\n"); for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("0x%08x ^ 0x%08x = 0x%08x\n", pressF, bit, pressF ^ bit); } pressF = 0x11111111; for (uint32_t i = 0 ; i < 32; i++) { uint32_t bit = (1 << i); printf("0x%08x ^ 0x%08x = 0x%08x\n", pressF, bit, pressF ^ bit); } }
ORing 1 bit 0xffffffff ^ 0x00000001 = 0xfffffffe 0xffffffff ^ 0x00000002 = 0xfffffffd 0xffffffff ^ 0x00000004 = 0xfffffffb 0xffffffff ^ 0x00000008 = 0xfffffff7 0xffffffff ^ 0x00000010 = 0xffffffef 0xffffffff ^ 0x00000020 = 0xffffffdf 0xffffffff ^ 0x00000040 = 0xffffffbf 0xffffffff ^ 0x00000080 = 0xffffff7f 0xffffffff ^ 0x00000100 = 0xfffffeff 0xffffffff ^ 0x00000200 = 0xfffffdff 0xffffffff ^ 0x00000400 = 0xfffffbff 0xffffffff ^ 0x00000800 = 0xfffff7ff 0xffffffff ^ 0x00001000 = 0xffffefff 0xffffffff ^ 0x00002000 = 0xffffdfff 0xffffffff ^ 0x00004000 = 0xffffbfff 0xffffffff ^ 0x00008000 = 0xffff7fff 0xffffffff ^ 0x00010000 = 0xfffeffff 0xffffffff ^ 0x00020000 = 0xfffdffff 0xffffffff ^ 0x00040000 = 0xfffbffff 0xffffffff ^ 0x00080000 = 0xfff7ffff 0xffffffff ^ 0x00100000 = 0xffefffff 0xffffffff ^ 0x00200000 = 0xffdfffff 0xffffffff ^ 0x00400000 = 0xffbfffff 0xffffffff ^ 0x00800000 = 0xff7fffff 0xffffffff ^ 0x01000000 = 0xfeffffff 0xffffffff ^ 0x02000000 = 0xfdffffff 0xffffffff ^ 0x04000000 = 0xfbffffff 0xffffffff ^ 0x08000000 = 0xf7ffffff 0xffffffff ^ 0x10000000 = 0xefffffff 0xffffffff ^ 0x20000000 = 0xdfffffff 0xffffffff ^ 0x40000000 = 0xbfffffff 0xffffffff ^ 0x80000000 = 0x7fffffff 0x11111111 ^ 0x00000001 = 0x11111110 0x11111111 ^ 0x00000002 = 0x11111113 0x11111111 ^ 0x00000004 = 0x11111115 0x11111111 ^ 0x00000008 = 0x11111119 0x11111111 ^ 0x00000010 = 0x11111101 0x11111111 ^ 0x00000020 = 0x11111131 0x11111111 ^ 0x00000040 = 0x11111151 0x11111111 ^ 0x00000080 = 0x11111191 0x11111111 ^ 0x00000100 = 0x11111011 0x11111111 ^ 0x00000200 = 0x11111311 0x11111111 ^ 0x00000400 = 0x11111511 0x11111111 ^ 0x00000800 = 0x11111911 0x11111111 ^ 0x00001000 = 0x11110111 0x11111111 ^ 0x00002000 = 0x11113111 0x11111111 ^ 0x00004000 = 0x11115111 0x11111111 ^ 0x00008000 = 0x11119111 0x11111111 ^ 0x00010000 = 0x11101111 0x11111111 ^ 0x00020000 = 0x11131111 0x11111111 ^ 0x00040000 = 0x11151111 0x11111111 ^ 0x00080000 = 0x11191111 0x11111111 ^ 0x00100000 = 0x11011111 0x11111111 ^ 0x00200000 = 0x11311111 0x11111111 ^ 0x00400000 = 0x11511111 0x11111111 ^ 0x00800000 = 0x11911111 0x11111111 ^ 0x01000000 = 0x10111111 0x11111111 ^ 0x02000000 = 0x13111111 0x11111111 ^ 0x04000000 = 0x15111111 0x11111111 ^ 0x08000000 = 0x19111111 0x11111111 ^ 0x10000000 = 0x01111111 0x11111111 ^ 0x20000000 = 0x31111111 0x11111111 ^ 0x40000000 = 0x51111111 0x11111111 ^ 0x80000000 = 0x91111111
- Uses of ^
XOR has interesting properties .
a ^ b ^ a = b
a ^ b != b for most values
(a ^ b) ^ a = b
(a ^ b) ^ b = a
So if we send a message of ab we can decode the message of (ab)b
#include <stdio.h> #include <inttypes.h> void encrypt(char * output, const char * key, char * input) { const size_t keylen = strlen(key); const size_t inputlen = strlen(input); for (size_t i = 0 ; i < inputlen; i++) { output[i] = input[i] ^ key[i%keylen]; } output[inputlen] = '\0'; } int main() { const char * key = "EATFOOD"; const size_t keylen = strlen(key); char * input = "I enjoy olive bread"; char output[1024] = {'\0'}; encrypt(output, key, input); printf("Encrypted: %s\n", output); encrypt(output, key, output); printf("Encrypted Again: %s\n", output); }
Encrypted: a1(% =e.8/9*d'31'+ Encrypted Again: I enjoy olive bread
1.6.7 bitwise complement
The ~ operator is bitwise complement. It means every bit is notted The ~ operator is a unary operator.
- 0 -> 1
- 1 -> 0
#include <stdio.h> #include <inttypes.h> int main() { uint8_t u8 = 0; for (uint32_t i = 0 ; i < 256; i+=6) { u8 = (uint8_t)i; // be careful about how you deal with chars printf("%3hhu %3hhu\t-- %3u %3u\n", u8, ~u8, u8, ~u8); } }
0 255 -- 0<PHONE_NUMBER> 6 249 -- 6<PHONE_NUMBER> 12 243 -- 12<PHONE_NUMBER> 18 237 -- 18<PHONE_NUMBER> 24 231 -- 24<PHONE_NUMBER> 30 225 -- 30<PHONE_NUMBER> 36 219 -- 36<PHONE_NUMBER> 42 213 -- 42<PHONE_NUMBER> 48 207 -- 48<PHONE_NUMBER> 54 201 -- 54<PHONE_NUMBER> 60 195 -- 60<PHONE_NUMBER> 66 189 -- 66<PHONE_NUMBER> 72 183 -- 72<PHONE_NUMBER> 78 177 -- 78<PHONE_NUMBER> 84 171 -- 84<PHONE_NUMBER> 90 165 -- 90<PHONE_NUMBER> 96 159 -- 96<PHONE_NUMBER> 102<PHONE_NUMBER>967193 108<PHONE_NUMBER>967187 114<PHONE_NUMBER>967181 120<PHONE_NUMBER>967175 126<PHONE_NUMBER>967169 132<PHONE_NUMBER>967163 138<PHONE_NUMBER>967157 144<PHONE_NUMBER>967151 150<PHONE_NUMBER>967145 156 99 --<PHONE_NUMBER>139 162 93 --<PHONE_NUMBER>133 168 87 --<PHONE_NUMBER>127 174 81 --<PHONE_NUMBER>121 180 75 --<PHONE_NUMBER>115 186 69 --<PHONE_NUMBER>109 192 63 --<PHONE_NUMBER>103 198 57 --<PHONE_NUMBER>097 204 51 --<PHONE_NUMBER>091 210 45 --<PHONE_NUMBER>085 216 39 --<PHONE_NUMBER>079 222 33 --<PHONE_NUMBER>073 228 27 --<PHONE_NUMBER>067 234 21 --<PHONE_NUMBER>061 240 15 --<PHONE_NUMBER>055 246 9 --<PHONE_NUMBER>049 252 3 --<PHONE_NUMBER>043
1.6.8 Flags
In C flags are often used. Flags are parameters who's bits indicate if some option is chosen.
`man 2 open` has the open function
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags);
open is part of POSIX systems like linux. open takes a file name (pathname) and takes flags:
- ORDONLY - Read the file
- OWRONLY - Write to the file
- ORDWR - Read and Write to the file
- OCREAT - CREATE the file if it doesn't exist
- OAPPEND - append to existing file
But how do we open a file for writing that creates a new file with only 1 argument?
The bitwise | operator!
OWRONLY | OCREAT will allow us to open a file for writing that will create the file on disk if it doesn't already exist!
// in /usr/include/asm-generic/fcntl.h (used by fcntl.h) // we see the definitions: #define O_ACCMODE 00000003 #define O_RDONLY 00000000 #define O_WRONLY 00000001 #define O_RDWR 00000002 #ifndef O_CREAT #define O_CREAT 00000100 /* not fcntl */ #endif #ifndef O_EXCL #define O_EXCL 00000200 /* not fcntl */ #endif #ifndef O_NOCTTY #define O_NOCTTY 00000400 /* not fcntl */ #endif #ifndef O_TRUNC #define O_TRUNC 00001000 /* not fcntl */ #endif #ifndef O_APPEND #define O_APPEND 00002000 #endif open("~/.bashrc", O_RDONLY); // open ~/.bashrc for read only access open("/tmp/map.txt", O_WRONLY | O_CREATE); // open /tmp/map.txt to write to it // 00000101 open("./log.txt", O_WRONLY | O_CREATE | O_APPEND); // open ./map.txt to write to it or append to it // 00002101 // So O_WRONLY | O_CREATE == 00000101 (00000001 | 00000100) O_WRONLY | O_CREATE | O_APPEND == 00002101 (00000001 | 00000100 | 00002000) if (00002101 & O_CREATE) { printf("O_CREATE is set!\n"); } // Question wht number format are they using to define these flags?
warning: open is just an example. Please use fopen if you want to read / write files.
warning: flags should only be unsigned integers
warning: flags should be not 0
- Expected Flag use:
Use | to set a flag: FLAG1 | FLAG2
Use & to check a flag: input & FLAG1
Watch out testing for truth and combining flags
To check for FLAG1 and FLAG2 being hot you should:
`if ((input & FLAG1) && (input & FLAG2)) `
or
`if (input & (FLAG1 | FLAG2) == (FLAG1 | FLAG2))`
It's very convoluted so don't get too complicated
This is a bug: ` // actually checks if FLAG1 or FLAG2 is set (FLAG1, FLAG2, FLAG1|FLAG2) if (input & (FLAG1 | FLAG2))`
It actually just checks if you have either FLAG1 or FLAG2.
- Flags Example
Hazel talks in depth about pizza
OK build some pizza using | A pizza of cheese, corn (srsly?), . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), exotic meats, . OK build some pizza via iteration A pizza of . A pizza of cheese, . A pizza of thin crust, . A pizza of cheese, thin crust, . A pizza of thick crust, . A pizza of cheese, thick crust, . A pizza of thin crust, thick crust, . A pizza of cheese, thin crust, thick crust, . A pizza of tomato sauce, . A pizza of cheese, tomato sauce, . A pizza of thin crust, tomato sauce, . A pizza of cheese, thin crust, tomato sauce, . A pizza of thick crust, tomato sauce, . A pizza of cheese, thick crust, tomato sauce, . A pizza of thin crust, thick crust, tomato sauce, . A pizza of cheese, thin crust, thick crust, tomato sauce, . A pizza of black metal sauce, . A pizza of cheese, black metal sauce, . A pizza of thin crust, black metal sauce, . A pizza of cheese, thin crust, black metal sauce, . A pizza of thick crust, black metal sauce, . A pizza of cheese, thick crust, black metal sauce, . A pizza of thin crust, thick crust, black metal sauce, . A pizza of cheese, thin crust, thick crust, black metal sauce, . A pizza of tomato sauce, black metal sauce, . A pizza of cheese, tomato sauce, black metal sauce, . A pizza of thin crust, tomato sauce, black metal sauce, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, . A pizza of thick crust, tomato sauce, black metal sauce, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, . A pizza of corn (srsly?), . A pizza of cheese, corn (srsly?), . A pizza of thin crust, corn (srsly?), . A pizza of cheese, thin crust, corn (srsly?), . A pizza of thick crust, corn (srsly?), . A pizza of cheese, thick crust, corn (srsly?), . A pizza of thin crust, thick crust, corn (srsly?), . A pizza of cheese, thin crust, thick crust, corn (srsly?), . A pizza of tomato sauce, corn (srsly?), . A pizza of cheese, tomato sauce, corn (srsly?), . A pizza of thin crust, tomato sauce, corn (srsly?), . A pizza of cheese, thin crust, tomato sauce, corn (srsly?), . A pizza of thick crust, tomato sauce, corn (srsly?), . A pizza of cheese, thick crust, tomato sauce, corn (srsly?), . A pizza of thin crust, thick crust, tomato sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, tomato sauce, corn (srsly?), . A pizza of black metal sauce, corn (srsly?), . A pizza of cheese, black metal sauce, corn (srsly?), . A pizza of thin crust, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), . A pizza of thick crust, black metal sauce, corn (srsly?), . A pizza of cheese, thick crust, black metal sauce, corn (srsly?), . A pizza of thin crust, thick crust, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, black metal sauce, corn (srsly?), . A pizza of tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thin crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of onions, . A pizza of cheese, onions, . A pizza of thin crust, onions, . A pizza of cheese, thin crust, onions, . A pizza of thick crust, onions, . A pizza of cheese, thick crust, onions, . A pizza of thin crust, thick crust, onions, . A pizza of cheese, thin crust, thick crust, onions, . A pizza of tomato sauce, onions, . A pizza of cheese, tomato sauce, onions, . A pizza of thin crust, tomato sauce, onions, . A pizza of cheese, thin crust, tomato sauce, onions, . A pizza of thick crust, tomato sauce, onions, . A pizza of cheese, thick crust, tomato sauce, onions, . A pizza of thin crust, thick crust, tomato sauce, onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, onions, . A pizza of black metal sauce, onions, . A pizza of cheese, black metal sauce, onions, . A pizza of thin crust, black metal sauce, onions, . A pizza of cheese, thin crust, black metal sauce, onions, . A pizza of thick crust, black metal sauce, onions, . A pizza of cheese, thick crust, black metal sauce, onions, . A pizza of thin crust, thick crust, black metal sauce, onions, . A pizza of cheese, thin crust, thick crust, black metal sauce, onions, . A pizza of tomato sauce, black metal sauce, onions, . A pizza of cheese, tomato sauce, black metal sauce, onions, . A pizza of thin crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, onions, . A pizza of thick crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, onions, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, onions, . A pizza of corn (srsly?), onions, . A pizza of cheese, corn (srsly?), onions, . A pizza of thin crust, corn (srsly?), onions, . A pizza of cheese, thin crust, corn (srsly?), onions, . A pizza of thick crust, corn (srsly?), onions, . A pizza of cheese, thick crust, corn (srsly?), onions, . A pizza of thin crust, thick crust, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, corn (srsly?), onions, . A pizza of tomato sauce, corn (srsly?), onions, . A pizza of cheese, tomato sauce, corn (srsly?), onions, . A pizza of thin crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, tomato sauce, corn (srsly?), onions, . A pizza of thick crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of black metal sauce, corn (srsly?), onions, . A pizza of cheese, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), onions, . A pizza of thick crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of exotic meats, . A pizza of cheese, exotic meats, . A pizza of thin crust, exotic meats, . A pizza of cheese, thin crust, exotic meats, . A pizza of thick crust, exotic meats, . A pizza of cheese, thick crust, exotic meats, . A pizza of thin crust, thick crust, exotic meats, . A pizza of cheese, thin crust, thick crust, exotic meats, . A pizza of tomato sauce, exotic meats, . A pizza of cheese, tomato sauce, exotic meats, . A pizza of thin crust, tomato sauce, exotic meats, . A pizza of cheese, thin crust, tomato sauce, exotic meats, . A pizza of thick crust, tomato sauce, exotic meats, . A pizza of cheese, thick crust, tomato sauce, exotic meats, . A pizza of thin crust, thick crust, tomato sauce, exotic meats, . OK check some pizzas yo A pizza of cheese, thin crust, thick crust, corn (srsly?), . WARNING: Impossible to make a thin / thick crust pizza! WARNING: Corn? REALLY? A pizza of cheese, thin crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thin crust, . WARNING: SPAAAAAAAARTA, or spartan. Let's use XOR to toggle flags! A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan.
#include <inttypes.h> #include <stdio.h> enum pizza_toppings { CHEESE = 0x1, THINCRUST = 0x2, THICKCRUST = 0x4, TOMATOSAUCE = 0x8, BLACKMETALSAUCE = 0x10, CORN = 0x20, // Hazel's favourite topping ONIONS = 0x40, MYSTERYMEAT = 0x80, }; typedef uint32_t PizzaFlags; void printPizza(PizzaFlags pizza) { // Don't do what doctor hindle does. // you should use a loop over the constants instead printf("A pizza of %s%s%s%s%s%s%s%s.\n", (pizza & CHEESE)?"cheese, ":"", (pizza & THINCRUST)?"thin crust, ":"", (pizza & THICKCRUST)?"thick crust, ":"", (pizza & TOMATOSAUCE)?"tomato sauce, ":"", (pizza & BLACKMETALSAUCE)?"black metal sauce, ":"", (pizza & CORN)?"corn (srsly?), ":"", (pizza & ONIONS)?"onions, ":"", (pizza & MYSTERYMEAT)?"exotic meats, ":"" ); } void violation(PizzaFlags pizza) { // Not all pizzas are created equal // if (pizza & (THINCRUST | THICKCRUST)) { if (pizza & THINCRUST && pizza & THICKCRUST) { printf("WARNING: Impossible to make a thin / thick crust pizza!\n"); } // if (! (pizza & THINCRUST ) && ! (pizza & THICKCURST) ) { if (! (pizza & (THINCRUST | THICKCRUST))) { printf("WARNING: No Crust???\n"); } if (pizza & CORN) { printf("WARNING: Corn? REALLY?\n"); } if (pizza == (CHEESE | THINCRUST) || pizza == (CHEESE | THICKCRUST)) { printf("WARNING: SPAAAAAAAARTA, or spartan.\n"); } } int main() { printf("OK build some pizza using |\n\n\n"); printPizza(CHEESE | CORN); printPizza(CHEESE | THINCRUST | BLACKMETALSAUCE | CORN | MYSTERYMEAT); printf("\n\nOK build some pizza via iteration\n\n\n"); for (uint32_t i = 0; i < 0x8F; i++) { printPizza((PizzaFlags)i); } // Check the pizza printf("\n\nOK check some pizzas yo\n\n\n"); PizzaFlags pizza = CHEESE | CORN | THINCRUST | THICKCRUST; printPizza(pizza); violation(pizza); pizza = CHEESE | CORN | THINCRUST; printPizza(pizza); violation(pizza); pizza = CHEESE | THINCRUST; printPizza(pizza); violation(pizza); // TOGGLE printf("\n\nLet's use XOR to toggle flags!\n"); pizza = CHEESE | CORN | THINCRUST | THICKCRUST; pizza = pizza ^ CORN; // NOPE pizza = pizza ^ THINCRUST; // NOPE printPizza(pizza); violation(pizza); pizza = pizza ^ CORN; // CORN IS BACK! printPizza(pizza); violation(pizza); for (int i = 0 ; i < 5; i++) { pizza = pizza ^ CORN; // CORN IS BACK! printPizza(pizza); violation(pizza); } }
OK build some pizza using | A pizza of cheese, corn (srsly?), . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), exotic meats, . OK build some pizza via iteration A pizza of . A pizza of cheese, . A pizza of thin crust, . A pizza of cheese, thin crust, . A pizza of thick crust, . A pizza of cheese, thick crust, . A pizza of thin crust, thick crust, . A pizza of cheese, thin crust, thick crust, . A pizza of tomato sauce, . A pizza of cheese, tomato sauce, . A pizza of thin crust, tomato sauce, . A pizza of cheese, thin crust, tomato sauce, . A pizza of thick crust, tomato sauce, . A pizza of cheese, thick crust, tomato sauce, . A pizza of thin crust, thick crust, tomato sauce, . A pizza of cheese, thin crust, thick crust, tomato sauce, . A pizza of black metal sauce, . A pizza of cheese, black metal sauce, . A pizza of thin crust, black metal sauce, . A pizza of cheese, thin crust, black metal sauce, . A pizza of thick crust, black metal sauce, . A pizza of cheese, thick crust, black metal sauce, . A pizza of thin crust, thick crust, black metal sauce, . A pizza of cheese, thin crust, thick crust, black metal sauce, . A pizza of tomato sauce, black metal sauce, . A pizza of cheese, tomato sauce, black metal sauce, . A pizza of thin crust, tomato sauce, black metal sauce, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, . A pizza of thick crust, tomato sauce, black metal sauce, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, . A pizza of corn (srsly?), . A pizza of cheese, corn (srsly?), . A pizza of thin crust, corn (srsly?), . A pizza of cheese, thin crust, corn (srsly?), . A pizza of thick crust, corn (srsly?), . A pizza of cheese, thick crust, corn (srsly?), . A pizza of thin crust, thick crust, corn (srsly?), . A pizza of cheese, thin crust, thick crust, corn (srsly?), . A pizza of tomato sauce, corn (srsly?), . A pizza of cheese, tomato sauce, corn (srsly?), . A pizza of thin crust, tomato sauce, corn (srsly?), . A pizza of cheese, thin crust, tomato sauce, corn (srsly?), . A pizza of thick crust, tomato sauce, corn (srsly?), . A pizza of cheese, thick crust, tomato sauce, corn (srsly?), . A pizza of thin crust, thick crust, tomato sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, tomato sauce, corn (srsly?), . A pizza of black metal sauce, corn (srsly?), . A pizza of cheese, black metal sauce, corn (srsly?), . A pizza of thin crust, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), . A pizza of thick crust, black metal sauce, corn (srsly?), . A pizza of cheese, thick crust, black metal sauce, corn (srsly?), . A pizza of thin crust, thick crust, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, black metal sauce, corn (srsly?), . A pizza of tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thin crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), . A pizza of onions, . A pizza of cheese, onions, . A pizza of thin crust, onions, . A pizza of cheese, thin crust, onions, . A pizza of thick crust, onions, . A pizza of cheese, thick crust, onions, . A pizza of thin crust, thick crust, onions, . A pizza of cheese, thin crust, thick crust, onions, . A pizza of tomato sauce, onions, . A pizza of cheese, tomato sauce, onions, . A pizza of thin crust, tomato sauce, onions, . A pizza of cheese, thin crust, tomato sauce, onions, . A pizza of thick crust, tomato sauce, onions, . A pizza of cheese, thick crust, tomato sauce, onions, . A pizza of thin crust, thick crust, tomato sauce, onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, onions, . A pizza of black metal sauce, onions, . A pizza of cheese, black metal sauce, onions, . A pizza of thin crust, black metal sauce, onions, . A pizza of cheese, thin crust, black metal sauce, onions, . A pizza of thick crust, black metal sauce, onions, . A pizza of cheese, thick crust, black metal sauce, onions, . A pizza of thin crust, thick crust, black metal sauce, onions, . A pizza of cheese, thin crust, thick crust, black metal sauce, onions, . A pizza of tomato sauce, black metal sauce, onions, . A pizza of cheese, tomato sauce, black metal sauce, onions, . A pizza of thin crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, onions, . A pizza of thick crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, onions, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, onions, . A pizza of corn (srsly?), onions, . A pizza of cheese, corn (srsly?), onions, . A pizza of thin crust, corn (srsly?), onions, . A pizza of cheese, thin crust, corn (srsly?), onions, . A pizza of thick crust, corn (srsly?), onions, . A pizza of cheese, thick crust, corn (srsly?), onions, . A pizza of thin crust, thick crust, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, corn (srsly?), onions, . A pizza of tomato sauce, corn (srsly?), onions, . A pizza of cheese, tomato sauce, corn (srsly?), onions, . A pizza of thin crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, tomato sauce, corn (srsly?), onions, . A pizza of thick crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, corn (srsly?), onions, . A pizza of black metal sauce, corn (srsly?), onions, . A pizza of cheese, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, black metal sauce, corn (srsly?), onions, . A pizza of thick crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, black metal sauce, corn (srsly?), onions, . A pizza of tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of cheese, thin crust, thick crust, tomato sauce, black metal sauce, corn (srsly?), onions, . A pizza of exotic meats, . A pizza of cheese, exotic meats, . A pizza of thin crust, exotic meats, . A pizza of cheese, thin crust, exotic meats, . A pizza of thick crust, exotic meats, . A pizza of cheese, thick crust, exotic meats, . A pizza of thin crust, thick crust, exotic meats, . A pizza of cheese, thin crust, thick crust, exotic meats, . A pizza of tomato sauce, exotic meats, . A pizza of cheese, tomato sauce, exotic meats, . A pizza of thin crust, tomato sauce, exotic meats, . A pizza of cheese, thin crust, tomato sauce, exotic meats, . A pizza of thick crust, tomato sauce, exotic meats, . A pizza of cheese, thick crust, tomato sauce, exotic meats, . A pizza of thin crust, thick crust, tomato sauce, exotic meats, . OK check some pizzas yo A pizza of cheese, thin crust, thick crust, corn (srsly?), . WARNING: Impossible to make a thin / thick crust pizza! WARNING: Corn? REALLY? A pizza of cheese, thin crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thin crust, . WARNING: SPAAAAAAAARTA, or spartan. Let's use XOR to toggle flags! A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan. A pizza of cheese, thick crust, corn (srsly?), . WARNING: Corn? REALLY? A pizza of cheese, thick crust, . WARNING: SPAAAAAAAARTA, or spartan.
1.6.9 PRACTICE and More resources
- PLEASE Read Hazel's Notes on the subject
- https://docs.google.com/document/d/1S-I-OthHf0mgNZXnTSOvyx5lSduQwsbHYMmVPLtTl1Y/edit#
- UAlberta accounts only
- Please READ CHAPTER 20 OF THE TEXTBOOK. Then practice your binary skills!
1.6.10 BitFields
struct bitfield { unsigned int b0: 1; unsigned int b1: 2; unsigned int b2: 4; unsigned int b3: 8; unsigned int b4: 16; //unsigned int b5: 1; //unsigned int b6: 32; }; int main() { struct bitfield b = { 0, 3, 12, 242, 65000}; union bex { unsigned int i; struct bitfield b; } bi; bi.b = b; printf("%hhu %hhu %hhu %hhu %hu\n", b.b0, b.b1, b.b2, b.b3, b.b4); printf("size: %lu\n", sizeof(b)); printf("%08x\n", bi.i); }
0 3 12 242 65000 size: 4 7ef47966
111 100 10 (b3)(745) [1 100(b2)](54) [11 (b1) 0 (b0)]6
1111 F 2 242
- Chmod example
Let's try bitfields and unix file permissions!
- 1 execute
- 2 write
- 4 read
- 7 read | write | execute
@piggy:/tmp/coolbears$ touch what @piggy:/tmp/coolbears$ ls -l what -rw-r--r-- 1 hindle1 hindle1 0 Mar 26 10:14 what @piggy:/tmp/coolbears$ echo NEAT > what @piggy:/tmp/coolbears$ cat what NEAT @piggy:/tmp/coolbears$ ls -l what -rw-r--r-- 1 hindle1 hindle1 5 Mar 26 10:14 what @piggy:/tmp/coolbears$ chmod 000 what @piggy:/tmp/coolbears$ cat what cat: what: Permission denied @piggy:/tmp/coolbears$ ls -l what ---------- 1 hindle1 hindle1 5 Mar 26 10:14 what @piggy:/tmp/coolbears$ chmod 444 what @piggy:/tmp/coolbears$ ls -l total 4 -r--r--r-- 1 hindle1 hindle1 5 Mar 26 10:14 what @piggy:/tmp/coolbears$ cat what NEAT @piggy:/tmp/coolbears$ echo echo do I hear an echo > what -bash: what: Permission denied @piggy:/tmp/coolbears$ chmod 666 what @piggy:/tmp/coolbears$ echo echo do I hear an echo > what @piggy:/tmp/coolbears$ cat what echo do I hear an echo @piggy:/tmp/coolbears$ ./what -bash: ./what: Permission denied @piggy:/tmp/coolbears$ chmod 777 what @piggy:/tmp/coolbears$ ./what do I hear an echo @piggy:/tmp/coolbears$ cat what echo do I hear an echo @piggy:/tmp/coolbears$ chmod 711 what @piggy:/tmp/coolbears$ ls -l what -rwx--x--x 1 hindle1 hindle1 23 Mar 26 10:15 what
Let's go to that chmod example I gave you where I set the
#include <stdio.h> struct unixperms { unsigned int execute: 1; unsigned int write: 1; unsigned int read: 1; unsigned int: 5; // try filling up to the next storage unit see what happens }; // these don't stack well struct fullperms { struct unixperms user; struct unixperms group; struct unixperms all; }; // GCC is OK with this but clang is not so impressed union unixpun { unsigned int uint; struct unixperms perm; }; union fullpun { unsigned char bytes[sizeof(struct fullperms)]; struct fullperms perm; }; int main() { union unixpun nonePun = { .perm=(struct unixperms){0,0,0} }; union unixpun readPun = { .perm=(struct unixperms){0,0,1} }; union unixpun readWritePun = { .perm=(struct unixperms){0,1,1} }; union unixpun readWriteExecPun = { .perm=(struct unixperms){1,1,1} }; union unixpun readExecPun = { .perm=(struct unixperms){1,0,1} }; union unixpun puns[] = { nonePun, readPun, readWritePun, readWriteExecPun, readExecPun }; char * names[] = { "nonePun", "readPun", "readWritePun", "readWriteExecPun", "readExecPun" }; size_t npuns = sizeof(puns)/sizeof(puns[0]); printf("sizeof(struct unixperms)=%lu\n",sizeof(struct unixperms)); for (size_t i = 0; i < npuns; i++) { printf("Pun:%16s E:%hhu W:%hhu R:%hhu [%4hhu] [%08x]\n", names[i], puns[i].perm.execute, puns[i].perm.write, puns[i].perm.read, puns[i].uint, // here's the undefined behaviour puns[i].uint // here's the undefined behaviour ); } struct fullperms aFile = { readExecPun.perm, readExecPun.perm, nonePun.perm }; union fullpun fullPun = { .perm=aFile }; printf("sizeof(fullPun)==%lu\n", sizeof(fullPun)); for (size_t i = 0 ; i < sizeof(struct fullperms); i++) { printf("%02x", fullPun.bytes[i]); } puts(""); printf("^^^ well that is confusing\n"); // Let's do this better struct betterunixperms { unsigned int ue: 1; unsigned int uw: 1; unsigned int ur: 1; unsigned int ge: 1; unsigned int gw: 1; unsigned int gr: 1; unsigned int ae: 1; unsigned int aw: 1; unsigned int ar: 1; unsigned int: 7; // pad }; struct chmod { unsigned int u: 3; unsigned int g: 3; unsigned int a: 3; unsigned int: 7; // pad }; union chmodb { struct chmod ch; struct betterunixperms bits; unsigned short ushort; unsigned int uint; }; union chmodb ch = { .ch={7,1,0} }; printf("sizeof(betterunixperms)==%lu\n",sizeof(struct betterunixperms)); printf("sizeof(chmod)==%lu\n",sizeof(struct chmod)); printf("sizeof(chmodb)==%lu\n",sizeof(union chmodb)); printf("Better? %o %o %o %03o\n", ch.ch.u, ch.ch.g, ch.ch.a, ch.uint); }
sizeof(struct unixperms)=4 Pun: nonePun E:0 W:0 R:0 [ 0] [00000000] Pun: readPun E:0 W:0 R:1 [ 4] [00000004] Pun: readWritePun E:0 W:1 R:1 [ 6] [00000006] Pun:readWriteExecPun E:1 W:1 R:1 [ 7] [00000007] Pun: readExecPun E:1 W:0 R:1 [ 5] [00000005] sizeof(fullPun)==12<PHONE_NUMBER>00000000000000 ^^^ well that is confusing sizeof(betterunixperms)==4 sizeof(chmod)==4 sizeof(chmodb)==4 Better? 7 1 0 017
1.7 Floating Point Numbers
1.7.1 Resources
1.7.2 Single precision IEEE754 floating point numbers
Type: float
-Sign: 0 - positive, 1 negative -Exponent: unsigned 8 bit integer with 127 (27-1) offset
- -126 to 127
- Fraction: 24 bits (only 23 used). The first bit is implied.
- So<PHONE_NUMBER>0000000000000 is actually<PHONE_NUMBER>00000000000000
| Sign (1 bit) | Exponent (8 bit) | b10 | Fraction (23 bit) | Value | base 2 |
| 0 | 01111100 | -3 |<PHONE_NUMBER>0000000000000 | 0.15625 | 1/8 + 1/32 |
| 1 | 01111100 | -3 |<PHONE_NUMBER>0000000000000 | -0.15625 | |
| 0 | 01111100 | -3 |<PHONE_NUMBER>0000000000000 | -0.15625 | |
| 0 | 00000000 | 0 |<PHONE_NUMBER>0000000000000 | 0 | |
| 1 | 00000000 | 0 |<PHONE_NUMBER>0000000000000 | -0 | |
| 0 | 11000000 | 65 |<PHONE_NUMBER>0000000000000 | 3.68e+19 | 265 |
| 1 | 11111111 | 0 |<PHONE_NUMBER>0000000000000 | -inf | |
| 0 | 11111111 | 0 |<PHONE_NUMBER>0000000000000 | inf | |
| 0 | 11111111 | 0 |<PHONE_NUMBER>0000000000000 | nan | |
| 0 | 11111111 | 0 |<PHONE_NUMBER>0000000000001 | nan |
- Neat design trick: you can sort them as signed integers (two's complement)!
- -0 is right before 0 for signed integer sorting
| 0 | 01111100 | -3 |<PHONE_NUMBER>0000000000000 | 0.15625 | 1/8 + 1/32 |
0.15625 to floating point. Implied bit is pow(2,-3)
pow(2,-3) + 0*pow(2,-4) + 1*pow(2,-5) (pow(2,5)==32, pow(2,-5) = 1/32.0)
1.7.3 Let's explore the bits of floats
#include <math.h> #include <stdio.h> #include <inttypes.h> // 0x1L MUST be used 0x1 causes bugs #define BIT(x,y) (x & (0x1L << y)) #define MAXBITSTRBITS 129 static char _bitstr[MAXBITSTRBITS] = { '\0' }; static char * bitString(uint64_t value, const unsigned int bits) { // iterator must be uint64_t for (uint64_t i = 0 ; i < bits; i++) { char bit = (BIT(value,i))?'1':'0'; _bitstr[bits-1-i] = bit; } _bitstr[bits] = '\0'; return _bitstr; } // NOT PORTABLE struct float_t { unsigned int mantissa:23; // LOWEST unsigned int exponent:8; unsigned int sign:1; // HIGHEST }; // type pun for fun! union floatint { float f; uint32_t i; struct float_t t; }; int main() { float floats[] = { 0.0, -0.0, INFINITY, -INFINITY, NAN, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0, 1/64.0, 1/32.0, 1/16.0, 1/8.0, 1/4.0, 1/2.0, 1.0, 2.0, 4.0, 128.0, 65536.0, 0.15625, -0.15625,<PHONE_NUMBER>7419103232.0, // 2**65 -36893488147419103232.0 // 2**65 }; size_t nfloats = sizeof(floats)/sizeof(float); printf("We're printing floats!\n"); printf("sizeof(floatint) == %lu\n", sizeof(union floatint)); printf("sizeof(float_t) == %lu\n", sizeof(struct float_t)); for (size_t i = 0; i < nfloats; i++) { float f = floats[i]; union floatint fi = {.f=f}; printf("%12g 0x%08x 0b%s\n", f, fi.i, bitString(fi.i, 32)); printf("%12g 0x%08x sign: %hhu exponent: %3hhu exp-127: %4d mantissa: 0x%06x\n", f, fi.i, fi.t.sign, fi.t.exponent, (int)fi.t.exponent - (int)127, fi.t.mantissa ); /* // We don't need bitfields printf("%12g %08x sign: %hhu exponent: %hhu exp-127: %4d mantissa: 0x%06x\n", f, fi.i, (fi.i >> 31), ((fi.i << 1) >> 24), (int)((fi.i << 1) >> 24) - (int)127, (fi.i & 0x007FFFFF) ); */ } puts(""); }
We're printing floats! sizeof(floatint) == 4 sizeof(float_t) == 4 0 0x00000000 0b00000000000000000000000000000000 0 0x00000000 sign: 0 exponent: 0 exp-127: -127 mantissa: 0x000000 -0 0x80000000 0b10000000000000000000000000000000 -0 0x80000000 sign: 1 exponent: 0 exp-127: -127 mantissa: 0x000000 inf 0x7f800000 0b01111111100000000000000000000000 inf 0x7f800000 sign: 0 exponent: 255 exp-127: 128 mantissa: 0x000000 -inf 0xff800000 0b11111111100000000000000000000000 -inf 0xff800000 sign: 1 exponent: 255 exp-127: 128 mantissa: 0x000000 nan 0x7fc00000 0b01111111110000000000000000000000 nan 0x7fc00000 sign: 0 exponent: 255 exp-127: 128 mantissa: 0x400000 1e-05 0x3727c5ac 0b00110111001001111100010110101100 1e-05 0x3727c5ac sign: 0 exponent: 110 exp-127: -17 mantissa: 0x27c5ac 0.0001 0x38d1b717 0b00111000110100011011011100010111 0.0001 0x38d1b717 sign: 0 exponent: 113 exp-127: -14 mantissa: 0x51b717 0.001 0x3a83126f 0b00111010100000110001001001101111 0.001 0x3a83126f sign: 0 exponent: 117 exp-127: -10 mantissa: 0x03126f 0.01 0x3c23d70a 0b00111100001000111101011100001010 0.01 0x3c23d70a sign: 0 exponent: 120 exp-127: -7 mantissa: 0x23d70a 0.1 0x3dcccccd 0b00111101110011001100110011001101 0.1 0x3dcccccd sign: 0 exponent: 123 exp-127: -4 mantissa: 0x4ccccd 1 0x3f800000 0b00111111100000000000000000000000 1 0x3f800000 sign: 0 exponent: 127 exp-127: 0 mantissa: 0x000000 0.015625 0x3c800000 0b00111100100000000000000000000000 0.015625 0x3c800000 sign: 0 exponent: 121 exp-127: -6 mantissa: 0x000000 0.03125 0x3d000000 0b00111101000000000000000000000000 0.03125 0x3d000000 sign: 0 exponent: 122 exp-127: -5 mantissa: 0x000000 0.0625 0x3d800000 0b00111101100000000000000000000000 0.0625 0x3d800000 sign: 0 exponent: 123 exp-127: -4 mantissa: 0x000000 0.125 0x3e000000 0b00111110000000000000000000000000 0.125 0x3e000000 sign: 0 exponent: 124 exp-127: -3 mantissa: 0x000000 0.25 0x3e800000 0b00111110100000000000000000000000 0.25 0x3e800000 sign: 0 exponent: 125 exp-127: -2 mantissa: 0x000000 0.5 0x3f000000 0b00111111000000000000000000000000 0.5 0x3f000000 sign: 0 exponent: 126 exp-127: -1 mantissa: 0x000000 1 0x3f800000 0b00111111100000000000000000000000 1 0x3f800000 sign: 0 exponent: 127 exp-127: 0 mantissa: 0x000000 2 0x40000000 0b01000000000000000000000000000000 2 0x40000000 sign: 0 exponent: 128 exp-127: 1 mantissa: 0x000000 4 0x40800000 0b01000000100000000000000000000000 4 0x40800000 sign: 0 exponent: 129 exp-127: 2 mantissa: 0x000000 128 0x43000000 0b01000011000000000000000000000000 128 0x43000000 sign: 0 exponent: 134 exp-127: 7 mantissa: 0x000000 65536 0x47800000 0b01000111100000000000000000000000 65536 0x47800000 sign: 0 exponent: 143 exp-127: 16 mantissa: 0x000000 0.15625 0x3e200000 0b00111110001000000000000000000000 0.15625 0x3e200000 sign: 0 exponent: 124 exp-127: -3 mantissa: 0x200000 -0.15625 0xbe200000 0b10111110001000000000000000000000 -0.15625 0xbe200000 sign: 1 exponent: 124 exp-127: -3 mantissa: 0x200000 3.68935e+19 0x60000000 0b01100000000000000000000000000000 3.68935e+19 0x60000000 sign: 0 exponent: 192 exp-127: 65 mantissa: 0x000000 -3.68935e+19 0xe0000000 0b11100000000000000000000000000000 -3.68935e+19 0xe0000000 sign: 1 exponent: 192 exp-127: 65 mantissa: 0x000000
1.7.4 How do I avoid typepunning?
The C99 is OK with union type punning :-/
The "blessed" way that is C11 compatible is memcpy.
Bitfields are generally considered "CURSED" due to platform specific behaviour.


Since the beginning of Mailee.me we didn’t care too much about the logs. Like everything is being logged but rarely we read it. The only case we really check it was doing a deep investigation about a bug or something like it.
Ok, I decided I should blog about the interesting things I am doing at my work. So let me introduce myself.


About
Taylor is a lightweight HTML editor ( < 1000 lines) written in Coffeescript and compiled into Javascript for
use on the web. It's open-source, MIT-licensed (do whatever the hell you want with it), but please consider
contributing to the source code to help better it. The goal is to eventually eliminate all dependencies to
make Taylor a standalone text-editor, use primarily targeted towards developers' blogs, hyper-text comment
systems, and such.
The source code can be found at http://github.com/ishmaelthedestroyer/taylor
The demo can be found at http://taylor-editor.herokuapp.com
The beef of the source code (Coffeescript) can be found at https://github.com/ishmaelthedestroyer/taylor/blob/master/js/taylor.coffee
The compiled source code (Javascript) can be found at https://github.com/ishmaelthedestroyer/taylor/blob/master/dist/js/taylor.js
TODO
Documentation in the works.
Methods for importing + exporting text as well as utility functions for basic statistics.
Sticky utility bar that sticks to the top after scrolling past.
Dependencies
Taylor requires the Bootstrap 3.0 and Font Awesome CSS + fonts, as well as the included CSS and Javascript files. Taylor does NOT depend on jQuery. Add the dependencies in the head of your document like so:
<link media='screen' rel='stylesheet' href='/path/to/bootstrap.css' /> <link media='screen' rel='stylesheet' href='/path/to/font-awesome.css' /> <link media='screen' rel='stylesheet' href='/path/to/taylor.css' />
Markup

Javascript (Initialize)

Properties
| Attribute | Description | Default |
|---|---|---|
| Width | Width of the editor (takes percentage or pixels). | 100% |
| Height | Height of the editor (percentage or pixels). | 500px |
| Buttons | An array of buttons to include in the utility bar. | Documentation in the works. |
Example
This is some quoted text with a header.
- ishmaelthedestroyer
This is an example of a hyperlink to http://google.com.
Here are examples of bolded, italicized, underlined, and striked
text. You can of course combine them in
any manner.
Here's some code, surrounded by the pre element and styled with bold and italic tags.
((w) -> Destroyer = () -> if !(@ instanceof Destroyer) return new Destroyer() @_destroy = [ 'cars', 'buildings', 'trees' ] @_spare = [] Destroyer::destroy = (obj) -> obj = null if obj in @_destroy obj = null if !(obj in @_destroy) # destroy anyways console.log 'Destroyer destroyed obj: ' + obj + '.' Destroyer::spare = (obj) -> @destroy obj return 'Requested denied. Object destroyed LOL' w.Ishmael = new Destroyer() # get it? ) window Ishmael.destroy o for o in [ 'Le Internetz', 'Los Angeles', 'Everything else.' ]


Image in Post
Aug 05, 2013
Once when I was six years old I saw a magniﬁcent picture in a book, called True Stories from Nature, about the primeval forest.
It was a picture of a boa constrictor in the act of swallowing an animal. Here is a copy of the drawing:
In the book it said: “Boa constrictors swallow their prey whole, without chewing it. After that they are not able to move, and they sleep through the six months that they need for digestion.”
I pondered deeply, then, over the adventures of the jungle. And after some work with a colored pencil I succeeded in making my ﬁrst drawing. My Drawing Number One. It looked like this:
I showed my masterpiece to the grown-ups, and asked them whether the drawing frightened them.
But they answered: “Frighten? Why should any one be frightened by a hat?”
My drawing was not a picture of a hat. It was a picture of a boa constrictor digesting an elephant. But since the grown-ups were not able to understand it, I made another drawing: I drew the inside of the boa constrictor, so that the grown-ups could see it clearly. They always need to have things explained.


|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
| Interface Summary | |
|---|---|
| AccountAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| AccountClient | Provides synchronous access to CloudStack Account features. |
| AddressAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| AddressClient | Provides synchronous access to CloudStack IPAddress features. |
| AsyncJobAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| AsyncJobClient | Provides synchronous access to CloudStack asyncJob features. |
| ConfigurationAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| ConfigurationClient | Provides synchronous access to CloudStack Configuration features. |
| DomainAccountAsyncClient | Provides asynchronous access to CloudStack Account features available to Domain Admin users. |
| DomainAccountClient | Provides synchronous access to CloudStack Account features available to Domain Admin users. |
| DomainDomainAsyncClient | Provides asynchronous access to CloudStack Domain features available to Domain Admin users. |
| DomainDomainClient | Provides synchronous access to CloudStack Domain features available to Domain Admin users. |
| DomainLimitAsyncClient | Provides asynchronous access to CloudStack Limit features available to Domain Admin users. |
| DomainLimitClient | Provides synchronous access to CloudStack Limit features available to Domain Admin users. |
| DomainUserAsyncClient | Provides asynchronous access to CloudStack User features available to Domain Admin users. |
| DomainUserClient | Provides synchronous access to CloudStack User features available to Domain Admin users. |
| EventAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| EventClient | Provides synchronous access to CloudStack Event features. |
| FirewallAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| FirewallClient | Provides synchronous access to CloudStack PortForwardingRule features. |
| GlobalAccountAsyncClient | Provides asynchronous access to CloudStack Account features available to Global Admin users. |
| GlobalAccountClient | Provides synchronous access to CloudStack Account features available to Global Admin users. |
| GlobalAlertAsyncClient | Provides asynchronous access to CloudStack Account features available to Global Admin users. |
| GlobalAlertClient | Provides synchronous access to CloudStack Alerts features available to Global Admin users. |
| GlobalCapacityAsyncClient | Provides asynchronous access to CloudStack Account features available to Global Admin users. |
| GlobalCapacityClient | Provides synchronous access to CloudStack Capacities features available to Global Admin users. |
| GlobalConfigurationAsyncClient | Provides asynchronous access to CloudStack Configuration features available to Global Admin users. |
| GlobalConfigurationClient | Provides synchronous access to CloudStack Configuration features available to Global Admin users. |
| GlobalDomainAsyncClient | Provides asynchronous access to CloudStack Domain features available to Global Admin users. |
| GlobalDomainClient | Provides synchronous access to CloudStack Domain features available to Global Admin users. |
| GlobalHostAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| GlobalHostClient | Provides synchronous access to CloudStack host features. |
| GlobalOfferingAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| GlobalOfferingClient | Provides synchronous access to CloudStack zone features. |
| GlobalPodAsyncClient | Provides asynchronous access to CloudStack Pod features available to Global Admin users. |
| GlobalPodClient | Provides synchronous access to CloudStack Pod features available to Global Admin users. |
| GlobalStoragePoolAsyncClient | Provides asynchronous access to CloudStack storage pool features. |
| GlobalStoragePoolClient | Provides synchronous access to CloudStack storage pool features. |
| GlobalUsageAsyncClient | Provides asynchronous access to CloudStack usage features. |
| GlobalUsageClient | Provides synchronous access to CloudStack usage features. |
| GlobalUserAsyncClient | Provides asynchronous access to CloudStack User features available to Global Admin users. |
| GlobalUserClient | Provides synchronous access to CloudStack User features available to Global Admin users. |
| GlobalVlanAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| GlobalVlanClient | Provides synchronous access to CloudStack VLAN features. |
| GlobalZoneAsyncClient | Provides asynchronous access to CloudStack Account features available to Global Admin users. |
| GlobalZoneClient | Provides synchronous access to CloudStack Zone features available to Global Admin users. |
| GuestOSAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| GuestOSClient | Provides synchronous access to CloudStack Operating System features. |
| HypervisorAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| HypervisorClient | Provides synchronous access to CloudStack Operating System features. |
| ISOAsyncClient | |
| ISOClient | |
| LimitAsyncClient | Provides asynchronous access to CloudStack SSHKeyPair features. |
| LimitClient | Provides synchronous access to CloudStack resource limit API. |
| LoadBalancerAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| LoadBalancerClient | Provides synchronous access to CloudStack LoadBalancer features. |
| NATAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| NATClient | Provides synchronous access to CloudStack IPForwardingRule features. |
| NetworkAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| NetworkClient | Provides synchronous access to CloudStack network features. |
| OfferingAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| OfferingClient | Provides synchronous access to CloudStack zone features. |
| SecurityGroupAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| SecurityGroupClient | Provides synchronous access to CloudStack security group features. |
| SessionAsyncClient | Provides asynchronous access to Cloudstack Sessions |
| SessionClient | Provides synchronous access to CloudStack Sessions |
| SnapshotAsyncClient | Provides synchronous access to CloudStack Snapshot features. |
| SnapshotClient | Provides synchronous access to CloudStack Snapshot features. |
| SSHKeyPairAsyncClient | Provides asynchronous access to CloudStack SSHKeyPair features. |
| SSHKeyPairClient | Provides synchronous access to CloudStack SSHKeyPair features. |
| TemplateAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| TemplateClient | Provides synchronous access to CloudStack template features. |
| VirtualMachineAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| VirtualMachineClient | Provides synchronous access to CloudStack VirtualMachine features. |
| VMGroupAsyncClient | Provides asynchronous access to CloudStack VM group features. |
| VMGroupClient | Provides synchronous access to CloudStack VM group features. |
| VolumeAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| VolumeClient | Provides synchronous access to CloudStack Event features. |
| ZoneAsyncClient | Provides asynchronous access to cloudstack via their REST API. |
| ZoneClient | Provides synchronous access to CloudStack zone features. |
|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |


|
|
| SpVector (T *v, INTM *r, INTM L, INTM nzmax) |
| | Constructor, of the sparse vector of size L.
|
| |
|
| SpVector (INTM nzmax) |
| | Constructor, allocates nzmax slots.
|
| |
|
| SpVector () |
| | Empty constructor.
|
| |
|
| ~SpVector () |
| | Destructor.
|
| |
| T | nzmax () const |
| |
|
T | length () const |
| | returns the length of the vector
|
| |
|
T | asum () const |
| | computes the sum of the magnitude of the elements
|
| |
|
T | nrm2sq () const |
| | computes the l2 norm ^2 of the vector
|
| |
|
T | nrm2 () const |
| | computes the l2 norm of the vector
|
| |
| T | fmaxval () const |
| | computes the linf norm of the vector More...
|
| |
|
void | print (const string &name) const |
| | print the vector to std::cerr
|
| |
|
void | refIndices (Vector< INTM > &indices) const |
| | create a reference on the vector r
|
| |
|
void | refVal (Vector< T > &val) const |
| | creates a reference on the vector val
|
| |
|
INTM | r (const INTM i) const |
| | access table r
|
| |
|
T | v (const INTM i) const |
| | access table r
|
| |
|
T * | rawX () const |
| |
|
INTM * | rawR () const |
| |
|
INTM | L () const |
| |
|
void | setL (const INTM L) |
| |
|
void | sqr () |
| | a <- a.^2
|
| |
|
T | dot (const SpVector< T > &vec) const |
| | dot product
|
| |
|
T | dot (const Vector< T > &vec) const |
| | dot product
|
| |
|
void | scal (const T a) |
| | dot product
|
| |
| void | clear () |
| | clears the vector More...
|
| |
|
void | resize (const INTM nzmax) |
| | resizes the vector
|
| |
|
void | toSpMatrix (SpMatrix< T > &out, const INTM m, const INTM n) const |
| | resize the vector as a sparse matrix
|
| |
|
void | toFull (Vector< T > &out) const |
| | resize the vector as a sparse matrix
|
| |
|
| SpVector (T *v, INTM *r, INTM L, INTM nzmax) |
| | Constructor, of the sparse vector of size L.
|
| |
|
| SpVector (INTM nzmax) |
| | Constructor, allocates nzmax slots.
|
| |
|
| SpVector () |
| | Empty constructor.
|
| |
|
| ~SpVector () |
| | Destructor.
|
| |
| T | nzmax () const |
| |
|
T | length () const |
| | returns the length of the vector
|
| |
|
T | asum () const |
| | computes the sum of the magnitude of the elements
|
| |
|
T | nrm2sq () const |
| | computes the l2 norm ^2 of the vector
|
| |
|
T | nrm2 () const |
| | computes the l2 norm of the vector
|
| |
|
T | fmaxval () const |
| | computes the linf norm of the vector
|
| |
|
void | print (const string &name) const |
| | print the vector to std::cerr
|
| |
|
void | refIndices (Vector< INTM > &indices) const |
| | create a reference on the vector r
|
| |
|
void | refVal (Vector< T > &val) const |
| | creates a reference on the vector val
|
| |
|
INTM | r (const INTM i) const |
| | access table r
|
| |
|
T | v (const INTM i) const |
| | access table r
|
| |
|
T * | rawX () const |
| |
|
INTM * | rawR () const |
| |
|
INTM | L () const |
| |
|
void | setL (const INTM L) |
| |
|
void | sqr () |
| | a <- a.^2
|
| |
|
T | dot (const SpVector< T > &vec) const |
| | dot product
|
| |
|
T | dot (const Vector< T > &vec) const |
| | dot product
|
| |
|
void | scal (const T a) |
| | dot product
|
| |
| void | clear () |
| |
|
void | resize (const INTM nzmax) |
| | resizes the vector
|
| |
|
void | toSpMatrix (SpMatrix< T > &out, const INTM m, const INTM n) const |
| | resize the vector as a sparse matrix
|
| |
|
void | toFull (Vector< T > &out) const |
| | resize the vector as a sparse matrix
|
| |
template<typename T>
class SpVector< T >
Sparse Vector class.
Sparse vector class.
Definition at line 61 of file linalg.h.


Setup Account on Prometheus
Invalid username or password
Username
Password
Repeat Password
Setup Account
Reset
Login


Forgiveness has always been the one virtue I was never able to accept - it almost offended me.
It never made sense why a loving God would enable horrors, degeneracy, deceit, and later compel a victim to bow down to the perpetrators.
This always infuriated me, still does, but the sparkles and glaze of life are starting to show me what's behind this formidable idea.
Today, I remembered the individuals that harmed and humiliated me the most, and I wasn't sure whether I wanted to destroy or thank them.
Although they caused me extreme pain and tears in the past, this blatantly led me towards the golden rivers I swim at the moment.
Surely, I will always be bitter and spit on them, but...
Perhaps, toxic classmates were essential to propel me towards the becoming of myself.
Perhaps, oppressive schoolgirls kept me away from ephemeral, plastic and degenerate relationships.
Perhaps, stingy and unethical co-founders were the best remedy to never become like themselves.

Perhaps, these tragedies were the only way to map a distinctive road, to get to the higher mountains.
Perhaps, forgiveness is not about forgiving a person, but God himself.


Provides all the definitions to access persistence storage for personal portal objects. This package provides also an implementation of all definied interfaces using MySQL as persistent storage.


Syrian Army captures town from Turkish-backed militants near Tal Tamr: video
by News DeskBEIRUT, LEBANON (12:00 P.M.) – The Syrian Arab Army (SAA) recently captured the town of Umm Shaefa in the Tal Tamr District from the Turkish-backed militants.
In a video released on Thursday, the Syrian Arab Army can be seen taking over the town after clashing with the Turkish-backed militants in the northwestern countryside of the Al-Hasakah Governorate.


Toggle navigation
Library Reports
Home
Table of Contents
Help
Contact
Print this page
Ipad Usage
Laptop Usage
Yearly Usage
Monthly Usage
Month
Year
HeadphoneVgaetc
Laptopusage
iPadusage


#assign n0_docs="true"> <#assign n1_query_find="true"> <#assign findCount= "true">
Execute a count query.
```java int count = new QCustomer() .name.icontains("foo.bar") .findCount(); ```
Ebean will modify the query select clause
as necessary executing a select count(*)
type
query.
When the predicates include a many path
(in the example below "contacts" is a @OneToMany
)
then Ebean will use a subquery with count distinct.
The above query includes a many path
of contacts so the SQL becomes:


Application Number:
Please enter your personal details in order to finally confirm your credit application
First Name
Last Name
Street
Postcode
City
Proceed