」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 位元運算概述

位元運算概述

發佈於2024-08-02
瀏覽:686

An Overview of Bitwise Operations

The following post was taken from a repository I created to help learn (and teach) about bitwise operations. You can find that repo here, and I'd suggest checking it out—there's some code examples and solutions there.

Introduction

The goal of this repository is to acquaint you with bitwise operations, explaining what they are, how they work, and what they can be used for.

Chapter 1: It's All Binary

In C (and most high-level languages), our variables have types. These types are indicative of a few things. Of course, a variable of type int will store an integer value, but the key to understanding these bitwise operations is to know that under the hood, all types are stored in memory (anywhere, stack, heap, wherever) as binary. Here's an example of what happens when you store a simple integer value on the stack in C:

int main(int argc, char** argv) {
    int x = 2;
    return 0;
}

After compiling to assembly, the code might look like this (I'm using ARM assembly here, and I've annotated the code using comments):

.section .text
.global main

main:
    ; Set up the stack for the function
    stp x29, x30 [sp, -16]! ; Save previous frame pointer & link register
    mov x29, sp ; Setup a new frame pointer

    ; Initialize x with 2
    ; IN C: int x = 2;
    mov w0, 2 ; Move 2 into the w0 register
    str w0, [sp, 16] ; Store the contents of w0 (2) at a 16-byte offset from the stack pointer
    ; Essentially, the above line stores 2 on the stack.

    mov w0, 0 ; Move 0 into w0, prepare for return

    ; Clear stack
    ldp x29, x30, [sp], 32 ; Restore frame pointer and link register
    ret ; Return

Note that most compilers would not actually store a variable like the one I showed on the stack, as it is unused. However, if it is used multiple times, it would be stored on the stack something like the above.

If we looked at the location where our variable was stored on the stack (while it is there, of course), we would see something like:

Memory Address Value Stored (Hex) Value Stored (Binary)
0x1000 0x02 00000010
0x1001 0x00 00000000
0x1002 0x00 00000000
0x1003 0x00 00000000

This assumes that your system is little-endian. I won't go into endianness here, but you can read more about it here.

The key thing I'd like you to notice about the table above is that even though our integer is only 2 bits long, it takes up 4 bytes (32 bits) of memory. Now, don't freak out—this is normal and expected. One of the many things that C and your compiler do is set standards for the types you invoke. So when I create an int variable, the compiler knows to allocate 4 bytes (again, 32 bits) of memory. We can even test this using the sizeof() operator in C.

The sizeof() Operator

sizeof() is not an actual C function. Instead, at compile time, the compiler replaces the expression with the size of the given data type. You can even use this with your own types, like typedefs and/or structs:

#include  

typedef struct {
  char name[64];
  int age;
} Person;

int main(int argc, char** argv) {
  printf("A Person is %lu bytes long.\n", sizeof(Person));
  return 0;
}

One other thing you might be asking is how negative numbers are stored. Excellent question. Numbers can be signed or unsigned, but by default, they're signed. If an integer is signed, it sacrifices its most significant bit to be the "sign bit." If the sign bit is 1, the number is negative; otherwise it's positive. An astute reader might realize that the change that happens here is in the range of possible numbers. Consider 8-bit numbers. There are 256 possible numbers to represent (given by 2^8). With an unsigned 8-bit integer, the values 0–255 can be represented; with a signed 8-bit int, -128–127 can be represented.

To get the inverse of a binary number, use two's compliment. Let's find -5 in binary.

  1. Start with 5. In binary, 5 is 0101. The leading 0 is the sign.
  2. Invert each bit. 0101 → 1010.
  3. Add 1 to this number (ignoring any possible overflow). 1010 0001 = 1011.

Your Turn!

  1. Confirm that -5 is 1011 in binary by performing two's compliment on it to get 5, or 0101.
  2. Write a C program that prints the size of an int in both bytes and bits. Use the code above as a starting point. Hint: to convert from bytes to bits, how many bits are in a byte?
  3. Fill in the following table with sizes of different types, modifying your program to check them.
