Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java Bitmask Control permissions and bit operators

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about Java bitmask control permissions and bit operators for example analysis of the content shared. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

1. java bit mask

Java bitmask, in java development there are few scenarios will use mask, but when the system needs to determine whether an object has certain permissions, you can do it through bitmask.

Bit masks are mainly made by bit operations such as AND (&), NOT (~), OR (|), XOR (^), shift (), etc. to realize the permission judgment function.

1.1 A brief introduction to bit operators (calculations are binary)

: right shift operator, num >> 1, equivalent to num divided by 2

>>> : unsigned right shift, ignoring sign bits, empty spaces are filled with 0

XOR (^ ): one sentence, dissimilarity is true returns boolean

AND (&): True only if both digits are 1, 0001 & 0101 is 0001

or (|): If one of the two bits is true, 0001| 0100, which is 0101.

Not (~): negates itself.

In Java, all data representation methods are expressed in the form of complements. If there is no special explanation, the default data type in Java is int. The length of the int data type is 8 bits, one bit is four bytes, that is, 32 bytes, 32bit.

For example: ~37

37 to binary is 100101

000000 000000 0000000 0000000 00100101

1111111111111111111111111111111110111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111011111111111111111111111111111111011111111111111111

Because the high bit is 1, the original code is negative, and the complement of the negative number is the inverse of the original code of its absolute value, plus 1 at the end.

Therefore, we can restore the complement of this binary number: First, subtract 1 from the end to get the complement: 111111111111111111111011001 Second, invert each bit to get the original code:

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

So ~37 = -38.

2. bitmask control permission

Suppose that in a system, users have four kinds of permissions: Select, Insert, Update, and Select. Mask is used to control and judge these permissions.

Code implementation:

package com.us.basics;/** * Created by yangyibo on 17/12/11. * Using bitmasks, all 16 permission states can be represented by an integer greater than or equal to 0 and less than 16. */public class BitMask { public static int ADD = 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report