当前位置:网站首页>Permission management with binary
Permission management with binary
2022-04-23 18:08:00 【dawnsun001】
Just give an example ! Examples will be explained 1. Permission means 2. Authority judgment 3. Add permissions 4. Cancellation of authority
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- /**
- * Four permissions , Currently defined as int, The following binary representation takes only the last four digits for explanation
- */
- // add to
- int c = 1;// ...0001
- // Inquire about
- int r = 2;// ...0010
- // modify
- int u = 4;// ...0100
- // Delete
- int d = 8;// ...1000
- /**
- *
- * You can observe the rules of binary representation of four permissions , All are 2 Of N Power ,
- * It means itself , The last bit of add permission is... Others 0, The penultimate digit of the query is 1 Everything else is 0, Change the penultimate to 1 Everything else is 0, Delete the penultimate as 1 Everything else is 0
- *
- */
- /**
- *1111---- This indicates what kind of permission you have. You can use |( Press bit or ) operation
- *
- */
- // user A Have the right to add and modify
- int usera = c | r | u;
- // user B Have the right to add and delete
- int userb = c | d;
- /**
- * 2222---- To judge whether a user has certain permissions, use the user permissions and the permissions to be judged &( Bitwise AND ) operation , When the result is the permission value to be judged, it means that the user has this permission , Otherwise, you don't have this permission
- */
- if ((usera & u) == u) {
- System.out.println(" user a Have update permission ");
- } else {
- System.out.println(" user a No update permission ");
- }
- /**
- * 3333---- Add permissions to users. Use user permissions and permissions to be added |( Press bit or ) The operation overwrites the previous permission value
- */
- if ((userb & u) == u) {
- System.out.println(" user b Have update permission ");
- } else {
- System.out.println(" user b No update permission ");
- }
- // To the user b Add update permissions
- userb = userb | u;
- if ((userb & u) == u) {
- System.out.println(" user b Have update permission ");
- } else {
- System.out.println(" user b No update permission ");
- }
- /**
- * 4444---- Cancel a user's permission , Use the user authority and the authority to be cancelled to reverse by bit, and then carry out by bit operation , Overwrite the previous permission value
- */
- if ((usera & r) == r) {
- System.out.println(" user a Have query authority ");
- } else {
- System.out.println(" user a No query permission ");
- }
- // Cancel user a The query authority of
- usera = usera & (~r);
- if ((usera & r) == r) {
- System.out.println(" user a Have query authority ");
- } else {
- System.out.println(" user a No query permission ");
- }
- }
- }
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289337.html
边栏推荐
猜你喜欢
【ACM】509. 斐波那契数(dp五部曲)
Process management command
Map basemap Library
Jenkspy package installation
Fashion classification case based on keras
Go's gin framework learning
String function in MySQL
Batch export ArcGIS attribute table
Dock installation redis
Implementation of k8s redis one master multi slave dynamic capacity expansion
随机推荐
MySQL 中的字符串函数
C# 网络相关操作
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
Data stream encryption and decryption of C
线上怎么确定期货账户安全的?
Notes on common basic usage of eigen Library
【ACM】70. 爬楼梯
深度学习经典网络解析目标检测篇(一):R-CNN
String function in MySQL
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
Excel opens large CSV format data
Generate verification code
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
I / O multiplexing and its related details
How to ensure the security of futures accounts online?
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Climbing watermelon video URL
Transfer learning of five categories of pictures based on VGg
Serialization scheme of serde - trust
An example of linear regression based on tensorflow