Type Size (bytes) Size (bits)
int
int64_t
int8_t
char
bool (you'll need to #include )
long int
short int
long long int
double
double

Sample Responses

Question 1

  1. Start with -5: 1011.
  2. Invert each bit: 1011 → 0100.
  3. Add 1: 0100 0001 = 0101

Question 2

Here's an example of what your simple program might look like (you can also check it out at Chapter 1/SizeOfOperatorTest.c).

 #include 

 int main(int argc, char** argv) {
    printf("The size of an int is %lu bytes, or %lu bits.\n", sizeof(int), sizeof(int) * 8);
    return 0;
 }

Go ahead and compile it using gcc and check out its output:

cd Chapter\ 1
gcc -o sizeof SizeOfOperatorTest.c
./sizeof

Question 3

Type Size (bytes) Size (bits)
int 4 32
int64_t 8 64
int8_t 1 8
char 1 8
bool (you'll need to #include ) 1 8
long int 4 32
short int 2 16
long long int 8 64
double 4 32
double 8 64

Take This Home

The main point I'd like you to keep in mind is that with control of every bit, we can optimize our memory usage. Though that has little effect on modern systems, in the case of embedded computing, every byte counts. By manually reading and writing bits as opposed to typical variable values, we can harness more functionality from less storage.

Chapter 2: Operating on Bits

Now that we've covered data types and how data is stored, we're ready to introduce the idea of bitwise operations. Put simply, a bitwise operation is an operation done on each bit of a piece of data. Let's take a look at each bitwise operator. Then, we'll implement them in C.

And (&)

Written x & y in C. This operator takes the corresponding bits of each number and performs an and operation. An and operation returns 1 (true) if and only if both bits are 1. This means that two bits that are both 0 do not return 1—they return 0. The result is the number made up of the binary string of results from each and. It's easiest to see this in a truth table.

Consider the operation int result = 3 & 5. First, convert 3 and 5 to binary.
Now, we have int result = 011 & 101. Consider the following truth table:

Bit A Bit B AND
0 1 0
1 0 0
1 1 1

The result of the and operation is 001, which when converted to decimal is 1.

Or (|)

Written x | y in C. This operator takes the corresponding bits of each number and performs an or operation. An or operation returns 1 if either bit is 1. As with other bitwise operators, the result is the number made up of the binary string of results from each or.

Consider the operation int result = 3 | 5. First, convert 3 and 5 to binary.
Now, we have int result = 011 | 101. Consider the following truth table:

Bit A Bit B OR
0 1 1
1 0 1
1 1 1

The result of the or operation is 111, which when converted to decimal is 7.

Xor (^)

Written x ^ y in C. This operator takes the corresponding bits of each number and performs an xor (exclusive or) operation. An xor operation returns 1 if and only if one of the two bits is 1. As with other bitwise operators, the result is the number made up of the binary string of results from each or.

Consider the operation int result = 3 ^ 5. First, convert 3 and 5 to binary.
Now, we have int result = 011 ^ 101. Consider the following truth table:

Bit A Bit B XOR
0 1 1
1 0 1
1 1 0

The result of the xor operation is 110, which when converted to decimal is 6.

Left Shift (

Written x

Consider int result = 5

Initial

1 0 1

After one shift

0 1 0

Result

1 0 0

The binary result is 100, which is 4 in decimal.

Right Shift (>>)

Written x >> amount This operator is just like the left shift, except it works in the opposite direction. Each bit in the given number is shifted to the right by the given amount. Any bits that reach the end of the number are truncated (and zeros appear on the left side). Let's walk through an example.

Consider int result = 3 >> 2. As you know, 3 is 011 in binary. Let's walk through each iteration of the shift.

Initial

0 1 1

After one shift

0 0 1

Result

0 0 0

The binary result is 000, which is 0 in decimal.

Not (~)

Written ~x. The not operator inverts all the bits of the given number. Once again, we'll use a truth table to elaborate.

Consider int result = ~5. As you know, 5 is 101 in binary.

Bit A ~ Bit A
1 0
0 1
1 0

Hence, the result of the not operation is 010, which is 2 in binary.

Left Shift & Right Shift Restrictions

There are a few notable restrictions placed on these shift operations. For starters, you cannot shift bits a negative number of times—that just doesn't make sense! Also, shifting for more than the number of bits allocated to your variable is considered undefined. You can do it, but its output is not guaranteed to be constant for a given value. Finally, although not a restriction per-say, shifting 0 times simply doesn't perform a shift.

Your Turn!

  1. Complete a truth table for each of the following. Consider all values to be unsigned. Convert to decimal when complete.
  2. 8 & 2
  3. 6 | 3
  4. 7 ^ 5
  5. (5 & 2) & (4 & 3)
  6. (5 | 2) & (4 & 3)
  7. (5 & 2) ^ (4 | 3)

  8. Complete each operation. Consider all values to be unsigned and as long as the longest value in the problem needs to be (i.e., if you have the largest value of 8, deal with 4 bits). Convert to decimal when complete.

  9. ~6

  10. 9

  11. ~(7 & 8)

  12. (2 | 6) >> 1

  13. 8 >> (~2)

  14. ~((3 >> 2) ^ ~7) & (~(7 >> 4) | 2)

Sample Responses

Question 1

  • 8 & 2 → 1000 & 0010
Bit A Bit B AND
1 0 0
0 0 0
0 1 0
0 0 0

⇒ 0000, which is 0 in decimal.

  • 6 | 3 → 110 | 011
Bit A Bit B OR
1 0 1
1 1 1
0 1 1

⇒ 111, which is 7 in decimal.

  • 7 ^ 5 → 111 ^ 101
Bit A Bit B XOR
1 1 0
1 0 1
1 1 0

⇒ 010, which is 2 in decimal.

  • (5 & 2) & (4 & 3) → (101 & 010) & (100 & 011)
Bit A Bit B A AND B Bit C Bit D C AND D (A AND B) AND (C AND D)
1 0 0 1 0 0 0
0 1 0 0 1 0 0
1 0 0 0 1 0 0

⇒ 000, which is 0 in decimal.

  • (5 | 2) & (4 & 3) → (101 | 010) & (100 & 011)
Bit A Bit B A OR B Bit C Bit D C AND D (A OR B) AND (C AND D)
1 0 1 1 0 0 0
0 1 1 0 1 0 0
1 0 1 0 1 0 0

⇒ 000, which is 0 in decimal.

  • (5 & 2) ^ (4 | 3) → (101 & 010) ^ (100 | 011)
Bit A Bit B A AND B Bit C Bit D C OR D (A AND B) XOR (C OR D)
1 0 0 1 0 1 1
0 1 0 0 1 1 1
1 0 0 0 1 1 1

⇒ 111, which is 7 in decimal.

Question 2

  • ~6 → ~110 ⇒ 011, which is 3 in decimal.

  • 9

  • ~(7 & 8) → ~(0111 & 1000) → ~(0000) ⇒ 1111, which is 15 in decimal.

  • (2 | 6) >> 1 → (010 | 110) >> 1 → 110 >> 1 ⇒ 011, which is 3 in decimal.

  • 8 >> (~2) → 1000 >> ~(10) → 1000 >> (01) → 1000 >> 1 ⇒ 0100, which is 4 in decimal.

  • ~((3 >> 2) ^ ~7) & (~(7 >> 4) | 2)

To solve this, I suggest splitting into halves:

~((3 >> 2) ^ ~7) and (~(7 >> 4) | 2)

~((3 >> 2) ^ ~7) → ~((011 >> 2) ^ ~(111)) → ~((000) ^ ~(111)) → ~(000 ^ 000) → 111
(~(7 >> 4) | 2) → (~(111 >> 4) | 010) → (~(000) | 010) → (111 | 010) → 111

Hence, 111 & 111 ⇒ 111, which is 7 in decimal.

Chapter 3: Applying Bitwise Operations in C

This chapter is all about writing C code that utilizes bitwise operators. Before we get to doing bitwise operations, we should begin by writing a function that can write the binary equivalent of a given variable.

To do this, we use a mask. We initialize it to contain a 1 in the most significant (leftmost in little-endian systems) bit followed by zeros. With each iteration of a loop, we right shift the mask by 1, moving the 1 all the way "across" the mask. When we use the & operator on the pointer and the mask, any non-zero value means that a 1 occurred somewhere in the result. Since there's only one 1 in the mask, we know exactly where this occurred. Since the loop moves from left to right, we can just append the corresponding bit's character to the string. The string is one character longer than the size because it needs to contain the null character (\0). This is how printf knows to stop printing, and omitting it can lead to numerous errors and/or unexpected behaviors (like the printing of other data from in memory).

void printBinary(unsigned int decimal) {

    // To determine size (in bits), we multiply the maximum size of an unsigned int by 8 (to convert from bytes to bits).
    int size = sizeof(decimal) * 8;

    // This counts the leading zeros of the number, then subtracts them from the size.
    // Hence, we start at the first bit set to 1 (unless decimal == 0)
    size -= __builtin_clz(decimal);

    if(size == 0) size = 1; // Handle zero case, we need one space to display "0."

    int curr = 0;
    char binary[size   1];
    for(unsigned int mask = 1 >= 1) {
        if((decimal & mask) != 0) {
            binary[curr] = '1';
        } else {
            binary[curr] = '0';
        }
        curr  ;
    }

    binary[curr] = '\0';

    printf("%s", binary);

}

Bitwise Assignment Operators

All bitwise operators, except for not (~), can be used in the assignment fashion. This means you can add an equals sign right next to one of the bitwise operator. For example, in

int a = 2;
a &= 7;

a is both the first operand and the destination. In other words, the value of a & 7 is stored in a. This works for all bitwise operators aside from the not (~) operator.

Now, I'd like to provide a few case study like prompts for you to try. Sample responses are available.

Case Study 1: Unix File Permissions

One use case of bitwise operations is in the Unix permission system. You've probably run the command

chmod 777 some-file

But what do each of the numbers mean? And why 7? The reality is, binary is at play here, and 7 should tip you off. Recall that 7 in binary is 111. The number being passed here is acting as three flags or booleans glued together.

The three digits specified are for three classes of users:

  • The file owner;
  • Group members of the file owner;
  • and everyone else.

As I mentioned above, each digit is a conglomeration of three flags, each representing a permission. The flag (binary bit) in the fours place represents read permission, the twos place is for write permission, and the ones is for execute. So,

chmod 777 some-file

is doing this under the hood:

File Permissions: some-file

Group Read Write Execute Decimal
Owner 1 1 1 7
Owner's Group 1 1 1 7
Everyone Else 1 1 1 7

In other words, all permissions are given to all.

Task

Design a simple permissions checker that takes in a full file permission value (a three-digit number) and checks for a given set of specific permissions (i.e., owner write, everyone execute, etc.). For an example, check the Chapter 3 folder.

Hint

After taking in a full number, you'll need to convert it to an int (from a char*). Then, use modular arithmetic to break down each permission set. Remember, the first digit represents the owner's permissions, the second is for the owner's user group, and the third is for everyone.

To check if a specific permission occurs in a permission set, bitwise and the given permission with the set.

Case 2: Subnetting a Network

If you've ever configured a router, you may have noticed an option to configure the "subnet mask." Usually, this is set to 255.255.255.0, but why? Firstly, we need to remember that each byte of an IPv4 address is separated by a '.'. When dealing with the type of network you're most familiar with (a class C network), there are 3 bytes dedicated to the network and the final byte is for the host.

Being that the subnet mask is a mask, you might be catching on to its purpose. For each bit you "borrow" from the host byte, two subnets are created.

Network Address

The network address has all host bits set to 0. This means any bit surrendered to create
a subnet still could be set to 1.

Read More!

Learn more about subnets by checking out this website.

Task

In C, write a program that takes in an IPv4 address and a subnet mask and finds and outputs the network address that the IPv4 address lives in. For an example, check the Chapter 3 folder.

Hint

You'll need to store each byte of the address and mask as a numerical value. To find the network address, consider which (bitwise) operation between the mask and address will create the intended effect.

Closing

I hope this explainer was useful for you! I wrote it because I wanted to learn about bitwise operations myself. I've checked it, but some things could be wrong, so feel free to correct me via a pull request, or even add a comment! Also, if you've got any questions, leave a comment! I can't wait to chat with you! To close, I'm so happy to have been able to provide this resource for you!

About Me

Hi! I'm Jackson, a student of computer science & French at Lafayette College and an aspiring researcher and professor in computer science. I'm currently interested in the fields of bioinformatics and low-level programming/systems. To learn more about me, check out my site.

版本聲明 本文轉載於:https://dev.to/jacksoneshbaugh/an-overview-of-bitwise-operations-2die如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • JavaScript 是同步還是異步,是單執行緒還是多執行緒? JavaScript程式碼是如何執行的?
    JavaScript 是同步還是異步,是單執行緒還是多執行緒? JavaScript程式碼是如何執行的?
    JavaScript 是一種同步、單執行緒語言,一次只能執行一個指令。僅噹噹前行執行完畢後,才會移至下一行。但是,JavaScript 可以使用事件循環、Promises、Async/Await 和回呼佇列執行非同步操作(JavaScript 預設是同步的)。 JavaScript程式碼是如何執行...
    程式設計 發佈於2024-11-06
  • 如何從 PHP 中的物件數組中提取一列屬性?
    如何從 PHP 中的物件數組中提取一列屬性?
    PHP:從物件數組中高效提取一列屬性許多程式設計場景都涉及使用物件數組,其中每個物件可能有多個屬性。有時,需要從每個物件中提取特定屬性以形成單獨的陣列。在 PHP 中,在不借助循環或外部函數的情況下用一行程式碼實現此目標可能很棘手。 一個可能的方法是利用 array_walk() 函數和 creat...
    程式設計 發佈於2024-11-06
  • 建構 PHP Web 專案的最佳實踐
    建構 PHP Web 專案的最佳實踐
    規劃新的 PHP Web 專案時,考慮技術和策略方面以確保成功非常重要。以下是一些規則來引導您完成整個過程: 1. 定義明確的目標和要求 為什麼重要:清楚了解專案目標有助於避免範圍蔓延並與利害關係人設定期望。 行動: 建立具有特定功能的專案大綱。 確定核心特徵和潛在的發展階段。 ...
    程式設計 發佈於2024-11-06
  • 如何在不使用巢狀查詢的情況下從 MySQL 中的查詢結果指派使用者變數?
    如何在不使用巢狀查詢的情況下從 MySQL 中的查詢結果指派使用者變數?
    MySQL 中根據查詢結果分配使用者變數背景和目標根據查詢結果分配使用者定義的變數可以增強資料庫操作能力。本文探討了在 MySQL 中實現此目的的方法,而無需借助嵌套查詢。 使用者變數賦值語法與流行的看法相反,使用者變數賦值可以直接整合到查詢中。 SET 語句的賦值運算子是= 或:=。但是,:= 必...
    程式設計 發佈於2024-11-06
  • 如何使用 array_column() 函數從 PHP 中的物件陣列中提取 Cat ID?
    如何使用 array_column() 函數從 PHP 中的物件陣列中提取 Cat ID?
    從PHP 中的物件陣列中提取貓ID處理物件陣列(例如貓物件陣列)時,提取特定屬性通常可以成為必要的任務。在這種特殊情況下,我們的目標是將每個 cat 物件的 id 屬性提取到一個新數組中。 正如您的問題中所建議的,一種方法涉及使用 array_walk() 和 create_function 。雖然...
    程式設計 發佈於2024-11-06
  • 實用指南 - 遷移到 Next.js App Router
    實用指南 - 遷移到 Next.js App Router
    隨著 Next.js App Router 的發布,許多開發者都渴望遷移他們現有的專案。在這篇文章中,我將分享我將專案遷移到 Next.js App Router 的經驗,包括主要挑戰、變更以及如何使流程更加順利。 這是一種增量方法,您可以同時使用頁面路由器和應用程式路由器。 為...
    程式設計 發佈於2024-11-06
  • 何時以及為何應調整 @Transactional 中的預設隔離和傳播參數?
    何時以及為何應調整 @Transactional 中的預設隔離和傳播參數?
    @Transactional中的隔離和傳播參數在Spring的@Transactional註解中,兩個關鍵參數定義了資料庫事務的行為:隔離和傳播。本文探討了何時以及為何應考慮調整其預設值。 傳播傳播定義了事務如何相互關聯。常見選項包括:REQUIRED: 在現有交易中執行程式碼,如果不存在則建立一個...
    程式設計 發佈於2024-11-06
  • OpenAPI 修剪器 Python 工具
    OpenAPI 修剪器 Python 工具
    使用 OpenAPI Trimmer 簡化您的 OpenAPI 文件 管理大型 OpenAPI 檔案可能會很麻煩,尤其是當您只需要一小部分 API 來執行特定任務時。這就是 OpenAPI Trimmer 派上用場的地方。它是一個輕量級工具,旨在精簡您的 OpenAPI 文件,使其...
    程式設計 發佈於2024-11-06
  • PHP:揭示動態網站背後的秘密
    PHP:揭示動態網站背後的秘密
    PHP(超文本預處理器)是一種伺服器端程式語言,廣泛用於建立動態和互動式網站。它以其簡單語法、動態內容生成能力、伺服器端處理和快速開發能力而著稱,並受到大多數網站託管服務商的支援。 PHP:揭秘動態網站背後的秘方PHP(超文本預處理器)是伺服器端程式語言,以其用於創建動態和互動式網站而聞名。它廣泛應...
    程式設計 發佈於2024-11-06
  • JavaScript 中的變數命名最佳實踐,實現簡潔、可維護的程式碼
    JavaScript 中的變數命名最佳實踐,實現簡潔、可維護的程式碼
    簡介:增強程式碼清晰度和維護 編寫乾淨、易於理解和可維護的程式碼對於任何 JavaScript 開發人員來說都是至關重要的。實現這一目標的一個關鍵方面是透過有效的變數命名。命名良好的變數不僅使您的程式碼更易於閱讀,而且更易於理解和維護。在本指南中,我們將探討如何選擇具有描述性且有意義的變數名稱,以顯...
    程式設計 發佈於2024-11-06
  • 揭示 Spring AOP 的內部運作原理
    揭示 Spring AOP 的內部運作原理
    在这篇文章中,我们将揭开 Spring 中面向方面编程(AOP)的内部机制的神秘面纱。重点将放在理解 AOP 如何实现日志记录等功能,这些功能通常被认为是一种“魔法”。通过浏览核心 Java 实现,我们将看到它是如何与 Java 的反射、代理模式和注释相关的,而不是任何真正神奇的东西。 ...
    程式設計 發佈於2024-11-06
  • JavaScript ESelease 筆記:釋放現代 JavaScript 的力量
    JavaScript ESelease 筆記:釋放現代 JavaScript 的力量
    JavaScript ES6,正式名稱為 ECMAScript 2015,引入了重大增強功能和新功能,改變了開發人員編寫 JavaScript 的方式。以下是定義 ES6 的前 20 個功能,它們使 JavaScript 程式設計變得更有效率和愉快。 JavaScript ES6 ...
    程式設計 發佈於2024-11-06
  • 了解 Javascript 中的 POST 請求
    了解 Javascript 中的 POST 請求
    function newPlayer(newForm) { fetch("http://localhost:3000/Players", { method: "POST", headers: { 'Content-Type': 'application...
    程式設計 發佈於2024-11-06
  • 如何使用 Savitzky-Golay 濾波平滑雜訊曲線?
    如何使用 Savitzky-Golay 濾波平滑雜訊曲線?
    雜訊資料的平滑曲線:探討Savitzky-Golay 濾波在分析資料集的過程中,平滑雜訊曲線的挑戰出現在提高清晰度並揭示潛在模式。對於此任務,特別有效的方法是 Savitzky-Golay 濾波器。 Savitzky-Golay 濾波器在資料可以透過多項式函數進行局部近似的假設下運作。它利用最小二乘...
    程式設計 發佈於2024-11-06
  • 重載可變參數方法
    重載可變參數方法
    重載可變參數方法 我們可以重載一個採用可變長度參數的方法。 此程式示範了兩種重載可變參數方法的方法: 1 各種可變參數類型:可以重載具有不同可變參數類型的方法,例如 vaTest(int...) 和 vaTest(boolean...)。 varargs 參數的類型決定了要呼叫哪個方法。 2 新...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3