Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Bitwise Operators
Go Bitwise Operators
Bitwise operators का उपयोग numbers के binary representation पर किया जाता है।
Example: AND (&)
package main
import "fmt"
func main() {
var x = 5 // 0101
var y = 3 // 0011
fmt.Println(x & y) // दोनों bits 1 होने पर 1, result: 1 (0001)
}
1
Example: OR (|)
package main
import "fmt"
func main() {
var x = 5 // 0101
var y = 3 // 0011
fmt.Println(x | y) // किसी एक bit 1 होने पर 1, result: 7 (0111)
}
7
Example: XOR (^)
package main
import "fmt"
func main() {
var x = 5 // 0101
var y = 3 // 0011
fmt.Println(x ^ y) // सिर्फ एक bit 1 होने पर 1, result: 6 (0110)
}
6
Example: Left Shift (<<)
package main
import "fmt"
func main() {
var x = 5 // 0101
fmt.Println(x << 2) // बायाँ shift 2 positions, result: 20 (10100)
}
20
Example: Right Shift (>>)
package main
import "fmt"
func main() {
var x = 20 // 10100
fmt.Println(x >> 2) // दाएँ shift 2 positions, result: 5 (0101)
}
5
All Bitwise Operators
| Operator | Name | Description | Example |
|---|---|---|---|
| & | AND | दोनों bits 1 होने पर 1 | x & y |
| | | OR | किसी एक bit 1 होने पर 1 | x | y |
| ^ | XOR | सिर्फ एक bit 1 होने पर 1 | x ^ y |
| << | Left Shift | बायाँ shift करके right में zeros डालता है | x << 2 |
| >> | Right Shift | दायाँ shift करके leftmost bit की copy डालता है | x >> 2 |
1. Bitwise operators केवल integer numbers पर ही उपयोग करें।
2. Left Shift (<<) और Right Shift (>>) binary representation पर काम करते हैं।
3. XOR (^) operator केवल तब 1 देता है जब दो bits अलग हों।
4. Result का binary समझना महत्वपूर्ण है, नहीं तो गलत output मिल सकता है।
5. Negative numbers के लिए right shift sign bit copy करता है।