8-26 钟诚 js基础
位运算
1.按位与 & | ^
我们所进行的数值计算,再计算中都是二进制去计算
计算 10&20
console.log(12&21)
console.log(12|21)
console.log(1221)
二进制:原码、反码、补码
12:正数的原码/反码/补码都是一样的
原码: 0000 1100
12: 0 1100
21: 1 0101,
& 0 0100 ==>4 按位与:对应两个位为1时,结果为1
| 1 1101 ==>29 按位或:对应两个位一个为1 结果便为1
^ 1 1001 ==>25 按位异或:对应两个位 相同为0 不同为1
20: 0000 1010
按位右移是在二进制末尾删除n位,在开头补0
console.log(20<<1) // 40
20: 0010 1000 ==>40
按位左移是在二进制开头删除n为,在末尾补0
console.log(-10>>2) // -3 ex:-10右移两位,-10/22=-2.5 取小 为-3
原码: 1 0000 1010,
反码: 1 1111 0101,
补码: 1 1111 0110,
移位: 1 1111 1101,
取反(反码): 1 0000 0010
原码(补码): 1 0000 0011 ,// -3
负数在按位右移的过程中,高位补1
按位左移
console.log(-10<<2) // -40 ex:-10左移两位 -10*22=-40
原码:1 0000 1010
反码:1 1111 0101
补码:1 1111 0110
左移:1 1101 1000
取反:1 0010 0111
原码(补码):1 0010 1000 // -40
短路与 (一假即假)
- 针对 && 顺口溜: 找第一个出现的假值. (一假即假)
- 针对 || 顺口溜: 找第一个出现的真值. (一真即真)
** 口诀 : 找第一个为假的值.
// 请问1 : 8 < 7 && 3 < 4, 结果为 ?
alert(8 < 7 && 3 < 4); // false
// 请问2 : -2 && 6 + 6 && null 结果为 ?
alert(-2 && 6 + 6 && null); // null
// 请问3 : 1 + 1 && 0 && 5 结果为 ?
alert(1 + 1 && 0 && 5); // 0
// console.log(true && false);
// console.log(false && true);
//1.按位与
//计算机中都是二进制计算
//10&20
console.log(12 & 21);//==>4
console.log(12 | 21);//==>29
//计算方式 原码 反码 补码
// 整数的原码/补码/反码都是一样的
//12: 原码:0000 1100
//21:原码:0001 0101
//按位与(&) 对应的两个二进位均为1时,结果位才为1 ,否则为0;
//12: 原码:0000 1100
//21:原码:0001 0101
// 0000 01001 =4
//按位或(|) 对应的一个二进位均为1时,结果位就为1
//12: 原码:0000 1100
//21:原码:0001 0101
// 0001 1101 =29
// 按位异或(^)对应的两位不同时结果为1,相同时为0 总结:相同为0,相异为1
//按位右移 (m>>n)
//提示:往右移动一位,删除二进制的最后一位,逐渐变小,将m的二进制数右移n位,相当于m/ 2的n次方
//末尾删除n位 开头补0
//按位左移 (m>>n)
//开头删除n位 末尾补0
// console.log(-10 >> 2);
// console.log(70 << 2);
// console.log(-230 >> 3);
// console.log(-230 << 3);
//使用if else 判定润年;
// var year = parseInt(prompt());
// if (isNaN(year)) {
// if ((year % 4 == 0 && year % 100 !== 0) || year % 400 == 0) {
// confirm(year + "是闰年");
// } else {
// confirm(year + "不是闰年");
// }
// }
//练习:弹出两次提示框分别输入商品单价和数量,假设总价满1000打九折,当前会员卡内由1300远。如果足够支付,警示框弹出“pay success”
//,否则弹出“pay error“
//if-else
// var price = parseInt(prompt("请输入商品单价"));
// var num = parseInt(prompt("请输入商品数量"));
// var sum = price * num
// var pay;
// var all = 1300;
// if (!isNaN(price) && !isNaN(num)) {
// if (all > sum * 0.9) {
// if (sum > 1000) {
// sum = sum * 0.9;
// all = all - sum;
// alert("pay success");
// confirm("您一共消费" + sum + "元,余额" + all + "元");
// } else {
// all = all - sum;
// alert("pay success");
// confirm("您一共消费" + sum + "元,余额" + all + "元");
// }
// }
// else {
// alert("pay error" + "您余额不足")
// }
// } else {
// alert("请正确输入")
// }
//模板字符串 ${10**10}
// ("陈哥有10*10个男朋友") ==> 模板字符串=("陈哥有${10*10}个男朋友") ==>解析 ("陈哥有100个男朋友")
//switch语句
//练习:输入年份和月份,判断这个月多少天
// var year = Number(prompt("请输入年份"));
// var month = Number(prompt("请输入月份份"));
// if ((year % 4 == 0 && year % 100 !== 0) || year % 400 == 0) {
// switch (month) {
// case 1:confirm(year + "年" + month + "月有31天");break
// case 2:confirm(year + "年" + month + "月有29天");break
// case 3:confirm(year + "年" + month + "月有31天");break
// case 4:confirm(year + "年" + month + "月有30天");break
// case 5:confirm(year + "年" + month + "月有31天");break
// case 6:confirm(year + "年" + month + "月有30天");break
// case 7:confirm(year + "年" + month + "月有31天");break
// case 8:confirm(year + "年" + month + "月有31天");break
// case 9:confirm(year + "年" + month + "月有30天");break
// case 10:confirm(year + "年" + month + "月有31天");break
// case 11:confirm(year + "年" + month + "月有30天");break
// case 12:confirm(year + "年" + month + "月有31天");break
// default:confirm("一年只有12个月")
// }
// }else{
// switch (month) {
// case 1:confirm(year + "年" + month + "月有31天");break
// case 2:confirm(year + "年" + month + "月有28天");break
// case 3:confirm(year + "年" + month + "月有31天");break
// case 4:confirm(year + "年" + month + "月有30天");break
// case 5:confirm(year + "年" + month + "月有31天");break
// case 6:confirm(year + "年" + month + "月有30天");break
// case 7:confirm(year + "年" + month + "月有31天");break
// case 8:confirm(year + "年" + month + "月有31天");break
// case 9:confirm(year + "年" + month + "月有30天");break
// case 10:confirm(year + "年" + month + "月有31天");break
// case 11:confirm(year + "年" + month + "月有30天");break
// case 12:confirm(year + "年" + month + "月有31天");break
// default:confirm("一年只有12个月")
// }
// }
//三目运算符
// switch (month) {
// case 1:confirm(year + "年" + month + "月有31天");break
// case 2:
// ((year % 4 == 0 && year % 100 !== 0) || year % 400 == 0)?confirm(year + "年" + month + "月有29天") : confirm(year + "年" + month + "月有28天");
// break;
// case 3:confirm(year + "年" + month + "月有31天");break
// case 4:confirm(year + "年" + month + "月有30天");break
// case 5:confirm(year + "年" + month + "月有31天");break
// case 6:confirm(year + "年" + month + "月有30天");break
// case 7:confirm(year + "年" + month + "月有31天");break
// case 8:confirm(year + "年" + month + "月有31天");break
// case 9:confirm(year + "年" + month + "月有30天");break
// case 10:confirm(year + "年" + month + "月有31天");break
// case 11:confirm(year + "年" + month + "月有30天");break
// case 12:confirm(year + "年" + month + "月有31天");break
// default:confirm("一年只有12个月")
// }
//循环
//while(循环条件){
// 循环体;;
//}
//循环条件,限制循环千万不要执行死循环;
//练习;在网页中使用prompt,实现石头剪刀布的游戏
// while(true){
// var a = Number(prompt("石头-0 ?-1 剪刀-2 "))
// var b =parseInt( Math.random()*3);
// if(a==0&&b==2){
// alert("你赢了!");
// }else if(a==2&&b==1){
// alert("你赢了!");
// }else if(a==1&&b==0){
// alert("你赢了!");
// }else if(a==b){
// alert("平局!")
// }else{
// alert("你输了!")
// }
// console.log(b);
// }
//for 循环 可以存在多个条件,以最后一个条件为准
//练习 :打印本世纪前10个闰年
// var years =2000
// var year;
// for(i=0;i<10;i++){
// year=years+i*4
// console.log(year)
// }
// for(i=2000;i<2100;i+=4){
// console.log(i);
// }
//练习 打印1-100之间所有的整数。排除所有能被3和5整除的数字;
// var a;
// for(i=1;i<101;i++){
// if(i%3!==0 && i%5!==0){
// console.log(i);
// }
// }
// var a;
// for(i=1;i<101;i++){
// if(i%3==0 || i%5==0){
// continue;
// }else{
// console.log(i);
// }
// }
//break 和 continue
// 练习: 水仙花数 列如: 153=1**3 + 5**3 +3**3
// for (a=1;a < 10;a++) {
// for (b=0; b < 10;b++) {
// for (c=0;c < 10;c++) {
// var sum1 = a * 100 + b * 10 + c;
// var sum2 = a ** 3 + b ** 3 + c ** 3;
// if (sum1 == sum2) {
// console.log(sum1);
// }
// }
// }
// }
// 练习 九九乘法表
// for(a=1;a<=9;a++){
// for(b=1; b<=a;b++){
// console.log(a+"X"+b+'='+ab)
// }
// }
// for(var i=1;i<=9;i++){
// for(var j=1;j<=i;j++){
// document.write(i+""+j+"="+i*j+" ");
// }
// document.write("
");
// }
// for(a=5;a<=9;a++){
// for(b=1;b<a;b++){
// document.write("*"+" ")
// }
// document.write("
")
// }
总结:boot太老火了!!!
近期评论